-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbandcamp.go
194 lines (166 loc) · 4.59 KB
/
bandcamp.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"github.com/gocolly/colly"
)
var embedAlbumArt bool
// Define a function to handle command-line flags
func init() {
// Register flags for embedding album art
flag.BoolVar(&embedAlbumArt, "embed-album-art", false, "Embed the album art into tracks?[default:false]")
flag.Parse()
}
// Config struct for environment and file paths
type Config struct {
UserHomeDir string
BaseDir string
}
// File represents the file details in JSON
type File struct {
MP3_128 string `json:"mp3-128"`
}
// TrackInfo represents a single track's data
type TrackInfo struct {
ID *int `json:"id"`
TrackID int `json:"track_id"`
File *File `json:"file"`
Artist *string `json:"artist"`
Title string `json:"title"`
TrackNum int `json:"track_num"`
Duration float64 `json:"duration"`
AltLink *string `json:"alt_link"`
PlayCount *int `json:"play_count"`
IsCapped *bool `json:"is_capped"`
}
// Album represents an album with track information and cover image link
type Album struct {
Name string
TrackInfo []TrackInfo `json:"trackinfo"`
CoverImageLink string
Artist string `json:"Artist"`
}
var site string
func init() {
// Use command-line argument to form the Bandcamp URL
if len(os.Args) < 2 {
log.Fatal("Please provide the Bandcamp URL subdomain as a command-line argument.")
}
site = "https://" + os.Args[1] + ".bandcamp.com"
}
func main() {
// Setup config and fetch album details
var config Config
config.setEnvVars()
// Handle Bandcamp URL input
var Link string
myAlbum := Album{}
if strings.Contains(os.Args[1], "album") {
Link = os.Args[1]
} else {
log.Fatal("No album is provided")
}
// Create a new collector for web scraping
c := colly.NewCollector()
// Extract album data from the page
c.OnHTML("script[data-tralbum]", func(e *colly.HTMLElement) {
link := e.Attr("data-tralbum")
myAlbum.setVars(link)
})
// Extract cover image URL from meta tag
c.OnHTML("meta[property]", func(e *colly.HTMLElement) {
if e.Attr("property") == "og:image" {
myAlbum.CoverImageLink = e.Attr("content")
}
})
// Start scraping the album page
err := c.Visit(Link)
if err != nil {
log.Fatal("Error visiting the page: ", err)
}
// Fetch and download album tracks
myAlbum.getTracks(&config)
fmt.Println("Cover Image Link:", myAlbum.CoverImageLink)
}
// Set environment variables for the config
func (c *Config) setEnvVars() {
c.UserHomeDir, _ = os.UserHomeDir()
}
// Set album variables from JSON data
func (A *Album) setVars(jsonData string) {
err := json.Unmarshal([]byte(jsonData), A)
if err != nil {
log.Fatalf("Failed to parse album JSON: %v", err)
}
}
// Download tracks and save them to the specified destination
func (A *Album) getTracks(C *Config) {
artist := strings.ToLower(A.Artist)
album := strings.ToLower(A.Name)
baseDir := fmt.Sprintf("%v/bandcamp/", C.UserHomeDir)
if C.BaseDir != "" {
baseDir = fmt.Sprintf("%v/", C.BaseDir)
}
destDir := fmt.Sprintf("%v%v/%v/", baseDir, artist, album)
// Create destination directory if it doesn't exist
if err := createDirIfNotExists(destDir); err != nil {
log.Fatal("Failed to create directory: ", err)
}
// Download each track
for _, v := range A.TrackInfo {
if v.File != nil {
fileName := removeIllegalCharacters(fmt.Sprintf("%d-%v.mp3", v.TrackNum, v.Title))
downloadTracks(v.File.MP3_128, fileName, destDir)
}
}
}
// Create a directory if it doesn't already exist
func createDirIfNotExists(destDir string) error {
_, err := os.Stat(destDir)
if os.IsNotExist(err) {
errDir := os.MkdirAll(destDir, 0755)
if errDir != nil {
return fmt.Errorf("failed to create directory: %v", errDir)
}
}
return nil
}
// Download a track from the provided URL
func downloadTracks(url, fileName, destDir string) {
filepath := fmt.Sprintf("%v/%v", destDir, fileName)
// Create the file
out, err := os.Create(filepath)
if err != nil {
log.Printf("Failed to create file %v: %v", filepath, err)
return
}
defer out.Close()
// Get the data from the URL
resp, err := http.Get(url)
if err != nil {
log.Printf("Failed to download track %v: %v", url, err)
return
}
defer resp.Body.Close()
// Write the body to the file
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Printf("Failed to write to file %v: %v", filepath, err)
}
}
// Remove illegal characters from filenames
func removeIllegalCharacters(s string) string {
return strings.Map(func(r rune) rune {
if r == '/' || r == '\\' {
log.Printf("Removed %c from filename: %v\n", r, s)
return -1
}
return r
}, s)
}