Store user's total songs
Also fixed "lag" to access admin charts page.
This commit is contained in:
@@ -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_id = models.CharField(primary_key=True, max_length=MAX_ID) # the user's Spotify ID
|
||||||
user_secret = models.CharField(max_length=50, default='')
|
user_secret = models.CharField(max_length=50, default='')
|
||||||
|
total_songs = models.PositiveIntegerField()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.user_id
|
return self.user_id
|
||||||
|
|||||||
@@ -191,9 +191,10 @@ def get_audio_features(headers, track_objs):
|
|||||||
:returns: None
|
:returns: None
|
||||||
"""
|
"""
|
||||||
track_ids = str.join(",", [track_obj.track_id for track_obj in track_objs])
|
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",
|
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)
|
# pprint.pprint(features_response)
|
||||||
|
|
||||||
useless_keys = [ "key", "mode", "type", "liveness", "id", "uri", "track_href", "analysis_url", "time_signature", ]
|
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])
|
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/',
|
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)):
|
for i in range(len(artist_objs)):
|
||||||
if len(artists_response[i]['genres']) == 0:
|
if len(artists_response[i]['genres']) == 0:
|
||||||
process_artist_genre("undefined", artist_objs[i])
|
process_artist_genre("undefined", artist_objs[i])
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ from .models import User, Track, AudioFeatures, Artist
|
|||||||
# global vars {{{ #
|
# global vars {{{ #
|
||||||
|
|
||||||
TIME_FORMAT = '%Y-%m-%d-%H-%M-%S'
|
TIME_FORMAT = '%Y-%m-%d-%H-%M-%S'
|
||||||
# TRACKS_TO_QUERY = 200
|
TRACKS_TO_QUERY = 200
|
||||||
TRACKS_TO_QUERY = 0
|
# TRACKS_TO_QUERY = 0
|
||||||
|
|
||||||
# }}} global vars #
|
# }}} global vars #
|
||||||
|
|
||||||
@@ -157,13 +157,19 @@ def user_data(request):
|
|||||||
|
|
||||||
# create user obj {{{ #
|
# create user obj {{{ #
|
||||||
|
|
||||||
|
total_songs = requests.get('https://api.spotify.com/v1/me/tracks',
|
||||||
|
headers=headers,
|
||||||
|
params={'limit': '1'}).json()['total']
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(user_id=user_data_response['id'])
|
user = User.objects.get(user_id=user_data_response['id'])
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
# Python docs recommends 32 bytes of randomness against brute force attacks
|
# 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
|
request.session['user_secret'] = user.user_secret
|
||||||
user.save()
|
|
||||||
|
|
||||||
# }}} create user obj #
|
# }}} create user obj #
|
||||||
|
|
||||||
@@ -189,7 +195,6 @@ def admin_graphs(request):
|
|||||||
'user_id': user_id,
|
'user_id': user_id,
|
||||||
'user_secret': user_obj.user_secret,
|
'user_secret': user_obj.user_secret,
|
||||||
}
|
}
|
||||||
update_track_genres(user_obj)
|
|
||||||
return render(request, 'spotifyvis/logged_in.html', context)
|
return render(request, 'spotifyvis/logged_in.html', context)
|
||||||
|
|
||||||
# }}} admin_graphs #
|
# }}} admin_graphs #
|
||||||
|
|||||||
Reference in New Issue
Block a user