-
Notifications
You must be signed in to change notification settings - Fork 0
/
sounds.py
304 lines (238 loc) · 11.7 KB
/
sounds.py
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
from urllib2 import urlopen, URLError, HTTPError
import json
import os
import re
from mutagen.mp3 import MP3, HeaderNotFoundError
from mutagen.id3 import ID3, TIT2, TIT3, TALB, TPE1, TRCK, TYER,USLT, APIC, error
from dialog import Ui_Status
YOUR_CLIENT_ID = "c585c5f24b092caec68984885cf2b0db"
resolveUrl = "http://api.soundcloud.com/resolve.json?url="
usersUrl = "http://api.soundcloud.com/users/"
class Sounds:
def __init__(self, url, path, likes=False, set=False, song = False, user = False):
self.url = url
self.likes = likes
self.set = set
self.song = song
self.user = user
self.errors = []
#path to make new dir in
self.path = path
#split url at /'s, returns a list
urlsplitted = self.url.split("/")
if "likes" in self.url:
self.likes = True
self.user = self.url.rsplit("/", 2)[1]
if self.user == "you":
print "I dunno who the user you is..."
else:
#setting self.userid to the user's id from the url
res = urlopen(resolveUrl + "https://soundcloud.com/" + self.user + "&client_id=" + YOUR_CLIENT_ID)
self.userid = json.load(res)["id"]
res.close()
#if its a playlist
elif "sets" in self.url:
self.set = True
#if the 2nd to last item in the split list is soundcloud.com
#assume it is a user
elif (urlsplitted[-2] == "soundcloud.com"):
self.user = True
#else, it is a song
else: self.song = True
def dlfile(self, url,track, folder):
title = track["title"]
description = track["description"]
art_url = track["artwork_url"]
#to get 500x500 art insteasd of 100x100
if art_url is not None:
art_url = art_url.replace("large", "t500x500")
has_art = True
else:
has_art = False
#title = re.sub(r"[\\\/:\*\?""'<>|]", "_", title)
title = re.sub(r"(<|>|:|/|\\|\||\?|\*|\"|\.)", " ", title)
artist = track["user"]["username"]
#removing special char's from file name
#filetitle = re.sub('[^A-Za-z0-9]+', '', title)
file = folder+"/"+title+".mp3"
#convert from utf-8 to unicode, instead of default ascii
#file.decode('utf-8')
try:
f = urlopen(url)
#http://stackoverflow.com/questions/862173/how-to-download-a-file-using-python-in-a-smarter-way
#if track provides download link that redirects and isnt mp3
#actually it downloads them with correct extension but some arent working o_O -->maybe cuz i cancelled in the middle?
#mnek - wrote a song about u has no stream url lol, it has streamable = false??
if f.info().has_key('Content-Disposition'):
# If the response has Content-Disposition, we take file name from it
file = f.info()['Content-Disposition'].split('filename=')[1]
if file[0] == '"' or file[0] == "'":
file = folder+"/"+file[1:-1]
#if file already exsists, dont download it!
if os.path.isfile(file):
return
#while loop just in case to catch file-name errors if they slip by.
while True:
try:
updateString = "Downloading... " + artist + " - " + title
self.dialog.statusUpdate.emit(updateString)
#printing some titles fux up some ascii encoding
# print updateString
# Open our local file for writing
with open(file, "wb") as local_file:
local_file.write(f.read())
try:
mp3 = MP3(file, ID3=ID3)
except HeaderNotFoundError:
print "not an mp3, but thats ok cuz it has data??????"
return
try:
mp3.add_tags(ID3=ID3)
except error:
print("has tags")
#good thred: http://stackoverflow.com/questions/14369366/assign-album-artwork-with-mutagen-mac-vs-pc
if has_art:
art = urlopen(art_url)
mp3.tags.add( APIC( encoding=3, mime='image/jpeg', type=2, desc=u'Cover', data=art.read() ) )
art.close()
mp3['TIT2'] = TIT2(encoding=3, text=title) #title
mp3['TPE1'] = TPE1(encoding=3, text=artist) #artist
mp3['USLT'] = USLT(encoding=3, desc=u'description', text=description) #lyrics, putting SC description in here
mp3.save(v2_version=3, v1=2)
except IOError, e:
#if get this error, means invalid file name
#LAST RESORT..
#remove everything except letters and numbers.
print 'got a damn io error'
print e
title = re.sub('[^a-zA-Z0-9\n\.]', "", title)
file = folder+"/"+title+".mp3"
continue
break
#handle errors
except HTTPError, e:
track["error"] = "HTTP Error:" + str(e.code) + " " + url
self.errors.append(track)
except URLError, e:
track["error"] = "URL Error:" + str(e.reason) + " " + url
self.errors.append(track)
def download(self, dialog):
#if playlist, gives you info with playlist info, "track_count" and array "tracks" with all tracks
#if likes, gives only array with 50 likes
#if track, gives track lol
res = urlopen(resolveUrl + self.url + "&client_id=" + YOUR_CLIENT_ID)
resolved = json.load(res)
res.close()
#setting dialog box as class property
self.dialog = dialog
if self.likes:
likes = []
offset = 0
offset_offset = 50
is_done = False
while not is_done:
res = urlopen(usersUrl + str(self.userid) + "/favorites.json?client_id="+YOUR_CLIENT_ID+"&limit=50&offset="+str(offset))
arr = json.load(res)
if arr == []:
offset_offset /= 2
if offset_offset == 0:
is_done = True
offset -= offset_offset
else:
likes.extend(arr)
res.close()
offset += offset_offset
#downloading likes
#make new dir called "likes" in currentdir
folder = self.path + "/" + self.user + "-" + "likes"
try:
os.mkdir(folder)
except OSError, e:
print "alrdy haz dir"
#must find way to do this async-ly, HELLA slow. grequests?
for index, like in enumerate(likes):
#if downloadable, get the higher quality, artist provided dl
if like["downloadable"]:
self.dlfile(like["download_url"] + "?client_id=" + YOUR_CLIENT_ID, like, folder)
elif "stream_url" not in like:
like["error"] = "doesn't have a stream url"
self.errors.append(like)
#else, dl streaming file @ 128kbps
else:
self.dlfile(like["stream_url"] + "?client_id=" + YOUR_CLIENT_ID, like, folder)
return True
#if playlist
elif self.set:
set_tracks = resolved["tracks"]
#remove illegal characters
set_title = re.sub(r"(<|>|:|/|\\|\||\?|\*|\"|\.)", "", resolved["title"])
#make new dir
if re.search(r"(<|>|:|/|\\|\||\?|\*|\"|\.)", resolved["user"]["username"]) == None:
artist_title = resolved["user"]["username"]
else: artist_title = resolved["user"]["permalink"]
folder = self.path + "/" + artist_title + "-" + set_title
try:
os.mkdir(folder)
except OSError, e:
print e
print "alrdy haz dir"
for index, track in enumerate(set_tracks):
#if downloadable, get the higher quality, artist provided dl
if track["downloadable"]:
self.dlfile(track["download_url"] + "?client_id=" + YOUR_CLIENT_ID, track, folder)
elif "stream_url" not in track:
track["error"] = "doesn't have a stream url"
self.errors.append(track)
#else, dl streaming file @ 128kbps
else:
self.dlfile(track["stream_url"] + "?client_id=" + YOUR_CLIENT_ID,track, folder)
return True
#if artist acc
elif self.user:
print 'im an artist'
if resolved["kind"] == "user":
userid = resolved["id"]
artistRes = urlopen(usersUrl + str(userid) +"/tracks" + "?client_id=" + YOUR_CLIENT_ID)
artistData = json.load(artistRes)
artistRes.close()
#list of artists public uploaded tracks
artists_tracks = artistData
if re.search(r"(<|>|:|/|\\|\||\?|\*|\"|\.)", str(resolved["username"])) == None:
artist_title = resolved["username"]
else: artist_title = resolved["permalink"]
#make new dir
folder = self.path + "/" + artist_title + "-" + "uploads"
try:
os.mkdir(folder)
except OSError, e:
print e
print "alrdy haz dir"
for index, track in enumerate(artists_tracks):
#if downloadable, get the higher quality, artist provided dl
if track["downloadable"]:
self.dlfile(track["download_url"] + "?client_id=" + YOUR_CLIENT_ID, track, folder)
elif "stream_url" not in track:
track["error"] = "doesn't have a stream url"
self.errors.append(track)
#else, dl streaming file @ 128kbps
else:
self.dlfile(track["stream_url"] + "?client_id=" + YOUR_CLIENT_ID,track, folder)
return True
else: return False
#if single track
elif self.song:
print 'hi'
track = resolved
#make new dir
folder = self.path + "/" + "soundcloud-downloads"
try:
os.mkdir(folder)
except OSError, e:
print "alrdy haz dir"
#if downloadable, get the higher quality, artist provided dl
if track["downloadable"]:
self.dlfile(track["download_url"] + "?client_id=" + YOUR_CLIENT_ID, track, folder)
#else, dl streaming file @ 128kbps
else:
self.dlfile(track["stream_url"] + "?client_id=" + YOUR_CLIENT_ID, track, folder )
return True