From 22ea4728897e71e181c09bccd45397c650aced86 Mon Sep 17 00:00:00 2001 From: Kevin Mok Date: Thu, 28 Jun 2018 14:06:58 -0400 Subject: [PATCH] Store user's total songs Also fixed "lag" to access admin charts page. --- spotifyvis/models.py | 1 + spotifyvis/utils.py | 10 ++++++---- spotifyvis/views.py | 15 ++++++++++----- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/spotifyvis/models.py b/spotifyvis/models.py index 09a9ec7..9ac0743 100644 --- a/spotifyvis/models.py +++ b/spotifyvis/models.py @@ -45,6 +45,7 @@ class User(models.Model): user_id = models.CharField(primary_key=True, max_length=MAX_ID) # the user's Spotify ID user_secret = models.CharField(max_length=50, default='') + total_songs = models.PositiveIntegerField() def __str__(self): return self.user_id diff --git a/spotifyvis/utils.py b/spotifyvis/utils.py index 067d631..cf62e5e 100644 --- a/spotifyvis/utils.py +++ b/spotifyvis/utils.py @@ -191,9 +191,10 @@ def get_audio_features(headers, track_objs): :returns: None """ track_ids = str.join(",", [track_obj.track_id for track_obj in track_objs]) - params = {'ids': track_ids} features_response = requests.get("https://api.spotify.com/v1/audio-features", - headers=headers,params=params).json()['audio_features'] + headers=headers, + params={'ids': track_ids} + ).json()['audio_features'] # pprint.pprint(features_response) useless_keys = [ "key", "mode", "type", "liveness", "id", "uri", "track_href", "analysis_url", "time_signature", ] @@ -248,9 +249,10 @@ def add_artist_genres(headers, artist_objs): """ artist_ids = str.join(",", [artist_obj.artist_id for artist_obj in artist_objs]) - params = {'ids': artist_ids} artists_response = requests.get('https://api.spotify.com/v1/artists/', - headers=headers, params=params).json()['artists'] + headers=headers, + params={'ids': artist_ids}, + ).json()['artists'] for i in range(len(artist_objs)): if len(artists_response[i]['genres']) == 0: process_artist_genre("undefined", artist_objs[i]) diff --git a/spotifyvis/views.py b/spotifyvis/views.py index ed25b54..662b6d8 100644 --- a/spotifyvis/views.py +++ b/spotifyvis/views.py @@ -21,8 +21,8 @@ from .models import User, Track, AudioFeatures, Artist # global vars {{{ # TIME_FORMAT = '%Y-%m-%d-%H-%M-%S' -# TRACKS_TO_QUERY = 200 -TRACKS_TO_QUERY = 0 +TRACKS_TO_QUERY = 200 +# TRACKS_TO_QUERY = 0 # }}} global vars # @@ -157,13 +157,19 @@ def user_data(request): # create user obj {{{ # + total_songs = requests.get('https://api.spotify.com/v1/me/tracks', + headers=headers, + params={'limit': '1'}).json()['total'] try: user = User.objects.get(user_id=user_data_response['id']) except User.DoesNotExist: # Python docs recommends 32 bytes of randomness against brute force attacks - user = User(user_id=user_data_response['id'], user_secret=secrets.token_urlsafe(32)) + user = User.objects.create( + user_id=user_data_response['id'], + user_secret=secrets.token_urlsafe(32), + total_songs=total_songs, + ) request.session['user_secret'] = user.user_secret - user.save() # }}} create user obj # @@ -189,7 +195,6 @@ def admin_graphs(request): 'user_id': user_id, 'user_secret': user_obj.user_secret, } - update_track_genres(user_obj) return render(request, 'spotifyvis/logged_in.html', context) # }}} admin_graphs #