From 9d430480678fb2face146f859986bc4b4056817d Mon Sep 17 00:00:00 2001 From: Chris Shyi Date: Sat, 9 Jun 2018 13:58:52 -0400 Subject: [PATCH 1/3] Clean up views.py and utils.py further views.py and utils.py were cleaned up further to remove any obsolete usage of library_stats. --- spotifyvis/templates/spotifyvis/test_db.html | 9 -------- spotifyvis/utils.py | 9 ++------ spotifyvis/views.py | 22 +++----------------- 3 files changed, 5 insertions(+), 35 deletions(-) diff --git a/spotifyvis/templates/spotifyvis/test_db.html b/spotifyvis/templates/spotifyvis/test_db.html index 0c51c81..ba3dcaa 100644 --- a/spotifyvis/templates/spotifyvis/test_db.html +++ b/spotifyvis/templates/spotifyvis/test_db.html @@ -11,15 +11,6 @@ - - -
 {% filter force_escape %} {% debug %} {% endfilter %} 
+ diff --git a/spotifyvis/urls.py b/spotifyvis/urls.py index 55380bd..eb7e299 100644 --- a/spotifyvis/urls.py +++ b/spotifyvis/urls.py @@ -10,4 +10,6 @@ urlpatterns = [ path('user_data', user_data, name='user_data'), path('test_db', test_db, name='test_db'), path('user_artists/', get_artist_data, name='get_artist_data'), + path('audio_features//', get_audio_feature_data, name='get_audio_feature_data'), + ] diff --git a/spotifyvis/views.py b/spotifyvis/views.py index 30da1b6..1385371 100644 --- a/spotifyvis/views.py +++ b/spotifyvis/views.py @@ -105,7 +105,7 @@ def callback(request): 'client_secret': os.environ['SPOTIFY_CLIENT_SECRET'], } - response = requests.post('https://accounts.spotify.com/api/token', data = payload).json() + response = requests.post('https://accounts.spotify.com/api/token', data=payload).json() # despite its name, datetime.today() returns a datetime object, not a date object # use datetime.strptime() to get a datetime object from a string request.session['token_obtained_at'] = datetime.strftime(datetime.today(), TIME_FORMAT) @@ -142,15 +142,18 @@ def user_data(request): } user_data_response = requests.get('https://api.spotify.com/v1/me', headers = headers).json() - request.session['user_id'] = user_data_response['id'] # store the user_id so it may be used to create model + request.session['user_id'] = user_data_response['id'] # store the user_id so it may be used to create model # request.session['user_name'] = user_data_response['display_name'] - # get_or_create() returns a tuple (obj, created) - user = User.objects.get_or_create(user_id=user_data_response['id'])[0] + try: + user = User.objects.get(user_id=user_data_response['id']) + except User.DoesNotExist: + user = User(user_id=user_data_response['id'], user_secret=generate_random_string(30)) + user.save() context = { - 'user_name': user_data_response['display_name'], - 'id': user_data_response['id'], + 'id': user_data_response['id'], + 'user_secret': user.user_secret, } parse_library(headers, TRACKS_TO_QUERY, user) @@ -177,4 +180,24 @@ def get_artist_data(request, user_id): artist_counts = Artist.objects.annotate(num_songs=Count('track')) processed_artist_data = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts] - return JsonResponse(data=processed_artist_data, safe=False) + return JsonResponse(data=processed_artist_data, safe=False) + + +def get_audio_feature_data(request, audio_feature, client_secret): + """Returns all data points for a given audio feature + + Args: + request: the HTTP request + audio_feature: The audio feature to be queried + client_secret: client secret, used to identify the user + """ + user = User.objects.get(user_secret=client_secret) + user_tracks = Track.objects.filter(users=user) + response_payload = { + 'data_points': [], + } + for track in user_tracks: + audio_feature_obj = AudioFeatures.objects.get(track=track) + response_payload['data_points'].append(getattr(audio_feature_obj, audio_feature)) + return JsonResponse(response_payload) + From 4698663a85a6327fe8146df26f58a5d7f6b43b59 Mon Sep 17 00:00:00 2001 From: Chris Shyi Date: Sun, 10 Jun 2018 09:21:35 -0400 Subject: [PATCH 3/3] Rewrite generate_random_string() Rewrote generate_random_string() in a more Pythonic fashion. --- spotifyvis/views.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/spotifyvis/views.py b/spotifyvis/views.py index 1385371..12774d4 100644 --- a/spotifyvis/views.py +++ b/spotifyvis/views.py @@ -7,6 +7,7 @@ import os import urllib import json import pprint +import string from datetime import datetime from django.shortcuts import render, redirect @@ -32,11 +33,8 @@ def generate_random_string(length): Returns: A random string """ - rand_str = "" - possible_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" - - for _ in range(length): - rand_str += possible_chars[random.randint(0, len(possible_chars) - 1)] + all_chars = string.ascii_letters + string.digits + string.punctuation + rand_str = "".join(random.choice(all_chars) for _ in range(length)) return rand_str