Browse Source

Process library_stats

library_stats is now processed into a format that's more easily consumed
by D3. Need to finalize format for popularity data.
master
Chris Shyi 7 years ago
parent
commit
e01331f951
  1. 55
      spotifyvis/utils.py
  2. 6
      spotifyvis/views.py

55
spotifyvis/utils.py

@ -214,7 +214,7 @@ def get_track_info(track_dict, library_stats, sample_size):
# increase_nested_key('artists', artist_name) # increase_nested_key('artists', artist_name)
# runtime # runtime
library_stats['total_runtime'] += float(track_dict['duration_ms']) / 60
library_stats['total_runtime'] += float(track_dict['duration_ms']) / (1000 * 60)
# }}} get_track_info # # }}} get_track_info #
@ -235,4 +235,55 @@ def calculate_genres_from_artists(headers, library_stats):
for genre in artist_response['genres']: for genre in artist_response['genres']:
increase_nested_key('genres', genre, library_stats, artist_entry['count']) increase_nested_key('genres', genre, library_stats, artist_entry['count'])
# }}} calculate_genres_from_artists #
# }}} calculate_genres_from_artists #
def process_library_stats(library_stats):
"""Processes library_stats into format more suitable for D3 consumption
Args:
library_stats: Dictionary containing the data mined from user's Spotify library
Returns:
A new dictionary that contains the data in library_stats, in a format more suitable for D3 consumption
"""
processed_library_stats = {}
for key in library_stats:
if key == 'artists' or key == 'genres' or key == 'year_released':
for inner_key in library_stats[key]:
if key not in processed_library_stats:
processed_library_stats[key] = []
processed_item_key = '' # identifier key for each dict in the list
count = 0
if 'artist' in key:
processed_item_key = 'name'
count = library_stats[key][inner_key]['count']
elif 'genre' in key:
processed_item_key = 'genre'
count = library_stats[key][inner_key]
else:
processed_item_key = 'year'
count = library_stats[key][inner_key]
processed_library_stats[key].append({
processed_item_key: inner_key,
"count": count
})
elif key == 'audio_features':
for audio_feature in library_stats[key]:
if 'audio_features' not in processed_library_stats:
processed_library_stats['audio_features'] = []
processed_library_stats['audio_features'].append({
'feature': audio_feature,
'average': library_stats[key][audio_feature]['average'],
'std_dev': library_stats[key][audio_feature]['std_dev']
})
# TODO: Not sure about final form for 'popularity'
# elif key == 'popularity':
# processed_library_stats[key] = []
# processed_library_stats[key].append({
# })
elif key == 'num_songs' or key == 'total_runtime' or key == 'popularity':
processed_library_stats[key] = library_stats[key]
return processed_library_stats

6
spotifyvis/views.py

@ -8,7 +8,7 @@ import urllib
import json import json
import pprint import pprint
from datetime import datetime from datetime import datetime
from .utils import *
from .utils import parse_library, process_library_stats
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}
@ -152,6 +152,10 @@ def user_data(request):
"total_runtime": 0 "total_runtime": 0
} }
parse_library(headers, tracks_to_query, library_stats) parse_library(headers, tracks_to_query, library_stats)
processed_library_stats = process_library_stats(library_stats)
print("================================================")
print("Processed data follows\n")
pprint.pprint(processed_library_stats)
return render(request, 'spotifyvis/user_data.html', context) return render(request, 'spotifyvis/user_data.html', context)
# }}} user_data # # }}} user_data #
Loading…
Cancel
Save