-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathfile.go
132 lines (110 loc) · 2.64 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"sync/atomic"
"time"
)
var (
gfyRequest = "https://gfycat.com/cajax/get/%s"
)
// A File contains information on a particular tumblr URL, as well as the user where the URL was found.
type File struct {
User *User
URL string
UnixTimestamp int64
Filename string
}
func newFile(URL string) File {
return File{
URL: URL,
Filename: path.Base(URL),
}
}
// Download downloads a file specified in the file's URL.
func (f File) Download() {
filepath := path.Join(cfg.DownloadDirectory, f.User.String(), path.Base(f.Filename))
var resp *http.Response
var err error
var pic []byte
for {
resp, err = http.Get(f.URL)
if err != nil {
log.Println(err)
continue
}
defer resp.Body.Close()
pic, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("ReadAll:", err)
continue
}
break
}
err = ioutil.WriteFile(filepath, pic, 0644)
if err != nil {
log.Fatal("WriteFile:", err)
}
err = os.Chtimes(filepath, time.Now(), time.Unix(f.UnixTimestamp, 0))
if err != nil {
log.Println(err)
}
FileTracker.Signal(f.Filename)
pBar.Increment()
f.User.downloadWg.Done()
atomic.AddUint64(&f.User.filesProcessed, 1)
atomic.AddUint64(&gStats.filesDownloaded, 1)
atomic.AddUint64(&gStats.bytesDownloaded, uint64(len(pic)))
}
// String is the standard method for the Stringer interface.
func (f File) String() string {
date := time.Unix(f.UnixTimestamp, 0)
return f.User.String() + " - " + date.Format("2006-01-02 15:04:05") + " - " + path.Base(f.Filename)
}
// Gfy houses the Gfycat response.
type Gfy struct {
GfyItem struct {
Mp4Url string `json:"mp4Url"`
WebmURL string `json:"webmUrl"`
} `json:"gfyItem"`
}
// GetGfycatURL gets the appropriate Gfycat URL for download, from a "normal" link.
func GetGfycatURL(slug string) string {
gfyURL := fmt.Sprintf(gfyRequest, slug)
var resp *http.Response
for {
resp2, err := http.Get(gfyURL)
if err != nil {
log.Println("GetGfycatURL: ", err)
} else {
resp = resp2
break
}
}
defer resp.Body.Close()
gfyData, err := ioutil.ReadAll(resp.Body)
checkFatalError(err)
var gfy Gfy
err = json.Unmarshal(gfyData, &gfy)
checkFatalError(err, "Gfycat Unmarshal:")
return gfy.GfyItem.Mp4Url
}
func getGfycatFiles(b, slug string) []File {
var files []File
regexResult := gfycatSearch.FindStringSubmatch(b)
if regexResult != nil {
for i, v := range regexResult[1:] {
gfyFile := newFile(GetGfycatURL(v))
if slug != "" {
gfyFile.Filename = fmt.Sprintf("%s_gfycat_%02d.mp4", slug, i+1)
}
files = append(files, gfyFile)
}
}
return files
}