-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
445 lines (353 loc) · 11.6 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/pkg/browser"
"github.com/schollz/progressbar"
"github.com/shibukawa/configdir"
"golang.org/x/oauth2"
)
const configFileName = "settings.json"
type ConfigStruct struct {
ApplicationID string `json:"application_id"`
Secret string `json:"secret"`
Token string `json:"token"`
}
var (
config ConfigStruct
destinationDir string
limit int
)
func authenticate(applicationID string, secret string) (string, error) {
var (
codeChan = make(chan string)
coubOauthConfig *oauth2.Config
)
coubOauthConfig = &oauth2.Config{
RedirectURL: "http://localhost:8080/callback",
ClientID: applicationID,
ClientSecret: secret,
Endpoint: oauth2.Endpoint{
AuthURL: "http://coub.com/oauth/authorize",
TokenURL: "http://coub.com/oauth/token",
},
}
url := coubOauthConfig.AuthCodeURL("state", oauth2.AccessTypeOffline)
if err := browser.OpenURL(url); err != nil {
fmt.Printf("Error while opening your browser. Please go to URL %s manually.", url)
return "", err
}
http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
codeChan <- r.FormValue("code")
fmt.Fprintf(w, "You can now close the browser.")
})
server := http.Server{
Addr: ":8080",
}
go func() {
if err := server.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
log.Fatalf("error while listening on ':8080': %s", err)
}
return
}
}()
code := <-codeChan
// we don't need http server any more
if err := server.Close(); err != nil {
return "", fmt.Errorf("error while closing server: %s", err)
}
token, err := coubOauthConfig.Exchange(context.Background(), code)
if err != nil {
log.Fatal(err)
}
return token.AccessToken, nil
}
func getNumberOfCoubs(token string) (int, error) {
resp, err := http.Get(fmt.Sprintf("http://coub.com/api/v2/timeline/likes?access_token=%s&per_page=1&page=1", token))
if err != nil {
return 0, fmt.Errorf("error while doing http req: %s", err)
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("error while reading http response: %s", err)
}
timeline := TimelineResponse{}
if err := json.Unmarshal(bytes, &timeline); err != nil {
return 0, fmt.Errorf("error while unmarshalling response json: %s", err)
}
return timeline.TotalPages, nil
}
func getCoubsFromSite(token string, limit int) ([]CoubResponse, error) {
fmt.Println("Downloading coub list from site...")
totalCoubs, err := getNumberOfCoubs(token)
if err != nil {
return nil, fmt.Errorf("error while getting number of coubs: %w", err)
}
if limit == 0 {
limit = totalCoubs
}
var coubs []CoubResponse
bar := progressbar.New(limit)
for page := 1; len(coubs) < totalCoubs && len(coubs) < limit; page++ {
resp, err := http.Get(fmt.Sprintf("http://coub.com/api/v2/timeline/likes?access_token=%s&per_page=50&page=%d", token, page))
if err != nil {
return nil, fmt.Errorf("error while doing http req: %s", err)
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
resp.Body.Close()
return nil, fmt.Errorf("error while reading http response: %s", err)
}
resp.Body.Close()
timeline := TimelineResponse{}
if err := json.Unmarshal(bytes, &timeline); err != nil {
return nil, fmt.Errorf("error while unmarshalling response json: %s", err)
}
if len(timeline.Coubs) == 0 {
break
}
added := 0
for _, coub := range timeline.Coubs {
if len(coubs) < limit {
coubs = append(coubs, coub)
added++
}
}
bar.Add(added)
}
fmt.Println()
return coubs, nil
}
func downloadFile(url string, size int, name string, destinationDir string) error {
finalPath := filepath.Join(destinationDir, name)
stat, err := os.Stat(finalPath)
if err == nil {
if size != 0 && stat.Size() == int64(size) { // already downloaded
return nil
} else { // already downloaded
return nil
}
}
out, err := os.Create(finalPath)
if err != nil {
return fmt.Errorf("error while creating file '%s': %w", finalPath, err)
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("error while getting url '%s': %w", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error while getting url '%s': bad status: %s", url, resp.Status)
}
_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("error while downloading file from url '%s' to '%s': %w", url, finalPath, err)
}
return nil
}
func saveIndexFile(coubs []CoubResponse, destinationDir string) error {
if err := os.MkdirAll(destinationDir, 0777); err != nil {
return fmt.Errorf("error while creating destination dir '%s': %w", destinationDir, err)
}
out, err := os.Create(filepath.Join(destinationDir, "index.html"))
if err != nil {
return fmt.Errorf("error while creating index.html file: %w", err)
}
defer out.Close()
fmt.Fprintf(out, "<!doctype html>\n")
fmt.Fprintf(out, "<html lang=\"en\">\n")
fmt.Fprintf(out, "<head>\n")
fmt.Fprintf(out, "<meta charset=\"utf-8\">")
fmt.Fprintf(out, "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">")
fmt.Fprintf(out, "<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css\" integrity=\"sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh\" crossorigin=\"anonymous\">")
fmt.Fprintf(out, "<title>My Favorite Coubs</title>\n")
fmt.Fprintf(out, "</head>\n")
fmt.Fprintf(out, "<body>\n")
fmt.Fprintf(out, "<div class=\"container\">\n")
fmt.Fprintf(out, "<div class=\"row\">\n")
fmt.Fprintf(out, "<h1>My Favorite Coubs</h1>")
fmt.Fprintf(out, "</div>\n")
fmt.Fprintf(out, "<div class=\"row\">\n")
fmt.Fprintf(out, "<div class=\"col\">\n")
fmt.Fprintf(out, "<table class=\"table table-striped table-hover table-sm\">\n")
fmt.Fprintf(out, "<thead>\n")
fmt.Fprintf(out, "<th scope=\"col\">#</th>\n")
fmt.Fprintf(out, "<th scope=\"col\">Name</th>\n")
fmt.Fprintf(out, "<th scope=\"col\">Tags</th>\n")
fmt.Fprintf(out, "<th scope=\"col\">Video</th>\n")
fmt.Fprintf(out, "</thead>\n")
fmt.Fprintf(out, "<tbody>\n")
for no, coub := range coubs {
fmt.Fprintf(out, "<tr>\n")
fmt.Fprintf(out, "<th scope=\"row\">%d</td>\n", no)
fmt.Fprintf(out, "<td><a href=\"https://coub.com/view/%s\">%s</a></td>\n", coub.Permalink, coub.Title)
fmt.Fprintf(out, "<td>\n")
for i, tag := range coub.Tags {
if i != 0 {
fmt.Fprintf(out, " ")
}
fmt.Fprintf(out, "<div class=\"badge badge-primary\">%s</div>", tag.Title)
}
fmt.Fprintf(out, "</td>\n")
fmt.Fprintf(out, "<td>\n")
fmt.Fprintf(out, "<video width=\"320\" height=\"240\" controls loop><source src=\"%s\" type=\"video/mp4\"></video>", "./"+getFileName(coub, ""))
fmt.Fprintf(out, "</td>\n")
fmt.Fprintf(out, "</tr>\n")
}
fmt.Fprintf(out, "<tbody>\n")
fmt.Fprintf(out, "</table>\n")
fmt.Fprintf(out, "</div>\n")
fmt.Fprintf(out, "</div>\n")
fmt.Fprintf(out, "</div>\n")
fmt.Fprintf(out, "</body>\n")
fmt.Fprintf(out, "</html>\n")
return nil
}
func sanitizeTitle(title string) string {
return strings.ReplaceAll(title, "/", "-")
}
func getFileName(coub CoubResponse, suffix string) string {
url := coub.FileVersions.Share.Default
extension := filepath.Ext(url)
name := sanitizeTitle(coub.Title) + " (" + coub.Permalink + ")" + suffix + extension
return name
}
func downloadCoubs(coubs []CoubResponse, destinationDir string) error {
if err := os.MkdirAll(destinationDir, 0777); err != nil {
return fmt.Errorf("error while creating destination dir '%s': %w", destinationDir, err)
}
fmt.Printf("Downloading %d coubs to '%s'...\n", len(coubs), destinationDir)
bar := progressbar.New(len(coubs) * 3) // looped + video + audio
for _, coub := range coubs {
// looped med
if coub.FileVersions.Share.Default != "" {
name := getFileName(coub, "")
if err := downloadFile(coub.FileVersions.Share.Default, 0, name, destinationDir); err != nil {
return err
}
} else {
//log.Printf("no looped url for coub '%s'", coub.Title)
}
bar.Add(1)
// good video
if coub.FileVersions.HTML5.Video.Higher.URL != "" {
name := getFileName(coub, "_video")
if err := downloadFile(coub.FileVersions.HTML5.Video.Higher.URL, coub.FileVersions.HTML5.Video.Higher.Size, name, destinationDir); err != nil {
return err
}
} else {
//log.Printf("no video url for coub '%s'", coub.Title)
}
bar.Add(1)
// good sound
if coub.FileVersions.HTML5.Audio.High.URL != "" {
name := getFileName(coub, "_audio")
if err := downloadFile(coub.FileVersions.HTML5.Audio.High.URL, coub.FileVersions.HTML5.Audio.High.Size, name, destinationDir); err != nil {
return err
}
} else {
//log.Printf("no audio url for coub '%s'", coub.Title)
}
bar.Add(1)
}
fmt.Println()
return nil
}
func populateConfig() error {
configDirs := configdir.New("mkevac", "coubdl")
folder := configDirs.QueryFolderContainsFile(configFileName)
if folder != nil {
data, err := folder.ReadFile(configFileName)
if err == nil {
json.Unmarshal(data, &config)
}
}
if config.Token != "" { // having just token is enough
return nil
}
// ok, token is empty, but maybe we have application_id and secret?
if config.ApplicationID != "" && config.Secret != "" {
token, err := authenticate(config.ApplicationID, config.Secret)
if err != nil {
return fmt.Errorf("error while authenticating with given application_id and secret: %w", err)
}
config.Token = token
data, err := json.Marshal(&config)
if err != nil {
log.Fatalf("error while marshalling config to json: %s", err)
}
folders := configDirs.QueryFolders(configdir.Global)
folders[0].WriteFile(configFileName, data)
}
// ask for application_id and secret
var applicationID string
var secret string
for applicationID == "" {
fmt.Printf("Please enter Application ID: ")
fmt.Scanln(&applicationID)
if applicationID == "" {
fmt.Println("Application ID is empty.")
}
}
for secret == "" {
fmt.Printf("Please enter Secret: ")
fmt.Scanln(&secret)
if secret == "" {
fmt.Println("Secret is empty.")
}
}
fmt.Println("You have entered")
fmt.Printf("Application ID: %s\n", applicationID)
fmt.Printf("Secret: %s\n", secret)
config.ApplicationID = applicationID
config.Secret = secret
token, err := authenticate(config.ApplicationID, config.Secret)
if err != nil {
return fmt.Errorf("error while authenticating with given application_id and secret: %w", err)
}
config.Token = token
data, err := json.Marshal(&config)
if err != nil {
log.Fatalf("error while marshalling config to json: %s", err)
}
folders := configDirs.QueryFolders(configdir.Global)
folders[0].WriteFile(configFileName, data)
return nil
}
func main() {
flag.StringVar(&destinationDir, "dir", ".", "destination directory for downloading")
flag.IntVar(&limit, "limit", 0, "limit number of coubs to download by this number (0 - no limit)")
flag.Parse()
destinationDir_, err := filepath.Abs(destinationDir)
if err != nil {
log.Fatalf("error while converting destination dir '%s' to absolute path: %s", destinationDir, err)
}
destinationDir = destinationDir_
if err := populateConfig(); err != nil {
log.Fatalf("error while populating config: %s", err)
}
coubs, err := getCoubsFromSite(config.Token, limit)
if err != nil {
log.Fatalf("error while getting coubs from site: %s", err)
}
if err := downloadCoubs(coubs, destinationDir); err != nil {
log.Fatalf("error while dowloading coubs: %s", err)
}
if err := saveIndexFile(coubs, destinationDir); err != nil {
log.Fatalf("error while saving index file: %s", err)
}
browser.OpenURL(fmt.Sprintf("file:///%s/index.html", destinationDir))
}