Browse Source

Store user's total songs

Also fixed "lag" to access admin charts page.
master
Kevin Mok 7 years ago
parent
commit
22ea472889
  1. 1
      spotifyvis/models.py
  2. 10
      spotifyvis/utils.py
  3. 15
      spotifyvis/views.py

1
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

10
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])

15
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 #

Loading…
Cancel
Save