Display genre counts on user_data page

Able to pass dict info successfully from backend to webpage.
This commit is contained in:
2018-05-20 23:11:35 -04:00
parent 6e1671b0f2
commit 96b7bfb2bb
2 changed files with 38 additions and 8 deletions

View File

@@ -14,7 +14,11 @@
<!--[if lt IE 7]> <!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p> <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
<![endif]--> <![endif]-->
<h1>Logged in as {{ id }}</h1> <p>Logged in as {{ id }}</p>
<h2>Display name {{ user_name }}</h2> <ul>
{% for genre_name, genre_count in genre_dict.items %}
<li>{{ genre_name }} - {{ genre_count }}</li>
{% endfor %}
</ul>
</body> </body>
</html> </html>

View File

@@ -1,3 +1,5 @@
# imports {{{ #
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseBadRequest from django.http import HttpResponse, HttpResponseBadRequest
import math import math
@@ -9,9 +11,15 @@ import json
import pprint import pprint
from datetime import datetime from datetime import datetime
# }}} imports #
# global vars {{{ #
TIME_FORMAT = '%Y-%m-%d-%H-%M-%S' TIME_FORMAT = '%Y-%m-%d-%H-%M-%S'
library_stats = {"audio_features":{}, "genres":{}, "year_released":{}, "artists":{}, "num_songs":0, "popularity":[], "total_runtime":0} library_stats = {"audio_features":{}, "genres":{}, "year_released":{}, "artists":{}, "num_songs":0, "popularity":[], "total_runtime":0}
# }}} global vars #
# generate_random_string {{{ # # generate_random_string {{{ #
def generate_random_string(length): def generate_random_string(length):
@@ -131,14 +139,15 @@ def user_data(request):
'Authorization': auth_token_str 'Authorization': auth_token_str
} }
tracks_to_query = 5
parse_library(headers, tracks_to_query)
user_data_response = requests.get('https://api.spotify.com/v1/me', headers = headers).json() user_data_response = requests.get('https://api.spotify.com/v1/me', headers = headers).json()
context = { context = {
'user_name': user_data_response['display_name'], 'user_name': user_data_response['display_name'],
'id': user_data_response['id'], 'id': user_data_response['id'],
'genre_dict': library_stats['genres']
} }
tracks_to_query = 5
parse_library(headers, tracks_to_query)
return render(request, 'spotifyvis/user_data.html', context) return render(request, 'spotifyvis/user_data.html', context)
# }}} user_data # # }}} user_data #
@@ -182,6 +191,8 @@ def parse_library(headers, tracks):
# }}} parse_library # # }}} parse_library #
# get_audio_features {{{ #
def get_audio_features(headers, track_id): def get_audio_features(headers, track_id):
"""Returns the audio features of a soundtrack """Returns the audio features of a soundtrack
@@ -206,6 +217,9 @@ def get_audio_features(headers, track_id):
return features_dict return features_dict
# }}} get_audio_features #
# update_std_dev {{{ #
def update_std_dev(cur_mean, cur_std_dev, new_data_point, sample_size): def update_std_dev(cur_mean, cur_std_dev, new_data_point, sample_size):
"""Calculates the standard deviation for a sample without storing all data points """Calculates the standard deviation for a sample without storing all data points
@@ -229,6 +243,9 @@ def update_std_dev(cur_mean, cur_std_dev, new_data_point, sample_size):
)) ))
return new_mean, new_std_dev return new_mean, new_std_dev
# }}} update_std_dev #
# update_audio_feature_stats {{{ #
def update_audio_feature_stats(feature, new_data_point, sample_size): def update_audio_feature_stats(feature, new_data_point, sample_size):
"""Updates the audio feature statistics in library_stats """Updates the audio feature statistics in library_stats
@@ -255,6 +272,7 @@ def update_audio_feature_stats(feature, new_data_point, sample_size):
library_stats['audio_features'][feature]['average'] = new_mean library_stats['audio_features'][feature]['average'] = new_mean
library_stats['audio_features'][feature]['std_dev'] = new_std_dev library_stats['audio_features'][feature]['std_dev'] = new_std_dev
# }}} update_audio_feature_stats #
# increase_nested_key {{{ # # increase_nested_key {{{ #
@@ -331,8 +349,16 @@ def calculate_genres_from_artists(headers):
""" """
for artist_entry in library_stats['artists'].values(): for artist_entry in library_stats['artists'].values():
artist_response = requests.get('https://api.spotify.com/v1/artists/' + artist_entry['id'], headers=headers).json() artist_response = requests.get('https://api.spotify.com/v1/artists/' + artist_entry['id'], headers=headers).json()
# increase each genre count by artist count # increase each genre count by artist count
for genre in artist_response['genres']: # for genre in artist_response['genres']:
increase_nested_key('genres', genre, artist_entry['count']) # print(genre, end='')
# increase_nested_key('genres', genre, artist_entry['count'])
# print('')
# only use first genre for simplicity right now
if len(artist_response['genres']) > 0:
print(artist_response['genres'][0])
increase_nested_key('genres', artist_response['genres'][0], artist_entry['count'])
# }}} calculate_genres_from_artists # # }}} calculate_genres_from_artists #