├── bandcamp_test.go ├── .gitignore ├── README.md ├── LICENSE └── bandcamp.go /bandcamp_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestExecuteCode(t *testing.T) { 8 | } 9 | 10 | func TestFetchPage(t *testing.T) { 11 | } 12 | 13 | func TestDownloadTrack(t *testing.T) { 14 | } 15 | 16 | func TestDownloadAlbum(t *testing.T) { 17 | } 18 | 19 | func TestIsAlbum(t *testing.T) { 20 | } 21 | 22 | func TestSetMetadata(t *testing.T) { 23 | } 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | 26 | *.mp3 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bandcamp-go 2 | A Go program for downloading Bandcamp songs, but moreso a rewrite of [bandcamp-dl](https://github.com/iheanyi/bandcamp-dl). 3 | 4 | # Installation 5 | TODO (Package in Homebrew) 6 | 7 | # Getting Started 8 | With Go installed, you can just run this program by running `go run bandcamp.go 9 | ` and it will download the tracks to 10 | `music/`. Will create a binary soon for OS X. 11 | 12 | # Running Tests 13 | TODO 14 | 15 | # Notes 16 | TODO 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Iheanyi Ekechukwu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /bandcamp.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "log" 7 | "strings" 8 | "os" 9 | "io" 10 | "encoding/json" 11 | "github.com/robertkrimen/otto" 12 | "github.com/PuerkitoBio/goquery" 13 | ) 14 | 15 | func GenerateAlbumMap(jsCode string) map[string]interface{} { 16 | /* 17 | Executes arbitrary JavaScript within the Bandcamp page. 18 | */ 19 | fullCodeBlock := "albumData = " + jsCode 20 | fmt.Println(jsCode) 21 | 22 | vm := otto.New() 23 | vm.Run(fullCodeBlock) 24 | vm.Run(` 25 | albumDataStr = JSON.stringify(albumData); 26 | `) 27 | 28 | var albumMap map[string]interface{} 29 | 30 | if value, err := vm.Get("albumDataStr"); err == nil { 31 | if valueStr, err := value.ToString(); err == nil { 32 | jsonByteArray := []byte(valueStr) 33 | jsonErr := json.Unmarshal(jsonByteArray, &albumMap) 34 | 35 | if jsonErr != nil { 36 | fmt.Println("Error encoding JSON from the JS.") 37 | panic(jsonErr) 38 | } 39 | } 40 | } 41 | 42 | return albumMap 43 | } 44 | 45 | func DownloadAlbum(url string) { 46 | fmt.Println("Here's the URL that we'll be parsing: ", url) 47 | resp, err := http.Get(os.Args[1]) 48 | 49 | doc, err := goquery.NewDocument(url) 50 | 51 | if err != nil { 52 | log.Fatal(err) 53 | } 54 | 55 | doc.Find(".yui-skin-sam script").Each(func(i int, s *goquery.Selection) { 56 | if (i == 1) { 57 | nodeText := s.Text() 58 | albumDataDef := strings.Split(nodeText, "var TralbumData = ")[1] 59 | albumData := strings.Split(albumDataDef, ";")[0] 60 | 61 | albumInfo := GenerateAlbumMap(albumData) 62 | DownloadAlbumTracks(albumInfo) 63 | } 64 | }) 65 | 66 | defer resp.Body.Close() 67 | } 68 | 69 | func DownloadAlbumTracks(albumInfo map[string]interface{}) { 70 | albumTracks := albumInfo["trackinfo"].([]interface{}) 71 | currentAlbum := albumInfo["current"].(map[string]interface{}) 72 | fmt.Println("Album Artist: ", albumInfo["artist"]) 73 | fmt.Println("Album Title: ", currentAlbum["title"]) 74 | fmt.Println("Album Release Date: ", albumInfo["album_release_date"]) 75 | fmt.Println("------------------------------------") 76 | 77 | // Create Directory 78 | directoryName := fmt.Sprintf("albums/%s", currentAlbum["title"]) 79 | 80 | // Making Directory 81 | os.MkdirAll(directoryName, 0700) 82 | 83 | for _, trackInstance := range albumTracks { 84 | track := trackInstance.(map[string]interface{}) 85 | trackFile := track["file"].(map[string]interface{}) 86 | trackUrl := fmt.Sprintf("https:%s", trackFile["mp3-128"]) 87 | trackFileName := fmt.Sprintf("%s/%s.mp3", directoryName, track["title"]) 88 | 89 | fmt.Println("Track Number: ", track["track_num"]) 90 | fmt.Println("Track File: ", trackUrl) 91 | fmt.Println("Track Title: ", track["title"]) 92 | fmt.Println("------------------------------------") 93 | 94 | trackOutFile, err := os.Create(trackFileName) 95 | 96 | if err != nil { 97 | fmt.Println("Error creating the filename.") 98 | panic(err) 99 | } 100 | 101 | defer trackOutFile.Close() 102 | 103 | resp, _ := http.Get(trackUrl) 104 | defer resp.Body.Close() 105 | 106 | _, downloadErr := io.Copy(trackOutFile, resp.Body) 107 | 108 | if downloadErr != nil { 109 | fmt.Println("Error downloading the file.") 110 | panic(downloadErr) 111 | } else { 112 | fmt.Println("Successfuly downloaded file", track["title"], "to", trackFileName) 113 | } 114 | } 115 | } 116 | 117 | func main() { 118 | albumUrl := os.Args[1] 119 | DownloadAlbum(albumUrl) 120 | } 121 | --------------------------------------------------------------------------------