From a5780387e108a66e7aa9fd7e60c5598f3c7a887b Mon Sep 17 00:00:00 2001 From: Chris Shyi Date: Sun, 20 May 2018 16:31:51 -0400 Subject: [PATCH] Made minor changes to variable names --- spotifyvis/views.py | 86 ++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/spotifyvis/views.py b/spotifyvis/views.py index 5aa780c..01edd75 100644 --- a/spotifyvis/views.py +++ b/spotifyvis/views.py @@ -143,6 +143,45 @@ def user_data(request): # }}} 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): """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 Returns: - (updated_mean, std_dev) + (new_mean, new_std_dev) """ # This is an implementationof Welford's method # 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, } 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'] - 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 -# 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 {{{ # def increase_nested_key(top_key, nested_key, amount=1):