Browse Source

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.
master
Chris Shyi 7 years ago
parent
commit
9d43048067
  1. 9
      spotifyvis/templates/spotifyvis/test_db.html
  2. 9
      spotifyvis/utils.py
  3. 22
      spotifyvis/views.py

9
spotifyvis/templates/spotifyvis/test_db.html

@ -11,15 +11,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
</head> </head>
<body> <body>
<!--[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>
<![endif]-->
<ul>
{% for artist in artist_data %}
<li>{{ artist.name }} - {{ artist.num_songs }}</li>
{% endfor %}
</ul>
<pre> {% filter force_escape %} {% debug %} {% endfilter %} </pre>
<script src="https://d3js.org/d3.v5.min.js"></script> <script src="https://d3js.org/d3.v5.min.js"></script>
<script> <script>
d3.json("{% url "get_artist_data" user_id %}").then(function(data) { d3.json("{% url "get_artist_data" user_id %}").then(function(data) {

9
spotifyvis/utils.py

@ -13,12 +13,12 @@ import json
# parse_library {{{ # # parse_library {{{ #
def parse_library(headers, tracks, library_stats, user):
def parse_library(headers, tracks, user):
"""Scans user's library for certain number of tracks to update library_stats with. """Scans user's library for certain number of tracks to update library_stats with.
:headers: For API call. :headers: For API call.
:tracks: Number of tracks to get from user's library. :tracks: Number of tracks to get from user's library.
:library_stats: Dictionary containing the data mined from user's library
:user: a User object representing the user whose library we are parsing :user: a User object representing the user whose library we are parsing
:returns: None :returns: None
@ -30,8 +30,6 @@ def parse_library(headers, tracks, library_stats, user):
# keeps track of point to get songs from # keeps track of point to get songs from
offset = 0 offset = 0
payload = {'limit': str(limit)} payload = {'limit': str(limit)}
# use two separate variables to track, because the average popularity also requires num_samples
num_samples = 0 # number of actual track samples
# iterate until hit requested num of tracks # iterate until hit requested num of tracks
for _ in range(0, tracks, limit): for _ in range(0, tracks, limit):
@ -73,7 +71,6 @@ def parse_library(headers, tracks, library_stats, user):
""" """
# calculates num_songs with offset + songs retrieved # calculates num_songs with offset + songs retrieved
offset += limit offset += limit
# calculate_genres_from_artists(headers, library_stats)
# pprint.pprint(library_stats) # pprint.pprint(library_stats)
# }}} parse_library # # }}} parse_library #
@ -125,7 +122,6 @@ def save_audio_features(headers, track_id, track):
response = requests.get("https://api.spotify.com/v1/audio-features/{}".format(track_id), headers = headers).json() response = requests.get("https://api.spotify.com/v1/audio-features/{}".format(track_id), headers = headers).json()
if 'error' in response: if 'error' in response:
return {} return {}
features_dict = {}
# Data that we don't need # Data that we don't need
useless_keys = [ useless_keys = [
@ -135,7 +131,6 @@ def save_audio_features(headers, track_id, track):
audio_features_entry.track = track audio_features_entry.track = track
for key, val in response.items(): for key, val in response.items():
if key not in useless_keys: if key not in useless_keys:
features_dict[key] = val
setattr(audio_features_entry, key, val) setattr(audio_features_entry, key, val)
audio_features_entry.save() audio_features_entry.save()

22
spotifyvis/views.py

@ -153,27 +153,12 @@ def user_data(request):
'id': user_data_response['id'], 'id': user_data_response['id'],
} }
library_stats = {
"audio_features":{},
"genres":{},
"year_released":{},
"artists":{},
"num_songs": 0,
"popularity": {
"average": 0,
"std_dev": 0,
},
"total_runtime": 0
}
parse_library(headers, TRACKS_TO_QUERY, library_stats, user)
processed_library_stats = process_library_stats(library_stats)
# print("================================================")
# print("Processed data follows\n")
# pprint.pprint(processed_library_stats)
parse_library(headers, TRACKS_TO_QUERY, user)
return render(request, 'spotifyvis/user_data.html', context) return render(request, 'spotifyvis/user_data.html', context)
# }}} user_data # # }}} user_data #
def test_db(request): def test_db(request):
user_id = "polarbier" user_id = "polarbier"
context = { context = {
@ -191,6 +176,5 @@ def get_artist_data(request, user_id):
# user = User.objects.get(user_id=user_id) # user = User.objects.get(user_id=user_id)
artist_counts = Artist.objects.annotate(num_songs=Count('track')) 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] processed_artist_data = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts]
# for artist in artist_counts:
# print(artist.name, artist.num_songs)
return JsonResponse(data=processed_artist_data, safe=False) return JsonResponse(data=processed_artist_data, safe=False)
Loading…
Cancel
Save