-
Notifications
You must be signed in to change notification settings - Fork 0
/
wimp-get.go
245 lines (206 loc) · 5.07 KB
/
wimp-get.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
package main
import (
"os"
"fmt"
"regexp"
"encoding/json"
"path"
"io"
"io/ioutil"
"wimp-get/wimp"
"wimp-get/platform"
"os/exec"
"net/http"
"strings"
)
func SanitiseFilename(filename string) (newName string, e error) {
r, e := regexp.Compile("[\\?<>:/\"\\\\|\\*]")
if e != nil {
return
}
newName = r.ReplaceAllString(filename, "")
return
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage: %s <wimp id or url>\n", os.Args[0])
os.Exit(-1)
}
var id string;
wimpUrl, _ := regexp.Compile("^https?://play\\.wimpmusic\\.com/album/")
if wimpUrl.MatchString(os.Args[1]) {
id = wimpUrl.ReplaceAllString(os.Args[1], "")
} else {
id = os.Args[1]
}
exe, _ := os.Executable()
wDir, e := platform.DirOf(exe)
if e != nil {
panic(e)
}
magicData, e := ioutil.ReadFile(wDir + "/magic.json")
if e != nil {
panic(e)
}
var magic map[string]interface{}
e = json.Unmarshal(magicData, &magic)
if e != nil {
panic(e)
}
println("Looking up album...")
album, e := wimp.GetAlbum(id, magic["sessionId"].(string))
if e != nil {
panic(e)
}
fmt.Printf("[ %s - %s (%d) ]\n", album.Artist, album.Title, album.Year)
// Determine whether we have more than one disc
maxdisc := 0
for _, track := range album.Tracks {
if track.Volume > maxdisc {
maxdisc = track.Volume
}
}
var multidisc bool
if maxdisc > 1 {
multidisc = true
}
dirName := album.Artist+" - "+album.Title+" ("+fmt.Sprintf("%d", album.Year)+") [WEB FLAC]"
dirName, e = SanitiseFilename(dirName)
if e != nil {
panic(e)
}
e = os.Mkdir(dirName, os.FileMode(0755))
if e != nil {
panic(e)
}
var firstFile string // we use this later to generate spectrals
// Time to do the ripping!
for i, track := range album.Tracks {
num := fmt.Sprintf("%d", track.Number)
if len(num) < 2 {
num = "0"+num
}
fmt.Printf("[%d/%s] %s...", track.Volume, num, track.Title)
var filename = fmt.Sprintf("%s - %s.flac", num, track.Title)
filename, e = SanitiseFilename(filename)
if e != nil {
panic(e)
}
if (multidisc) {
filename = fmt.Sprintf("%s/Disc %d/%s", dirName, track.Volume, filename)
} else {
filename = fmt.Sprintf("%s/%s", dirName, filename)
}
if i == 0 {
firstFile = filename
}
// create disc dir if necessary
if _, e = os.Stat(path.Dir(filename)); e != nil {
if os.IsNotExist(e) {
e = os.Mkdir(path.Dir(filename), os.FileMode(0755))
if e != nil {
panic(e)
}
} else {
panic(e)
}
}
resp, e := http.Get(track.Url)
if e != nil {
panic(e)
}
ffmpeg := exec.Command(magic["ffmpeg"].(string), "-i", "-", "-metadata", "title="+track.Title, "-metadata", "artist="+track.Artist,
"-metadata", "album="+album.Title, "-metadata", "year="+fmt.Sprintf("%d", album.Year), "-metadata", "track="+fmt.Sprintf("%d", track.Number),
"-metadata", "albumartist="+album.Artist, "-metadata", "discnumber="+fmt.Sprintf("%d", track.Volume), filename)
stdin, e := ffmpeg.StdinPipe()
if e != nil {
panic(e)
}
e = ffmpeg.Start()
if e != nil {
panic(e)
}
_, e = io.Copy(stdin, resp.Body)
if e != nil {
panic(e)
}
resp.Body.Close()
println(" Done!")
}
// Get art
print("Getting album art...")
resp, e := http.Get(album.CoverUrl)
if e != nil {
panic(e)
}
defer resp.Body.Close()
var out io.Writer
if multidisc {
var outs []io.Writer
for i := 1; i <= maxdisc; i++ {
o, e := os.Create(fmt.Sprintf("%s/Disc %d/cover.jpg", dirName, i))
if e != nil {
panic(e)
}
outs = append(outs, o)
}
out = io.MultiWriter(outs...)
e = nil
} else {
out, e = os.Create(dirName+"/cover.jpg")
}
if e != nil {
panic(e)
}
_, e = io.Copy(out, resp.Body)
if e != nil {
panic(e)
}
println(" Done!")
// Generate Spectrals
var choice string
for !(choice == "y" || choice == "n") {
print("Generate spectrals? [y/n] ")
fmt.Scanln(&choice)
choice = strings.TrimRight(choice, "\n")
}
if choice == "y" {
full := exec.Command(magic["sox"].(string), firstFile, "-n", "remix", "1", "spectrogram",
"-x", "3000", "-y", "513", "-z", "120", "-w", "Kaiser", "-o", "SpecFull.png")
zoom := exec.Command(magic["sox"].(string), firstFile, "-n", "remix", "1", "spectrogram",
"-X", "500", "-y", "1025", "-z", "120", "-w", "Kaiser", "-S", "0:30", "-d", "0:04", "-o", "SpecZoom.png")
e = full.Run()
if e != nil {
println("Error generating full spectral!")
} else {
println("SpecFull.png written")
}
e = zoom.Run()
if e != nil {
println("Error generating zoomed spectral!")
} else {
println("SpecZoom.png written")
}
}
// Make .torrent file
choice = ""
for !(choice == "y" || choice == "n") {
print("Create .torrent file? [y/n] ")
fmt.Scanln(&choice)
choice = strings.TrimRight(choice, "\n")
}
if choice == "y" {
print("Announce URL: ")
var announce string
fmt.Scanln(&announce)
announce = strings.TrimRight(announce, "\n")
torrent := exec.Command(magic["mktorrent"].(string), "-l", "20", "-a", announce, dirName)
e = torrent.Run()
if e != nil {
println("Error creating .torrent file!")
} else {
fmt.Printf("%s.torrent created\n", dirName)
}
}
println("All done, exiting")
}