/
spotifyScreenScraper.py
282 lines (258 loc) · 8.13 KB
/
spotifyScreenScraper.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
import sys
import spotipy
import spotipy.util as util
import pylyrics3.pylyrics3 as lyrics
import copy
import authorizeSpotify as authorize
from Artist import Artist
from Album import Album
from Song import Song
import urllib.request
import os
import json
import lastfm
sp = None
token = None
hit = 0
miss = 0
def getAuthorization():
global sp
global token
if sp is None or authorize.is_token_expired():
token = authorize.get_access_token(token,client_id,client_secret)
sp = spotipy.Spotify(auth=token['access_token'])
def getLyrics(artist,songName,albumName):
global hit
global miss
l = None
for ch in ['(',')','\'','&']:
try:
if(ch in songName and ch is '('):
firstIndex = songName.index('(')
secIndex = songName.index(')')
if(firstIndex != -1 and secIndex != -1):
songName = songName.replace(
songName[firstIndex:secIndex+1],"").strip()
#if(ch in artist and ch is '('):
# firstIndex = artist.index('(')
# secIndex = artist.index(')')
# if(firstIndex != -1 and secIndex!=-1):
# artist = artist.replace(
# artist[firstIndex:secIndex+1],"").strip()
if(ch in songName and ch is '\''):
songName = songName.replace('\'','%27')
if(ch in songName and ch is '&'):
songName = songName.replace('&',' %26 ')
if(ch in artist and ch is '&'):
artist = artist.replace('&',' %26 ')
if(ch in artist and ch is '\''):
artist = artist.replace('\'','%27')
except Exception as e:
print("Error: ",e)
print("-----------------------------------")
try:
l = lyrics.get_song_lyrics(artist,songName)
if(l is not None):
print("Successfully downloaded-> ","Artist: ",artist,", Album: ",albumName," , Song: ",songName)
hit = hit + 1
return l
except Exception as e:
print("Error: ",e)
if(l is None):
if('-' in songName):
index = songName.index('-')
songName = songName.replace(songName[index:len(songName)],"")
try:
l = lyrics.get_song_lyrics(artist,songName)
if(l is not None):
print("\tRecovered downloaded-> ","Artist: ",artist,", Album: ",albumName,", Song: ",songName)
hit = hit + 1
return l
except Exception as e:
print("Error: ",e)
if(l is None):
if('feat' in songName):
try:
index = songName.index('feat')
songName = songName.replace(songName[index:len(songName)],"")
l = lyrics.get_song_lyrics(artist,songName)
if(l is not None):
print("\tRecovered downloaded-> ","Artist: ",artist,", Album: ",albumName,", Song: ",songName)
hit = hit + 1
return l
except Exception as e:
print("Error: ",e)
if(l is None):
print("Fail: ","Artist: ",artist,", Album: ",albumName,", Song: ",songName)
miss = miss + 1
return ""
def getSongInfo(token_info,artistName,albumName):
duration_ms = token_info['duration_ms']
name = token_info['name']
explicit = token_info['explicit']
artistList = []
lyrics = getLyrics(artistName,name,albumName)
for x in token_info['artists']:
artistList.append(x['name'])
song = Song()
song.setName(name)
song.setDuration(duration_ms)
song.setExplicit(explicit)
song.setArtists(artistList)
song.setLyrics(lyrics)
song.setTrackNum(token_info['track_number'])
return song
def getAlbumInfo(token_info,songList):
name = token_info['name']
artistList = []
for x in token_info['artists']:
artistList.append(x['name'])
imageList = []
for x in token_info['images']:
imageList.append(x['url'])
album = Album()
if(len(imageList)>=1):
album.setImageLarge(imageList[0])
if(len(imageList)>=2):
album.setImageSmall(imageList[1])
album.setName(name)
album.setArtists(artistList)
album.setTracks(songList)
return album
def getArtistInfo(token_info,albumList):
name = token_info['name']
genres = []
for x in token_info['genres']:
genres.append(x)
followers = token_info['followers']['total']
imageList = []
for x in token_info['images']:
imageList.append(x['url'])
artist = Artist()
if(len(imageList)>=1):
artist.setImageLarge(imageList[0])
if(len(imageList)>=2):
artist.setImageMed(imageList[1])
if(len(imageList)>=3):
artist.setImageSmall(imageList[2])
artist.setName(name)
artist.setFollowers(followers)
artist.setGenres(genres)
artist.setAlbumList(albumList)
try:
artist.setBio(lastfm.get_artist_bio(name))
except Exception:
print("Artist Biography content could not be retrieved")
return artist
def getArtistImages(artist,dir):
artistName = artist.getName()
largeImageFile = dir + '\\' + artistName+'Large.png'
medImageFile = dir + '\\' +artistName+ 'Med.png'
smallImageFile = dir + '\\' +artistName+ 'Small.png'
urllib.request.urlretrieve(artist.getImageLarge(),largeImageFile)
urllib.request.urlretrieve(artist.getImageMed(),medImageFile)
urllib.request.urlretrieve(artist.getImageSmall(),smallImageFile)
def getAlbumImages(albumJson,dir):
album = albumJson
albumName = album['setName'].replace("/","")
albumName = albumName.replace('\\','')
albumName = albumName.replace(" ","")
albumName = albumName.replace("\"","")
largeImageFile = dir + '\\' + albumName+'Large.png'
smallImageFile = dir + '\\' +albumName+ 'Small.png'
urllib.request.urlretrieve(album['imageSmall'],smallImageFile)
urllib.request.urlretrieve(album['imageLarge'],largeImageFile)
def getImages(artist,albumList,dir):
try:
getArtistImages(artist,dir)
except (Exception) as e:
print("Artist Image Failed to Download: ",e)
for x in albumList:
try:
getAlbumImages(x,dir)
except (Exception) as e:
print("Album Image Failed to Download: ",e)
client_id='4c712d6bc65d44f9b2dcf24694754ecc'
client_secret='aed502ac81c84679addf435af1a77916'
redirect_uri='https://example.com/callback/'
scope = 'user-library-read'
username = 'jhurtle'
#token = util.prompt_for_user_token(username, scope,client_id,client_secret,redirect_uri)
def main():
global sp
global token
global hit
global miss
songSet = set()
albumSet = set()
genreSet = set()
artistList = []
cwd = os.getcwd()
artistDirectory = cwd +'\Artists'
if not os.path.exists(artistDirectory):
os.makedirs(artistDirectory)
print("Artist Directory Created")
with open('TopArtists.txt','r') as f:
for line in f:
try:
getAuthorization()
results = sp.search(q='artist:'+line.strip(), type='artist')
artistList = results['artists']['items']
if artistList == []:
print("No Artists Found By The Name of ",line.strip())
continue
artist = artistList[0]
artistName = artist['name']
results = sp.artist_albums(artist['id'],album_type='album')
album = []
album.extend(results['items'])
for x in artist['genres']:
if x not in genreSet:
genreSet.add(x)
while results['next']:
results = sp.next(results)
album.extend(results['items'])
albumList = []
for x in album:
albumName = x['name']
album = None
songList = []
if albumName not in albumSet:
albumSet.add(albumName)
getAuthorization()
songs = sp.album(x['id'])['tracks']['items']
for y in songs:
song = None
songName = y['name']
if songName not in songSet:
songSet.add(songName)
song = getSongInfo(y,artistName,albumName)
songList.append(json.loads(json.dumps(vars(song))))
album = getAlbumInfo(x,songList)
albumList.append(json.loads(json.dumps(vars(album))))
artist_info = getArtistInfo(artist,albumList)
artistList.append(artist_info)
formatArtistName = artistName.replace(" ","")
formatArtistName = formatArtistName.replace("\\","")
formatArtistName = formatArtistName.replace("\"","")
formatArtistName = formatArtistName.replace("\'","")
newDir = artistDirectory + '\\' + formatArtistName
if not os.path.exists(newDir):
os.makedirs(newDir)
print(artistName + " Directory Created")
fileName = newDir +"\\"+formatArtistName + ".txt"
with open(fileName, 'w') as outFile:
json.dump(vars(artist_info),outFile)
getImages(artist_info,albumList,newDir)
except Exception as e:
print("Failure in Getting Artist Files: ",e)
total = hit + miss
accuracy = (hit/float(total))*100
print("Total: ",total)
print("Accuracy : %.2f" % accuracy,"%")
print("Hit: ",hit)
print("Miss: ",miss)
with open("Genres.txt",'w') as genreFile:
for x in genreSet:
genreFile.write(x)
main()