|
@ -143,6 +143,45 @@ def user_data(request): |
|
|
|
|
|
|
|
|
# }}} user_data # |
|
|
# }}} user_data # |
|
|
|
|
|
|
|
|
|
|
|
# parse_library {{{ # |
|
|
|
|
|
|
|
|
|
|
|
def parse_library(headers, tracks): |
|
|
|
|
|
"""Scans user's library for certain number of tracks to update library_stats with. |
|
|
|
|
|
|
|
|
|
|
|
:headers: For API call. |
|
|
|
|
|
:tracks: Number of tracks to get from user's library. |
|
|
|
|
|
:returns: None |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
|
|
# TODO: implement importing entire library with 0 as tracks param |
|
|
|
|
|
# number of tracks to get with each call |
|
|
|
|
|
limit = 5 |
|
|
|
|
|
# keeps track of point to get songs from |
|
|
|
|
|
offset = 0 |
|
|
|
|
|
payload = {'limit': str(limit)} |
|
|
|
|
|
for _ in range(0, tracks, limit): |
|
|
|
|
|
payload['offset'] = str(offset) |
|
|
|
|
|
saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks', headers=headers, params=payload).json() |
|
|
|
|
|
num_samples = offset |
|
|
|
|
|
for track_dict in saved_tracks_response['items']: |
|
|
|
|
|
# Track the number of samples for calculating |
|
|
|
|
|
# audio feature averages and standard deviations on the fly |
|
|
|
|
|
num_samples += 1 |
|
|
|
|
|
get_track_info(track_dict['track']) |
|
|
|
|
|
# get_genre(headers, track_dict['track']['album']['id']) |
|
|
|
|
|
audio_features_dict = get_audio_features(track_dict['id'], headers) |
|
|
|
|
|
for feature, feature_data in audio_features_dict.items(): |
|
|
|
|
|
update_audio_feature_stats(feature, feature_data, num_samples) |
|
|
|
|
|
for artist_dict in track_dict['track']['artists']: |
|
|
|
|
|
increase_artist_count(headers, artist_dict['name'], artist_dict['id']) |
|
|
|
|
|
# calculates num_songs with offset + songs retrieved |
|
|
|
|
|
library_stats['num_songs'] = offset + len(saved_tracks_response['items']) |
|
|
|
|
|
offset += limit |
|
|
|
|
|
calculate_genres_from_artists(headers) |
|
|
|
|
|
pprint.pprint(library_stats) |
|
|
|
|
|
|
|
|
|
|
|
# }}} parse_library # |
|
|
|
|
|
|
|
|
def get_audio_features(track_id, headers): |
|
|
def get_audio_features(track_id, headers): |
|
|
"""Returns the audio features of a soundtrack |
|
|
"""Returns the audio features of a soundtrack |
|
|
|
|
|
|
|
@ -178,7 +217,7 @@ def update_std_dev(cur_mean, cur_std_dev, new_data_point, sample_size): |
|
|
sample_size: sample size including the new data point |
|
|
sample_size: sample size including the new data point |
|
|
|
|
|
|
|
|
Returns: |
|
|
Returns: |
|
|
(updated_mean, std_dev) |
|
|
|
|
|
|
|
|
(new_mean, new_std_dev) |
|
|
""" |
|
|
""" |
|
|
# This is an implementationof Welford's method |
|
|
# This is an implementationof Welford's method |
|
|
# http://jonisalonen.com/2013/deriving-welfords-method-for-computing-variance/ |
|
|
# http://jonisalonen.com/2013/deriving-welfords-method-for-computing-variance/ |
|
@ -209,53 +248,14 @@ def update_audio_feature_stats(feature, new_data_point, sample_size): |
|
|
"std_dev": 0, |
|
|
"std_dev": 0, |
|
|
} |
|
|
} |
|
|
else: |
|
|
else: |
|
|
current_mean = library_stats['audio_features'][feature]['average'] |
|
|
|
|
|
|
|
|
cur_mean = library_stats['audio_features'][feature]['average'] |
|
|
cur_std_dev = library_stats['audio_features'][feature]['std_dev'] |
|
|
cur_std_dev = library_stats['audio_features'][feature]['std_dev'] |
|
|
updated_mean, new_std_dev = update_std_dev(current_mean, cur_std_dev, new_data_point, sample_size) |
|
|
|
|
|
|
|
|
new_mean, new_std_dev = update_std_dev(cur_mean, cur_std_dev, new_data_point, sample_size) |
|
|
|
|
|
|
|
|
library_stats['audio_features'][feature]['average'] = updated_mean |
|
|
|
|
|
|
|
|
library_stats['audio_features'][feature]['average'] = new_mean |
|
|
library_stats['audio_features'][feature]['std_dev'] = new_std_dev |
|
|
library_stats['audio_features'][feature]['std_dev'] = new_std_dev |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# parse_library {{{ # |
|
|
|
|
|
|
|
|
|
|
|
def parse_library(headers, tracks): |
|
|
|
|
|
"""Scans user's library for certain number of tracks to update library_stats with. |
|
|
|
|
|
|
|
|
|
|
|
:headers: For API call. |
|
|
|
|
|
:tracks: Number of tracks to get from user's library. |
|
|
|
|
|
:returns: None |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
|
|
# TODO: implement importing entire library with 0 as tracks param |
|
|
|
|
|
# number of tracks to get with each call |
|
|
|
|
|
limit = 5 |
|
|
|
|
|
# keeps track of point to get songs from |
|
|
|
|
|
offset = 0 |
|
|
|
|
|
payload = {'limit': str(limit)} |
|
|
|
|
|
for _ in range(0, tracks, limit): |
|
|
|
|
|
payload['offset'] = str(offset) |
|
|
|
|
|
saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks', headers=headers, params=payload).json() |
|
|
|
|
|
num_samples = offset |
|
|
|
|
|
for track_dict in saved_tracks_response['items']: |
|
|
|
|
|
# Track the number of samples for calculating |
|
|
|
|
|
# audio feature averages and standard deviations on the fly |
|
|
|
|
|
num_samples += 1 |
|
|
|
|
|
get_track_info(track_dict['track']) |
|
|
|
|
|
# get_genre(headers, track_dict['track']['album']['id']) |
|
|
|
|
|
audio_features_dict = get_audio_features(track_dict['id'], headers) |
|
|
|
|
|
for feature, feature_data in audio_features_dict.items(): |
|
|
|
|
|
update_audio_feature_stats(feature, feature_data, num_samples) |
|
|
|
|
|
for artist_dict in track_dict['track']['artists']: |
|
|
|
|
|
increase_artist_count(headers, artist_dict['name'], artist_dict['id']) |
|
|
|
|
|
# calculates num_songs with offset + songs retrieved |
|
|
|
|
|
library_stats['num_songs'] = offset + len(saved_tracks_response['items']) |
|
|
|
|
|
offset += limit |
|
|
|
|
|
calculate_genres_from_artists(headers) |
|
|
|
|
|
pprint.pprint(library_stats) |
|
|
|
|
|
|
|
|
|
|
|
# }}} parse_library # |
|
|
|
|
|
|
|
|
|
|
|
# increase_nested_key {{{ # |
|
|
# increase_nested_key {{{ # |
|
|
|
|
|
|
|
|
def increase_nested_key(top_key, nested_key, amount=1): |
|
|
def increase_nested_key(top_key, nested_key, amount=1): |
|
|