Skip to content

Commit

Permalink
Update bcdl.go
Browse files Browse the repository at this point in the history
  • Loading branch information
01luna authored Sep 28, 2022
1 parent 13a9598 commit 3bae987
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions bcdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
"gopkg.in/alecthomas/kingpin.v2"
)

func getReviews(releaseFolder string, releaseLink string, releasePageHTML soup.Root) {
func getReviews(releaseFolder string, releaseLink string) {
parsedURL, _ := url.Parse(releaseLink)
dataEmbed := getAttrJSON("data-embed", releasePageHTML)
dataEmbed := getAttrJSON("data-embed")

albumID := gjson.Get(dataEmbed, "tralbum_param.value").String()
releaseType := gjson.Get(dataEmbed, "tralbum_param.name").String()
Expand Down Expand Up @@ -88,24 +88,24 @@ func writeToFile(filename string, data string) error {
return file.Sync()
}

func getDescription(releaseFolder string, releasePageHTML soup.Root) {
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, releasePageHTML soup.Root) {
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, releasePageHTML)
getDescription(releaseFolder)
}
if writeReviews {
color.New(color.FgCyan).Print(string(">>> "))
fmt.Println("Writing Reviews")
getReviews(releaseFolder, releaseLink, releasePageHTML)
getReviews(releaseFolder, releaseLink)
}
}

Expand Down Expand Up @@ -340,8 +340,8 @@ func getRetryURL(link string) string {
return gjson.Get(jsonString, "retry_url").String()
}

func getEmailLink(releaseLink string, releasePageHTML soup.Root) string {
dataEmbed := getAttrJSON("data-embed", releasePageHTML)
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]

Expand Down Expand Up @@ -383,51 +383,51 @@ func getEmailLink(releaseLink string, releasePageHTML soup.Root) string {
return emailContentSoup.Find("a").Attrs()["href"]
}

func getAttrJSON(attr string, releasePageHTML soup.Root) string {
func getAttrJSON(attr string) string {
r := regexp.MustCompile(attr + `=["\']({.+?})["\']`).FindStringSubmatch(html.UnescapeString(releasePageHTML.HTML()))[1]
return r
}

func tryForFreeDownloadURL(releaseLink string, releasePageHTML soup.Root) {
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", releasePageHTML)
tralbum := getAttrJSON("data-tralbum")
freeDownloadPage := gjson.Get(tralbum, "freeDownloadPage").String()
if freeDownloadPage == "" {
releasePageSoup, _ := soup.Get(releaseLink)
releasePageHTML = soup.HTMLParse(releasePageSoup)

selectDownloadURL := getEmailLink(releaseLink, releasePageHTML)
selectDownloadURL := getEmailLink(releaseLink)
downloadURL = getPopplersFromSelectDownloadPage(selectDownloadURL)
releaseFolder = download(downloadURL, "", false, "")
} else {
selectDownloadURL := freeDownloadPage
downloadURL = getRetryURL(selectDownloadURL)
releaseFolder = download(downloadURL, "", false, "")
}
finalAdditives(releaseFolder, releaseLink, releasePageHTML)
finalAdditives(releaseFolder, releaseLink)
}

func freePageDownload(releaseLink string, releasePageHTML soup.Root) {
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, releasePageHTML)
tryForFreeDownloadURL(releaseLink)
fmt.Println()
}

func purchasedPageDownload(releaseLink string, releasePageHTML soup.Root) {
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", releasePageHTML)
tralbum := getAttrJSON("data-tralbum")
albumID := gjson.Get(tralbum, "id")

if collectionSummary == "" {
collectionSummary = getCollectionSummary(true, releasePageHTML)
collectionSummary = getCollectionSummary(true)
}
redownloadMap := organizeRedownloadURLS(collectionSummary)

Expand All @@ -439,7 +439,7 @@ func purchasedPageDownload(releaseLink string, releasePageHTML soup.Root) {

popplersLink := getPopplersFromSelectDownloadPage(downloadPageLink)
releaseFolder := download(popplersLink, "", false, "")
finalAdditives(releaseFolder, releaseLink, releasePageHTML)
finalAdditives(releaseFolder, releaseLink)
fmt.Println()
}

Expand All @@ -455,7 +455,7 @@ func removeDuplicateValues(stringSlice []string) []string {
return list
}

func artistPageLinkGen(releaseLink string, releasePageHTML soup.Root) {
func artistPageLinkGen(releaseLink string) {
// Strips trailing characters off URL
releaseLink = regexp.MustCompile(`.+bandcamp.com`).FindString(releaseLink)

Expand Down Expand Up @@ -490,7 +490,7 @@ func artistPageLinkGen(releaseLink string, releasePageHTML soup.Root) {
get(pageLink)
}
}
func getCollectionSummary(self bool, releasePageHTML soup.Root) string {
func getCollectionSummary(self bool) string {
// Get fan ID
var fanID string
if self {
Expand Down Expand Up @@ -547,10 +547,10 @@ func organizeRedownloadURLS(jsonstring string) map[string]string {
return redownloadMap
}

func userPageLinkGen(releaseLink string, releasePageHTML soup.Root) {
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, releasePageHTML)
collectionSummary = getCollectionSummary(true)

for _, k := range gjson.Get(collectionSummary, "items").Array() {
printReleaseName(gjson.Get(k.Raw, "album_title").String(),
Expand All @@ -570,12 +570,12 @@ func userPageLinkGen(releaseLink string, releasePageHTML soup.Root) {

popplersLink := getPopplersFromSelectDownloadPage(redownloadLink)
releaseFolder := download(popplersLink, "", false, "")
finalAdditives(releaseFolder, releaseLink, releasePageHTML)
finalAdditives(releaseFolder, releaseLink)
fmt.Println()
}
}
} else {
collectionSummary = getCollectionSummary(false, releasePageHTML)
collectionSummary = getCollectionSummary(false)
for _, k := range gjson.Get(collectionSummary, "items").Array() {
if strings.HasSuffix(gjson.Get(k.Raw, "item_url").String(), "/subscribe") {
continue
Expand All @@ -586,7 +586,7 @@ func userPageLinkGen(releaseLink string, releasePageHTML soup.Root) {
}

// Finds out what type of release the link is
func checkReleaseAvailability(link string, releasePageHTML soup.Root) int {
func checkReleaseAvailability(link string) int {
// User page
if regexp.MustCompile(`^(https?:\/\/)?bandcamp.com\/.+$`).MatchString(link) {
return 0
Expand All @@ -612,21 +612,21 @@ func checkReleaseAvailability(link string, releasePageHTML soup.Root) int {
}

// Directs the link to appropriate downloader
func availAndDownload(releaseLink string, releasePageHTML soup.Root) {
releaseAvailability := checkReleaseAvailability(releaseLink, releasePageHTML)
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, releasePageHTML)
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, releasePageHTML)
artistPageLinkGen(releaseLink)
case 2:
purchasedPageDownload(releaseLink, releasePageHTML)
purchasedPageDownload(releaseLink)
case 3:
freePageDownload(releaseLink, releasePageHTML)
freePageDownload(releaseLink)
default:
color.Red("### Paid\n\n")
}
Expand All @@ -652,9 +652,9 @@ func get(releaseLink string) {
color.Red("### Invalid Link\n\n")
} else {
releasePageSoup, _ := soup.Get(releaseLink)
releasePageHTML := soup.HTMLParse(releasePageSoup)
releasePageHTML = soup.HTMLParse(releasePageSoup)

availAndDownload(releaseLink, releasePageHTML)
availAndDownload(releaseLink)
}
}

Expand Down Expand Up @@ -707,6 +707,7 @@ var config = struct {

// Common global variables
var (
releasePageHTML soup.Root
outputFolder string
downloadQuality string
collectionSummary string
Expand Down

0 comments on commit 3bae987

Please sign in to comment.