-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotiLib.py
223 lines (171 loc) · 5.75 KB
/
spotiLib.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
import sys, os, time, shutil, spotipy, time, datetime, logging, io
from urllib.request import urlopen
from spotipy.oauth2 import SpotifyClientCredentials
clientID = ''
clientSecret = ''
IGNORE_ARTWORK = False
def setClientData(ID, secret):
global clientID
global clientSecret
clientID = ID
clientSecret = secret
def disableArtworkFetch():
global IGNORE_ARTWORK
IGNORE_ARTWORK = True
def enableArtworkFetch():
global IGNORE_ARTWORK
IGNORE_ARTWORK = False
def getArtwork(artworkURL):
global IGNORE_ARTWORK
if(not IGNORE_ARTWORK):
artworkData = urlopen(artworkURL).read()
return artworkData
else:
return ''
def newTrack():
return {
'uri': None,
'artist': None,
'album': None,
'albumArtist': None,
'trackTitle': None,
'trackNumber': None,
'diskNumber': None,
'year': None,
'artwork': None,
'duration': None,
'filenaming': None,
'compilation': None,
}
def parseSpotifyData(rawData, uriType):
trackList = []
if(uriType == 'track'):
track = newTrack()
track['uri'] = rawData['uri']
track['artist'] = rawData['artists'][0]['name']
track['album'] = rawData['album']['name']
track['albumArtist'] = rawData['album']['artists'][0]['name']
track['trackTitle'] = rawData['name']
track['trackNumber'] = rawData['track_number']
track['diskNumber'] = rawData['disc_number']
track['year'] = rawData['album']['release_date'][:4]
track['artwork'] = getArtwork(rawData['album']['images'][0]['url'])
track['duration'] = str(int(rawData['duration_ms']) // 1000)
trackList.append(track)
elif(uriType == 'playlist'):
for item in rawData:
rawTrack = item['track']
track = newTrack()
track['uri'] = rawTrack['uri']
track['artist'] = rawTrack['artists'][0]['name']
track['album'] = rawTrack['album']['name']
track['albumArtist'] = rawTrack['album']['artists'][0]['name']
track['trackTitle'] = rawTrack['name']
track['trackNumber'] = rawTrack['track_number']
track['diskNumber'] = rawTrack['disc_number']
track['year'] = rawTrack['album']['release_date'][:4]
track['artwork'] = getArtwork(rawTrack['album']['images'][0]['url'])
track['duration'] = str(int(rawTrack['duration_ms']) // 1000)
trackList.append(track)
elif(uriType == 'album'):
artwork = getArtwork(rawData['images'][0]['url'])
for rawTrack in rawData['tracks']['items']:
track = newTrack()
track['uri'] = rawTrack['uri']
track['artist'] = rawTrack['artists'][0]['name']
track['album'] = rawData['name']
track['albumArtist'] = rawData['artists'][0]['name']
track['trackTitle'] = rawTrack['name']
track['trackNumber'] = rawTrack['track_number']
track['diskNumber'] = rawTrack['disc_number']
track['year'] = rawData['release_date'][:4]
track['artwork'] = artwork
track['duration'] = str(int(rawTrack['duration_ms']) // 1000)
trackList.append(track)
return trackList
def processSpotifyData(rawData, uriType, overrides = None):
trackList = parseSpotifyData(rawData, uriType)
if(overrides != None):
for track in trackList:
for override in overrides:
if overrides[override] != None:
track[override] = overrides[override]
return trackList
def getPlaylistTracks(spotify,uri):
results = spotify.playlist_tracks(uri)
tracks = results['items']
while results['next']:
results = spotify.next(results)
tracks.extend(results['items'])
return tracks
def getSpotifyRaw(uri):
global clientID
global clientSecret
# print("clientSecret: " + clientSecret)
spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(clientID, clientSecret))
#grab the raw data from spotify
if(':playlist:' in uri):
# print(getPlaylistTracks(uri))
return getPlaylistTracks(spotify, uri)
# return spotify.playlist_tracks(uri)['items']
elif(':album:' in uri):
# return spotify.album_tracks(uri)['items']
return spotify.album(uri)
elif(':track:' in uri):
return spotify.track(uri)
'''
parses an ID (track, album, etc) from a given spotify "share" url
params:
url: str - raw spotify url
start: str - portion of the url that can be used as a marker for where to find the
length: int - expected size of the ID (seems to always be 22 but leaving this param because why not)
returns: str - parsed spotify ID string
'''
def getID(url, start, length):
startIndex = url.find(start) + len(start)
endIndex = startIndex + length
return url[startIndex : endIndex]
def generateURN(urlStr, urlType):
if urlType == 'playlist':
return 'spotify:playlist:' + getID(urlStr, '/playlist/', 22)
elif urlType == 'album':
return 'spotify:album:' + getID(urlStr, '/album/', 22)
elif urlType == 'track':
return 'spotify:track:' + getID(urlStr, '/track/', 22)
def getURNType(urlStr):
if 'playlist' in urlStr:
return 'playlist'
elif 'album' in urlStr:
return 'album'
elif 'track' in urlStr:
return 'track'
def processResource(resourceStr):
uriType = getURNType(resourceStr)
#parse "share" url vs uri
if('open.spotify.com' in resourceStr):
uri = generateURN(resourceStr, uriType)
return uri, uriType
return resourceStr, uriType
def printTrack(trackData):
temp = trackData.copy()
temp['artwork'] = "THERE IS SOME ART"
for item in temp:
print(item + ': ' + str(temp[item]))
# pprint(temp)
def calculateDuration(trackList):
duration = 0
for track in trackList:
duration += int(track['duration'])
return duration
def printETA(duration):
#convert to seconds
print("Duration: " + str(datetime.timedelta(seconds = duration)))
t = datetime.datetime.now()
t += datetime.timedelta(seconds=duration)
print("ETA: " + t.strftime("%H:%M:%S"))
def supress_stdout():
'''Supresses print statements until enable_stdOut() is called'''
sys.stdout = open(os.devnull, 'w')
def enable_stdOut():
'''Restores stdout to the system default. (To be called after supress_stdout)'''
sys.stdout = sys.__stdout__