-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcdl.go
791 lines (679 loc) · 22.5 KB
/
bcdl.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
package main
import (
"archive/zip"
"bufio"
"fmt"
"html"
"io"
"io/ioutil"
"log"
"math/rand"
"mime"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/anaskhan96/soup"
"github.com/cheggaaa/pb"
"github.com/fatih/color"
"github.com/jinzhu/configor"
"github.com/tidwall/gjson"
"gopkg.in/alecthomas/kingpin.v2"
)
func getReviews(releaseFolder string, releaseLink string) {
parsedURL, _ := url.Parse(releaseLink)
dataEmbed := getAttrJSON("data-embed")
albumID := gjson.Get(dataEmbed, "tralbum_param.value").String()
releaseType := gjson.Get(dataEmbed, "tralbum_param.name").String()
// Get all reviews from release
jsonstring, err := soup.Post(fmt.Sprintf(`https://%s/api/tralbumcollectors/2/reviews`, parsedURL.Host),
"application/x-www-form-urlencoded",
fmt.Sprintf(`{"tralbum_type":"%s","tralbum_id":%s,"token":"1:9999999999:9999999999:1:1:0","count":9999999999,"exclude_fan_ids":[]}`, string(releaseType[0]), albumID))
if err != nil {
panic(err)
}
prefix := "\n\n"
if !writeDescription {
prefix = ""
writeToFile(filepath.Join(releaseFolder, "info.txt"), "")
}
f, err := os.OpenFile(filepath.Join(releaseFolder, "info.txt"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()
// If there are reviews, write them
if gjson.Get(jsonstring, "results.#").Int() != 0 {
if _, err = f.WriteString(prefix + "# Reviews:"); err != nil {
panic(err)
}
for _, k := range gjson.Get(jsonstring, "results").Array() {
cleaned := removeWhiteSpace(strings.TrimSpace(html.UnescapeString(k.String())))
review := "\n\n" + gjson.Get(cleaned, "name").String() +
" (" + gjson.Get(cleaned, "username").String() + "): " +
gjson.Get(cleaned, "why").String()
if _, err = f.WriteString(review); err != nil {
panic(err)
}
}
}
}
// https://golangcode.com/writing-to-file/
func writeToFile(filename string, data string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
_, err = io.WriteString(file, data)
if err != nil {
return err
}
return file.Sync()
}
func getDescription(releaseFolder string) {
description := releasePageHTML.Find("meta", "name", "description").Attrs()["content"]
writeToFile(filepath.Join(releaseFolder, "info.txt"), strings.TrimSpace(html.UnescapeString(description)))
}
func finalAdditives(releaseFolder string, releaseLink string) {
releasePageSoup, _ := soup.Get(releaseLink)
releasePageHTML = soup.HTMLParse(releasePageSoup)
if writeDescription {
color.New(color.FgCyan).Print(string(">>> "))
fmt.Println("Writing Description")
getDescription(releaseFolder)
}
if writeReviews {
color.New(color.FgCyan).Print(string(">>> "))
fmt.Println("Writing Reviews")
getReviews(releaseFolder, releaseLink)
}
}
// https://stackoverflow.com/questions/20357223/easy-way-to-unzip-file-with-golang#24792688
func unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
os.MkdirAll(dest, 0755)
// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()
path := filepath.Join(dest, f.Name)
// Check for ZipSlip (Directory traversal)
if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) {
return fmt.Errorf("illegal file path: %s", path)
}
if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode())
} else {
os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
return nil
}
for _, f := range r.File {
err := extractAndWriteFile(f)
if err != nil {
return err
}
}
return nil
}
func download(url string, releaseFolder string, ignoreExistingFolder bool, filenamePrefix string) string {
errored := false
retry:
resp, err := http.Get(url)
if err != nil {
color.Red("### Unable to download")
return ""
}
defer resp.Body.Close()
_, params, _ := mime.ParseMediaType(resp.Header.Get("Content-Disposition"))
filePath := filepath.Join(outputFolder, filenamePrefix+params["filename"])
if releaseFolder == "" {
releaseFolder = strings.TrimSuffix(filePath, filepath.Ext(filePath))
}
if _, err := os.Stat(outputFolder); os.IsNotExist(err) {
os.MkdirAll(outputFolder, 0700)
}
if !errored {
if !ignoreExistingFolder {
if _, err := os.Stat(releaseFolder); !os.IsNotExist(err) {
color.Red("### Exists")
return releaseFolder
}
}
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
if keepZip {
color.Red("### Exists")
} else if filepath.Ext(filePath) != ".zip" {
color.New(color.FgCyan).Print(string(">>> "))
fmt.Println("Moving")
os.MkdirAll(releaseFolder, 0600)
err := os.Rename(filePath, filepath.Join(releaseFolder, filepath.Base(filePath)))
if err != nil {
log.Fatal(err)
}
} else {
color.New(color.FgCyan).Print(string(">>> "))
fmt.Print("Unzipping")
unzip(filePath, releaseFolder)
fmt.Println(" - Done")
os.Remove(filePath)
}
return releaseFolder
}
}
out, _ := os.Create(filePath)
defer out.Close()
i, _ := strconv.Atoi(resp.Header.Get("Content-Length"))
var sourceSize int64 = int64(i)
if noBar {
color.New(color.FgCyan).Print(string(">>> "))
fmt.Print("Downloading")
if _, err = io.Copy(out, resp.Body); err != nil {
color.Red("### Error Saving")
errored = true
goto retry
}
fmt.Println(" - Done")
} else {
bar := pb.New(int(sourceSize)).SetUnits(pb.U_BYTES).SetRefreshRate(time.Millisecond * 10)
bar.ShowSpeed = true
bar.Start()
reader := bar.NewProxyReader(resp.Body)
if _, err = io.Copy(out, reader); err != nil {
color.Red("### Error Saving")
errored = true
bar.Finish()
goto retry
}
bar.Finish()
}
out.Close()
if keepZip {
return releaseFolder
} else if filepath.Ext(filePath) != ".zip" {
color.New(color.FgCyan).Print(string(">>> "))
fmt.Println("Moving")
os.MkdirAll(releaseFolder, 0600)
err := os.Rename(filePath, filepath.Join(releaseFolder, filepath.Base(filePath)))
if err != nil {
log.Fatal(err)
}
os.Remove(filePath)
} else {
color.New(color.FgCyan).Print(string(">>> "))
fmt.Print("Unzipping")
unzip(filePath, releaseFolder)
fmt.Println(" - Done")
err = os.Remove(filePath)
if err != nil {
log.Panic(err)
}
}
return releaseFolder
}
func getPopplersFromSelectDownloadPage(selectDownloadURL string) string {
parsedURL, _ := url.Parse(selectDownloadURL)
urlQuerys := parsedURL.Query()
// Change some of the url parameters because email download links and collection download links are different.
if urlQuerys.Get("from") == "collection" {
selectDownloadPageHTML, _ := soup.Get(selectDownloadURL)
urlQuerys.Set("id", regexp.MustCompile(`id=(\d*)&`).FindStringSubmatch(selectDownloadPageHTML)[1])
urlQuerys.Set("type", regexp.MustCompile(`\/download\/(album|track)\?`).FindStringSubmatch(selectDownloadPageHTML)[1])
}
params := url.Values{}
params.Add("enc", downloadQuality)
params.Add("id", urlQuerys.Get("id"))
params.Add("payment_id", urlQuerys.Get("payment_id"))
params.Add("sig", urlQuerys.Get("sig"))
params.Add(".rand", "1234567891234")
params.Add(".vrs", "1")
popplersPage, _ := soup.Get("https://popplers5.bandcamp.com/statdownload/" + urlQuerys.Get("type") + "?" + params.Encode())
jsonString := regexp.MustCompile(`{\".*\"}`).FindString(popplersPage)
downloadURL := gjson.Get(jsonString, "download_url").String()
color.New(color.FgGreen).Print(string("==> "))
fmt.Println(downloadURL)
return downloadURL
}
func getRetryURL(link string) string {
selectDownloadPageHTML, _ := soup.Get(link)
parsedURL, _ := url.Parse(link)
urlQuerys := parsedURL.Query()
releaseID := "id=" + urlQuerys.Get("id")
releaseType := regexp.MustCompile(`download_type_str.*?(album|track)`).FindStringSubmatch(selectDownloadPageHTML)[1]
fsig := regexp.MustCompile(`fsig=\w{32}\b`).FindString(selectDownloadPageHTML)
ts := regexp.MustCompile(`ts=\w{10}.[0-9]\b`).FindString(selectDownloadPageHTML)
// All of the parts must be present for the request to work
if releaseID == "" || fsig == "" || ts == "" {
return ""
}
color.New(color.FgCyan).Print(string(">>> "))
fmt.Println(fsig, releaseID, ts)
popplersPage, _ := soup.Get(fmt.Sprintf("https://popplers5.bandcamp.com/statdownload/%s?enc=%s&%s&%s&%s", releaseType, downloadQuality, fsig, releaseID, ts))
jsonString := regexp.MustCompile(`{\".*\"}`).FindString(popplersPage)
color.New(color.FgGreen).Print(string("==> "))
fmt.Println(gjson.Get(jsonString, "retry_url").String())
return gjson.Get(jsonString, "retry_url").String()
}
func getEmailLink(releaseLink string) string {
dataEmbed := getAttrJSON("data-embed")
releaseID := gjson.Get(dataEmbed, "tralbum_param.value").String()
releaseType := regexp.MustCompile(`bandcamp.com\/?(track|album)\/`).FindStringSubmatch(releaseLink)[1]
// Set email and clear inbox (manually doing request since soup doesn't support DELETE requests)
emailAddr := config.UserName + releaseID + strconv.Itoa(os.Getpid()) + "@getnada.com"
req, _ := http.NewRequest("DELETE", "https://getnada.com/api/v1/inboxes/"+emailAddr, nil)
Client := &http.Client{}
Client.Do(req)
baseURL, _ := url.Parse(releaseLink)
baseURL.Path = "email_download"
data := url.Values{}
data.Add("address", emailAddr)
data.Add("country", "US")
data.Add("encoding_name", "none")
data.Add("item_id", releaseID)
data.Add("item_type", releaseType)
data.Add("postcode", "20500")
// Send request to get download link
soup.PostForm(baseURL.String(), data)
// Wait until the email has been received. Checks every second.
var UID string
for UID == "" {
inboxData, _ := soup.Get("https://getnada.com/api/v1/inboxes/" + emailAddr)
UID = gjson.Get(inboxData, "msgs.0.uid").String()
for _, icon := range []string{"-", "\\", "|", "/"} {
color.New(color.FgCyan).Print("\r>>> WAITING " + icon)
time.Sleep(250 * time.Millisecond)
}
}
fmt.Println()
// Get content of the email
emailData, _ := soup.Get("https://getnada.com/api/v1/messages/html/" + UID)
emailContentSoup := soup.HTMLParse(emailData)
return emailContentSoup.Find("a").Attrs()["href"]
}
func getAttrJSON(attr string) string {
r := regexp.MustCompile(attr + `=["\']({.+?})["\']`).FindStringSubmatch(html.UnescapeString(releasePageHTML.HTML()))[1]
return r
}
func tryForFreeDownloadURL(releaseLink string) {
// Searching for direct link to free download page. If mmissing, the release may be behind an email wall
var downloadURL string
var releaseFolder string
tralbum := getAttrJSON("data-tralbum")
freeDownloadPage := gjson.Get(tralbum, "freeDownloadPage").String()
if freeDownloadPage == "" {
releasePageSoup, _ := soup.Get(releaseLink)
releasePageHTML = soup.HTMLParse(releasePageSoup)
selectDownloadURL := getEmailLink(releaseLink)
downloadURL = getPopplersFromSelectDownloadPage(selectDownloadURL)
releaseFolder = download(downloadURL, "", false, "")
} else {
selectDownloadURL := freeDownloadPage
downloadURL = getRetryURL(selectDownloadURL)
releaseFolder = download(downloadURL, "", false, "")
}
finalAdditives(releaseFolder, releaseLink)
}
func freePageDownload(releaseLink string) {
nameSection := releasePageHTML.Find("div", "id", "name-section")
printReleaseName(nameSection.Find("h2", "class", "trackTitle").Text(),
nameSection.Find("span").Find("a").Text())
tryForFreeDownloadURL(releaseLink)
fmt.Println()
}
func purchasedPageDownload(releaseLink string) {
nameSection := releasePageHTML.Find("div", "id", "name-section")
printReleaseName(nameSection.Find("h2", "class", "trackTitle").Text(),
nameSection.Find("span").Find("a").Text())
tralbum := getAttrJSON("data-tralbum")
albumID := gjson.Get(tralbum, "id")
if collectionSummary == "" {
collectionSummary = getCollectionSummary(true)
}
redownloadMap := organizeRedownloadURLS(collectionSummary)
salesID := gjson.Get(collectionSummary, fmt.Sprintf(`items.#(item_id=="%s").sale_item_id`, albumID))
downloadPageLink := redownloadMap[salesID.String()]
color.New(color.FgGreen).Print(string("==> "))
fmt.Println(downloadPageLink)
popplersLink := getPopplersFromSelectDownloadPage(downloadPageLink)
releaseFolder := download(popplersLink, "", false, "")
finalAdditives(releaseFolder, releaseLink)
fmt.Println()
}
func removeDuplicateValues(stringSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range stringSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func artistPageLinkGen(releaseLink string) {
// Strips trailing characters off URL
releaseLink = regexp.MustCompile(`.+bandcamp.com`).FindString(releaseLink)
releasePageSoup, _ := soup.Get(releaseLink + "/music")
releasePageHTML = soup.HTMLParse(releasePageSoup)
if scriptJSON := regexp.MustCompile(`application\/ld\+json["\']>\s+({.+?})\s+<`).FindStringSubmatch(html.UnescapeString(releasePageHTML.HTML())); scriptJSON != nil {
get(gjson.Get(scriptJSON[1], "@id").String())
return
}
var pageLinks []string
u, err := url.Parse(releaseLink)
if err != nil {
log.Fatal(err)
}
// Gets all links in the middle box
for _, boxLink := range releasePageHTML.Find("div", "class", "leftMiddleColumns").FindAll("a") {
rel, err := u.Parse(boxLink.Attrs()["href"])
if err != nil {
log.Fatal(err)
}
pageLinks = append(pageLinks, rel.String())
}
// Removes duplicate values from pages that have featured releases
pageLinks = removeDuplicateValues(pageLinks)
// Sending individual links to get retested
for _, pageLink := range pageLinks {
get(pageLink)
}
}
func getCollectionSummary(self bool) string {
// Get fan ID
var fanID string
if self {
collectionSummary, err := soup.Get("https://bandcamp.com/api/fan/2/collection_summary")
if err != nil {
panic(err)
}
fanID = gjson.Get(collectionSummary, "fan_id").String()
} else {
buttonid := releasePageHTML.Find("div", "id", "following-actions").Find("button").Attrs()["id"]
fanID = regexp.MustCompile(`follow-unfollow_(\d*)`).FindStringSubmatch(buttonid)[1]
}
// Get all redownload urls from profile
jsonstring, err := soup.Post("https://bandcamp.com/api/fancollection/1/collection_items",
"application/x-www-form-urlencoded",
fmt.Sprintf(`{"fan_id":%s,"older_than_token":"9999999999::a::","count":9999999999}`, fanID))
if err != nil {
panic(err)
}
return jsonstring
}
func removeWhiteSpace(s string) string {
// Removes a variety of white space types. Probably breaks script languages
out := regexp.MustCompile(`^[\s\p{Zs}\p{Cf}]+|[\s\p{Zs}\p{Cf}]+$`).ReplaceAllString(strings.TrimSpace(s), "")
out = regexp.MustCompile(`[\s\p{Zs}\p{Cf}]{2,}`).ReplaceAllString(out, " ")
return out
}
func printReleaseName(releaseTitle string, releaseArtist string) {
color.New(color.FgBlue).Print(string("--- "))
fmt.Println(removeWhiteSpace(releaseTitle), "by", removeWhiteSpace(releaseArtist))
}
func organizeRedownloadURLS(jsonstring string) map[string]string {
redownloadJSON := gjson.Get(jsonstring, "redownload_urls")
redownloadMap := make(map[string]string)
// Get map of correct sales IDs
for k, v := range redownloadJSON.Map() {
if strings.HasPrefix(k, "p") || strings.HasPrefix(k, "c") {
redownloadMap[k[1:]] = v.String()
} else {
u, err := url.Parse(v.String())
urlQuerys := u.Query()
if err != nil {
log.Fatal(err)
}
redownloadMap[urlQuerys.Get("sitem_id")] = v.String()
}
}
return redownloadMap
}
func userPageLinkGen(releaseLink string) {
// Decide if the user page entered is the user's or another's
if config.UserName == regexp.MustCompile(`.*bandcamp.com/(.*)`).FindStringSubmatch(releaseLink)[1] {
collectionSummary = getCollectionSummary(true)
for _, k := range gjson.Get(collectionSummary, "items").Array() {
printReleaseName(gjson.Get(k.Raw, "album_title").String(),
gjson.Get(k.Raw, "band_name").String())
redownloadMap := organizeRedownloadURLS(collectionSummary)
redownloadLink := redownloadMap[gjson.Get(k.Raw, "sale_item_id").String()]
if redownloadLink == "" {
if strings.HasSuffix(gjson.Get(k.Raw, "item_url").String(), "/subscribe") {
continue
}
get(gjson.Get(k.Raw, "item_url").String())
} else {
color.New(color.FgGreen).Print(string("==> "))
fmt.Println(redownloadLink)
popplersLink := getPopplersFromSelectDownloadPage(redownloadLink)
releaseFolder := download(popplersLink, "", false, "")
finalAdditives(releaseFolder, releaseLink)
fmt.Println()
}
}
} else {
collectionSummary = getCollectionSummary(false)
for _, k := range gjson.Get(collectionSummary, "items").Array() {
if strings.HasSuffix(gjson.Get(k.Raw, "item_url").String(), "/subscribe") {
continue
}
get(gjson.Get(k.Raw, "item_url").String())
}
}
}
// Finds out what type of release the link is
func checkReleaseAvailability(link string) int {
// User page
if regexp.MustCompile(`^(https?:\/\/)?bandcamp.com\/.+$`).MatchString(link) {
return 0
}
// Artist page
if regexp.MustCompile(`^(https?:\/\/)?.+\.bandcamp\.com\/?(\w+)?$`).MatchString(link) {
return 1
}
// Purchased
if releasePageHTML.FindStrict("a", "class", "you-own-this-link").Error == nil {
return 2
}
// Check if free download
buyButton := releasePageHTML.FindStrict("h4", "class", "ft compound-button main-button")
if buyButton.Error == nil {
if strings.Contains(buyButton.FullText(), "name your price") {
return 3
} else if strings.Contains(buyButton.FullText(), "Free Download") {
return 3
}
}
return 4
}
// Directs the link to appropriate downloader
func availAndDownload(releaseLink string) {
releaseAvailability := checkReleaseAvailability(releaseLink)
switch releaseAvailability {
case 0:
color.New(color.FgCyan).Print(string(">>> "))
fmt.Print("Getting links from User Page (May take a while)\n\n")
userPageLinkGen(releaseLink)
case 1:
color.New(color.FgCyan).Print(string(">>> "))
fmt.Print("Getting links from Artist Page (May take a while)\n\n")
artistPageLinkGen(releaseLink)
case 2:
purchasedPageDownload(releaseLink)
case 3:
freePageDownload(releaseLink)
default:
color.Red("### Paid\n\n")
}
}
// Makes sure url is valid Bandcamp link
func validateLink(link string) string {
link = strings.TrimSpace(link)
re := regexp.MustCompile(`(([\w-]+\.)?bandcamp.com\/((\w+)(\/.*)?)?)`)
if validLink := re.FindStringSubmatch(link); validLink != nil {
return "https://" + validLink[1]
}
return ""
}
// Validate link, get link page, continues, returns downloaded file path
func get(releaseLink string) {
color.New(color.FgGreen).Print(string("==> "))
fmt.Println(releaseLink)
releaseLink = validateLink(releaseLink)
if releaseLink == "" {
color.Red("### Invalid Link\n\n")
} else {
releasePageSoup, _ := soup.Get(releaseLink)
releasePageHTML = soup.HTMLParse(releasePageSoup)
availAndDownload(releaseLink)
}
}
// Reads file and returns lines as array
func scanLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, nil
}
// Prints logo at startup
func printLogo() {
logo := `
__ __ __
/ | / |/ |
## |____ _______ ____## |## |
## \ / | / ## |## |
####### |/#######/ /####### |## |
## | ## |## | ## | ## |## |
## |__## |## \_____ ## \__## |## |
## ##/ ## |## ## |## |
#######/ #######/ #######/ ##/`
for _, c := range logo {
if c == '#' {
color.New(color.FgCyan).Print(string(c))
} else {
fmt.Print(string(c))
}
}
fmt.Print("\n\n")
}
// Config for login
var config = struct {
Identity string
UserName string
}{}
// Common global variables
var (
releasePageHTML soup.Root
outputFolder string
downloadQuality string
collectionSummary string
writeDescription bool
writeReviews bool
noBar bool
keepZip bool
)
func main() {
// Print logo. wow.
printLogo()
// If config file does not exist, create and fill with instructions
if _, err := os.Stat("config.yaml"); os.IsNotExist(err) {
configExample := []byte(
"# Load bandcamp.com -> Inspect Element -> Application -> Cookies -> identity\n" +
"identity: \n" +
"# Your profile name - bandcamp.com/(username)\n" +
"username: ")
ioutil.WriteFile("config.yaml", configExample, 0644)
}
// Load config file data
configor.Load(&config, "config.yaml")
// Generate placeholder username if config username is blank
if config.UserName == "" {
rand.Seed(time.Now().UnixNano())
config.UserName = fmt.Sprint(rand.Int())
}
// Set bandcamp login cookie and generic user agent
soup.Cookie("identity", config.Identity)
soup.Header("User-Agent", "Mozilla/5.0 (Windows NT 6.2 rv:20.0) Gecko/20121202 Firefox/20.0")
// Get CLI flags
batch := kingpin.Flag("batch", "Download From download_links.txt").Short('b').Bool()
zFlag := kingpin.Flag("zipped", "Keep albums in .zip format (don't extract)").Short('z').Bool()
dqFlag := kingpin.Flag("quality", "Quality of Download (mp3-v0, mp3-320, flac, aac-hi, vorbis, alac, wav, aiff-lossless)").Default("flac").Short('q').String()
ofFlag := kingpin.Flag("output", "Output Folder").Default("downloads").Short('o').String()
rlArg := kingpin.Arg("url", "URL to Download").String()
wdFlag := kingpin.Flag("description", "Download and write description to info.txt").Short('d').Bool()
wrFlag := kingpin.Flag("reviews", "Download and write reviews to info.txt").Short('r').Bool()
nbFlag := kingpin.Flag("nobar", "Turns off progress bar").Short('p').Bool()
kingpin.Parse()
// Assign flags to variables
downloadQuality = *dqFlag
outputFolder = *ofFlag
releaseLink := *rlArg
writeDescription = *wdFlag
writeReviews = *wrFlag
noBar = *nbFlag
keepZip = *zFlag
if *batch {
// Batch downloading
if _, err := os.Stat("download_links.txt"); os.IsNotExist(err) {
color.Blue("--- Created download_links.txt")
os.Create("download_links.txt")
os.Exit(0)
}
data, err := scanLines("download_links.txt")
if err != nil {
fmt.Println("File reading error", err)
os.Exit(0)
}
for i := 0; i < len(data); i++ {
get(data[i])
}
} else {
if releaseLink != "" {
get(releaseLink)
} else {
color.Red("### Please enter a URL")
}
}
}