-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_data.py
462 lines (388 loc) · 17.5 KB
/
export_data.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import requests, time
from tqdm import tqdm
import pandas as pd
import numpy as np
import spotipy
import re
import yaml
import logging
from spotipy import oauth2
from spotipy import SpotifyException
cid = secret = lfkey = logPath = None # vars for config.yaml
logger = None # global logger
# todo: refactor variable names to align with PEP
def clean_query(q):
def collapse_brackets(text, brackets="()[]"):
count = [0] * (len(brackets) // 2) # count open/close brackets
saved_chars = []
for character in text:
for i, b in enumerate(brackets):
if character == b: # found bracket
kind, is_close = divmod(i, 2)
count[kind] += (-1) ** is_close # `+1`: open, `-1`: close
if count[kind] < 0: # unbalanced bracket
count[kind] = 0 # keep it
else: # found bracket to remove
break
else: # character is not a [balanced] bracket
if not any(count): # outside brackets
saved_chars.append(character)
return ''.join(saved_chars)
s = collapse_brackets(q)
s = re.sub("'", '', s)
return s
def load_cfg(yaml_filepath):
"""
Load config vars from yaml
:param yaml_filepath: path to config.yaml
"""
global cid, secret, lfkey, logPath
with open(yaml_filepath, 'r') as stream:
config = yaml.safe_load(stream)
cid = config['sp_cid']
secret = config['sp_secret']
lfkey = config['lf_key']
logPath = config['log_path']
def init_logger():
'''
Initialize logger globally
'''
global logPath, logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(logPath, 'w', 'utf-8')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
def get_spotify_token():
'''
Get OAuth token from spotify.
:return token_info dict
:return sp_oauth object
'''
sp_oauth = oauth2.SpotifyOAuth(client_id=cid, client_secret=secret,
redirect_uri='https://example.com/callback/')
token_info = sp_oauth.get_cached_token()
if not token_info:
auth_url = sp_oauth.get_authorize_url()
print(auth_url)
response = input('Paste the above link into your browser, then paste the redirect url here: ')
code = sp_oauth.parse_response_code(response)
token_info = sp_oauth.get_access_token(code)
return token_info, sp_oauth
def token_refresh(token_info, sp_oauth):
'''
Used to refresh OAuth token if token expired
:param token_info dict
:param sp_oauth object
'''
global sp
if sp_oauth._is_token_expired(token_info):
token_info_ref = sp_oauth.refresh_access_token(token_info['refresh_token'])
token_ref = token_info_ref['access_token']
sp = spotipy.Spotify(auth=token_ref)
logger.info("________token refreshed________")
def authenticate():
'''
authenticate with spotify
'''
global token_info, sp, sp_oauth
token_info, sp_oauth = get_spotify_token() # authenticate with spotify
sp = spotipy.Spotify(auth=token_info['access_token']) # create spotify object globally
#thanks to Geoff Boeing : https://github.com/gboeing/data-visualization/blob/master/lastfm-listening-history/lastfm_downloader.ipynb
def get_scrobbles(username, method='recenttracks', timezone='Asia/Kolkata', limit=200, page=1, pages=0):
'''
Retrieves scrobbles from lastfm for a user
:param method: api method
:param username: last.fm username for retrieval
:param timezone: timezone of the user (must correspond with the timezone in user's settings)
:param limit: api lets you retrieve up to 200 records per call
:param page: page of results to start retrieving at
:param pages: how many pages of results to retrieve. if 0, get as many as api can return.
:return dataframe with lastfm scrobbles
'''
# initialize url and lists to contain response fields
print("\nFetching data from last.fm for user " + username)
url = 'https://ws.audioscrobbler.com/2.0/?method=user.get{}&user={}&api_key={}&limit={}&page={}&format=json'
responses = []
artist_names = []
artist_mbids = []
album_names = []
album_mbids = []
track_names = []
track_mbids = []
timestamps = []
# read from loadCFG()
key = lfkey
# make first request, just to get the total number of pages
request_url = url.format(method, username, key, limit, page)
response = requests.get(request_url).json()
# error handling
if 'error' in response:
print("Error code : " + str(response['error']))
logging.critical("Error code : " + str(response['error']))
print("Error message : " + response['message'])
logging.critical("Error message : " + response['message'])
return None
total_pages = int(response[method]['@attr']['totalPages'])
total_scrobbles = int(response[method]['@attr']['total'])
if pages > 0:
total_pages = min([total_pages, pages])
print('\n{} total tracks scrobbled by the user'.format(total_scrobbles))
print('\n{} total pages to retrieve'.format(total_pages))
# request each page of data one at a time
for page in tqdm(range(1, int(total_pages) + 1, 1)):
time.sleep(0.20)
request_url = url.format(method, username, key, limit, page)
responses.append(requests.get(request_url))
# parse the fields out of each scrobble in each page (aka response) of scrobbles
for response in responses:
scrobbles = response.json()
for scrobble in scrobbles[method]['track']:
# only retain completed scrobbles (aka, with timestamp and not 'now playing')
if 'date' in scrobble.keys():
artist_names.append(scrobble['artist']['#text'])
artist_mbids.append(scrobble['artist']['mbid'])
album_names.append(scrobble['album']['#text'])
album_mbids.append(scrobble['album']['mbid'])
track_names.append(scrobble['name'])
track_mbids.append(scrobble['mbid'])
timestamps.append(scrobble['date']['uts'])
# create and populate a dataframe to contain the data
df = pd.DataFrame()
df['timestamp'] = timestamps
df['datetime'] = pd.to_datetime(df['timestamp'].astype(int), unit='s')
df['datetime'] = df['datetime'].dt.tz_localize('UTC').dt.tz_convert(timezone)
df['artist_name'] = artist_names
df['artist_mbid'] = artist_mbids
df['album_name'] = album_names
df['album_mbid'] = album_mbids
df['track_name'] = track_names
df['track_mbid'] = track_mbids
return df
def map_to_spotify(scrobblesDF):
"""
Maps track names to spotifyID and adds track length,popularity,genre to dataframe.
:param scrobblesDF : lastfm scrobbles dataframe
:return scrobblesDF : dataframe with spotifyID ,track length,popularity,genre
"""
track_ids = []
length = []
pop = []
genre = []
print("\n\nFetching SpotifyID for tracks")
for index, row in tqdm(scrobblesDF.iterrows(), total=scrobblesDF.shape[0]):
try:
artist = clean_query(row['artist_name'])
track = clean_query(row['track_name'])
searchDict = sp.search(q='artist:' + artist + ' track:' + track, type='track', limit=1,
market='US') # api cakk
logging.debug("Mapping spotifyID for " + track)
# logging.debug("Mapping spotifyID for " + str(index) + " out of " + str(len(scrobblesDF.index)-1))
if len(searchDict['tracks']['items']) != 0:
track_ids.append(searchDict['tracks']['items'][0]['id'])
length.append(searchDict['tracks']['items'][0]['duration_ms'])
pop.append(searchDict['tracks']['items'][0]['popularity'])
artist_id = searchDict['tracks']['items'][0]['artists'][0]['id']
artist = sp.artist(artist_id) # get genre from artist
try:
genreA = artist['genres'][0] # gets only the first genre from list of genres (may be inaccurate)
genre.append(genreA)
except IndexError:
genre.append(np.nan)
else:
track_ids.append(np.nan)
length.append(np.nan)
pop.append(np.nan)
genre.append(np.nan)
logging.warning("failed to map " + track)
except SpotifyException:
if sp_oauth._is_token_expired(token_info):
token_refresh(token_info, sp_oauth) # refresh OAuth token
else:
logging.critical("SpotifyException")
scrobblesDF['trackID'] = pd.Series(track_ids)
scrobblesDF['lengthMS'] = pd.Series(length)
scrobblesDF['popularity'] = pd.Series(pop)
scrobblesDF['genre_name'] = pd.Series(genre)
unmapped_cnt = scrobblesDF['trackID'].isna().sum()
print("\ntracks without spotifyID : " + str(unmapped_cnt))
return scrobblesDF
def map_audio_features(scrobblesDF): # todo: [for v2]pass 50 IDs at once in chunks to sp.audio_features to speedup
'''
Adds track features to dataframe with SpotifyID.
:param scrobblesDF: dataframe with SpotifyID
:return enriched dataframe with audio features
'''
danceabilitySeries = []
energySeries = []
keySeries = []
loudnessSeries = []
modeSeries = []
speechinessSeries = []
acousticnessSeries = []
instrumentalnessSeries = []
livenessSeries = []
valenceSeries = []
tempoSeries = []
print("\nFetching audio features for tracks")
for index, row in tqdm(scrobblesDF.iterrows(), total=scrobblesDF.shape[0]):
try:
logging.debug("Fetching features for " + str(index) + " out of " + str(len(scrobblesDF.index) - 1))
if row['trackID'] is not np.nan:
search_id = [str(row['trackID'])]
feature = sp.audio_features(search_id) # api call
try:
danceabilitySeries.append(feature[0]["danceability"])
energySeries.append(feature[0]["energy"])
keySeries.append(feature[0]["key"])
loudnessSeries.append(feature[0]["loudness"])
modeSeries.append(feature[0]["mode"])
speechinessSeries.append(feature[0]["speechiness"])
acousticnessSeries.append(feature[0]["acousticness"])
livenessSeries.append(feature[0]["liveness"])
valenceSeries.append(feature[0]["valence"])
tempoSeries.append(feature[0]["tempo"])
instrumentalnessSeries.append(feature[0]["instrumentalness"])
except (TypeError, AttributeError, IndexError):
logging.warning("\nTrack feature fetch failed for " + row['track_name'])
danceabilitySeries.append(np.nan)
energySeries.append(np.nan)
keySeries.append(np.nan)
loudnessSeries.append(np.nan)
modeSeries.append(np.nan)
speechinessSeries.append(np.nan)
acousticnessSeries.append(np.nan)
livenessSeries.append(np.nan)
valenceSeries.append(np.nan)
tempoSeries.append(np.nan)
instrumentalnessSeries.append(np.nan)
else:
logging.warning("\nTrack ID not available for " + row['track_name'])
danceabilitySeries.append(np.nan)
energySeries.append(np.nan)
keySeries.append(np.nan)
loudnessSeries.append(np.nan)
modeSeries.append(np.nan)
speechinessSeries.append(np.nan)
acousticnessSeries.append(np.nan)
livenessSeries.append(np.nan)
valenceSeries.append(np.nan)
tempoSeries.append(np.nan)
instrumentalnessSeries.append(np.nan)
continue
except SpotifyException:
if sp_oauth._is_token_expired(token_info):
token_refresh(token_info, sp_oauth) # refresh OAuth token
else:
logging.critical("SpotifyException")
scrobblesDF['danceability'] = danceabilitySeries
scrobblesDF['energy'] = energySeries
scrobblesDF['key'] = keySeries
scrobblesDF['loudness'] = loudnessSeries
scrobblesDF['mode'] = modeSeries
scrobblesDF['speechiness'] = speechinessSeries
scrobblesDF['acousticness'] = acousticnessSeries
scrobblesDF['liveness'] = livenessSeries
scrobblesDF['instrumentalness'] = instrumentalnessSeries
scrobblesDF['valence'] = valenceSeries
scrobblesDF['tempo'] = tempoSeries
unmapped_cnt = scrobblesDF['trackID'].isna().sum()
print("tracks without audio features : " + str(unmapped_cnt))
return scrobblesDF
def get_playlist(user='billboard.com', playlist_id='6UeSakyzhiEt4NB3UAd6NQ'):
'''
retrives audio features of a playlist (Billboard Hot 100 is the default playlist)
:param user: username of the playlist owner
:param playlist_id: playlist id (found at the end of a playlist url)
:return: a dataframe with audio features of a playlist
'''
trackID = []
track = []
artist = []
artistID = []
genre = []
lengthMS = []
popularity = []
playlist = sp.user_playlist(user=user, playlist_id=playlist_id)
count = playlist['tracks']['total']
print("\n\nFetching playlist")
for i in tqdm(range(count)):
# print('fetching ' + str(i) + ' out of ' + str(count) + ' ' + playlist['tracks']['items'][i]['track']['id'])
trackID.append(playlist['tracks']['items'][i]['track']['id'])
track.append(playlist['tracks']['items'][i]['track']['name'])
lengthMS.append(playlist['tracks']['items'][i]['track']['duration_ms'])
popularity.append(playlist['tracks']['items'][i]['track']['popularity'])
artist.append(playlist['tracks']['items'][i]['track']['artists'][0]['name'])
artistID.append(playlist['tracks']['items'][i]['track']['artists'][0]['id'])
artistOb = sp.artist(artistID[i])
try:
genreA = artistOb['genres'][0]
genre.append(genreA)
except IndexError:
genre.append(None)
playlistDF = pd.DataFrame()
playlistDF['track'] = pd.Series(track)
playlistDF['trackID'] = pd.Series(trackID)
playlistDF['artist'] = pd.Series(artist)
playlistDF['artistID'] = pd.Series(artistID)
playlistDF['genre'] = pd.Series(genre)
playlistDF['lengthMS'] = pd.Series(lengthMS)
playlistDF['popularity'] = pd.Series(popularity)
playlistDF = map_audio_features(playlistDF)
return playlistDF
def generate_dataset(lfusername, timezone='Asia/Kolkata', pages=0):
'''
Gets user's listening history and enriches it with Spotify audio features
:param lfusername: last.fm username
:param timezone: timezone of the user (must correspond with the timezone in user's settings)
:param pages: number of pages to retrieve, use pages = 0 to retrieve full listening history
:return scrobblesDFdict: dictionary with two dataframes ('complete' with timestamps and 'library' with library contents)
'''
scrobblesDF_lastfm = get_scrobbles(username=lfusername, timezone=timezone,
pages=pages) # get all pages form lastfm with pages = 0
scrobblesDF_condensed = scrobblesDF_lastfm[['artist_name', 'track_name']]
scrobblesDF_uniques = scrobblesDF_condensed.groupby(['artist_name', 'track_name']).size().reset_index()
scrobblesDF_uniques.rename(columns={0: 'frequency'}, inplace=True)
scrobblesDF_wTrackID_uniques = map_to_spotify(scrobblesDF_uniques)
scrobblesDF_wFeatures_uniques = map_audio_features(scrobblesDF_wTrackID_uniques)
scrobblesDF_complete = pd.merge(scrobblesDF_lastfm, scrobblesDF_wFeatures_uniques, how='left',
on=['track_name', 'artist_name'])
scrobblesDFdict = dict()
scrobblesDFdict['complete'] = scrobblesDF_complete
scrobblesDFdict['library'] = scrobblesDF_wFeatures_uniques
return scrobblesDFdict
def unmapped_tracks(scrobblesDF):
'''
:param scrobblesDF: dataframe with scrobbled tracks and trackIDs
:return scrobblesDF: dataframe containing tracks with no trackIDs
'''
noTrackID_df = scrobblesDF[scrobblesDF['trackID'].isnull()]
return noTrackID_df
def initialize(cfgPath):
'''
calls functions needed for initialization, handles loading config file,
initializing logger object, initializing Spotipy object.
To be called before calling other functions.
:param cfgPath: filepath for config.yaml
'''
load_cfg(cfgPath)
init_logger()
authenticate()
"""
def main():
start_time = time.time() # get running time for the script
load_cfg('C:\\Users\Madhan\PycharmProjects\lfm4pandas\config.yaml')
init_logger()
authenticate() # authenticate with spotify
scrobblesDFdict = generate_dataset(lfusername='madhan_001', pages=1) # returns a dict of dataframes
dic = scrobblesDFdict['library']
print(unmapped_tracks(dic))
# scrobbles_complete.to_csv("data\LFMscrobbles.tsv", sep='\t') #using tsv as some attributes contain commas
end_time = time.time()
start_time = start_time / 60
end_time = end_time / 60 # show time in minutes
print("Finished in " + str(end_time - start_time) + " mins")
if __name__ == '__main__':
main()
"""