|
@ -13,12 +13,12 @@ import json |
|
|
|
|
|
|
|
|
# parse_library {{{ # |
|
|
# parse_library {{{ # |
|
|
|
|
|
|
|
|
def parse_library(headers, tracks, library_stats, user): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_library(headers, tracks, user): |
|
|
"""Scans user's library for certain number of tracks to update library_stats with. |
|
|
"""Scans user's library for certain number of tracks to update library_stats with. |
|
|
|
|
|
|
|
|
:headers: For API call. |
|
|
:headers: For API call. |
|
|
:tracks: Number of tracks to get from user's library. |
|
|
:tracks: Number of tracks to get from user's library. |
|
|
:library_stats: Dictionary containing the data mined from user's library |
|
|
|
|
|
:user: a User object representing the user whose library we are parsing |
|
|
:user: a User object representing the user whose library we are parsing |
|
|
|
|
|
|
|
|
:returns: None |
|
|
:returns: None |
|
@ -30,8 +30,6 @@ def parse_library(headers, tracks, library_stats, user): |
|
|
# keeps track of point to get songs from |
|
|
# keeps track of point to get songs from |
|
|
offset = 0 |
|
|
offset = 0 |
|
|
payload = {'limit': str(limit)} |
|
|
payload = {'limit': str(limit)} |
|
|
# use two separate variables to track, because the average popularity also requires num_samples |
|
|
|
|
|
num_samples = 0 # number of actual track samples |
|
|
|
|
|
|
|
|
|
|
|
# iterate until hit requested num of tracks |
|
|
# iterate until hit requested num of tracks |
|
|
for _ in range(0, tracks, limit): |
|
|
for _ in range(0, tracks, limit): |
|
@ -73,7 +71,6 @@ def parse_library(headers, tracks, library_stats, user): |
|
|
""" |
|
|
""" |
|
|
# calculates num_songs with offset + songs retrieved |
|
|
# calculates num_songs with offset + songs retrieved |
|
|
offset += limit |
|
|
offset += limit |
|
|
# calculate_genres_from_artists(headers, library_stats) |
|
|
|
|
|
# pprint.pprint(library_stats) |
|
|
# pprint.pprint(library_stats) |
|
|
|
|
|
|
|
|
# }}} parse_library # |
|
|
# }}} parse_library # |
|
@ -125,7 +122,6 @@ def save_audio_features(headers, track_id, track): |
|
|
response = requests.get("https://api.spotify.com/v1/audio-features/{}".format(track_id), headers = headers).json() |
|
|
response = requests.get("https://api.spotify.com/v1/audio-features/{}".format(track_id), headers = headers).json() |
|
|
if 'error' in response: |
|
|
if 'error' in response: |
|
|
return {} |
|
|
return {} |
|
|
features_dict = {} |
|
|
|
|
|
|
|
|
|
|
|
# Data that we don't need |
|
|
# Data that we don't need |
|
|
useless_keys = [ |
|
|
useless_keys = [ |
|
@ -135,7 +131,6 @@ def save_audio_features(headers, track_id, track): |
|
|
audio_features_entry.track = track |
|
|
audio_features_entry.track = track |
|
|
for key, val in response.items(): |
|
|
for key, val in response.items(): |
|
|
if key not in useless_keys: |
|
|
if key not in useless_keys: |
|
|
features_dict[key] = val |
|
|
|
|
|
setattr(audio_features_entry, key, val) |
|
|
setattr(audio_features_entry, key, val) |
|
|
audio_features_entry.save() |
|
|
audio_features_entry.save() |
|
|
|
|
|
|
|
|