Browse Source

Merge branch 'master' of https://github.com/Kevin-Mok/spotify-lib-vis into database

Removed some lines that prevented program from running.
master
Kevin Mok 7 years ago
parent
commit
c17d318f7d
  1. 8
      musicvis/settings.py
  2. 1
      requirements.txt
  3. 6
      spotifyvis/models.py
  4. 0
      spotifyvis/static/spotifyvis/scripts/user_data.js
  5. 3
      spotifyvis/templates/spotifyvis/user_data.html
  6. 20
      spotifyvis/utils.py
  7. 17
      spotifyvis/views.py

8
musicvis/settings.py

@ -76,8 +76,12 @@ WSGI_APPLICATION = 'musicvis.wsgi.application'
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'spotifyvis',
'USER': 'django',
'PASSWORD': 'django',
'HOST': 'localhost',
'PORT': '',
} }
} }

1
requirements.txt

@ -7,6 +7,7 @@ idna==2.6
isort==4.3.4 isort==4.3.4
lazy-object-proxy==1.3.1 lazy-object-proxy==1.3.1
mccabe==0.6.1 mccabe==0.6.1
psycopg2==2.7.4
pylint==1.8.4 pylint==1.8.4
pytz==2018.4 pytz==2018.4
requests==2.18.4 requests==2.18.4

6
spotifyvis/models.py

@ -3,7 +3,6 @@ from django.db import models
# Artist {{{ # # Artist {{{ #
class Artist(models.Model): class Artist(models.Model):
class Meta: class Meta:
verbose_name = "Artist" verbose_name = "Artist"
verbose_name_plural = "Artists" verbose_name_plural = "Artists"
@ -21,13 +20,12 @@ class Artist(models.Model):
# User {{{ # # User {{{ #
class User(models.Model): class User(models.Model):
class Meta: class Meta:
verbose_name = "User" verbose_name = "User"
verbose_name_plural = "Users" verbose_name_plural = "Users"
user_id = models.CharField(primary_key=True, max_length=30)
username = models.CharField(max_length=30)
user_id = models.CharField(primary_key=True, max_length=30) # the user's Spotify ID
username = models.CharField(max_length=30) # User's Spotify user name, if set
def __str__(self): def __str__(self):
return self.username return self.username

0
spotifyvis/static/spotifyvis/scripts/user_data.js

3
spotifyvis/templates/spotifyvis/user_data.html

@ -15,10 +15,11 @@
<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]-->
<p>Logged in as {{ id }}</p> <p>Logged in as {{ id }}</p>
<h2>Display name {{ user_name }}</h2>
<ul> <ul>
{% for genre_name, genre_count in genre_dict.items %} {% for genre_name, genre_count in genre_dict.items %}
<li>{{ genre_name }} - {{ genre_count }}</li> <li>{{ genre_name }} - {{ genre_count }}</li>
{% endfor %} {% endfor %}
</ul>
</ul>
</body> </body>
</html> </html>

20
spotifyvis/utils.py

@ -3,18 +3,19 @@
import requests import requests
import math import math
import pprint import pprint
from models import *
from .models import Artist, User, Track, AudioFeatures
# }}} imports # # }}} imports #
# parse_library {{{ # # parse_library {{{ #
def parse_library(headers, tracks, library_stats):
def parse_library(headers, tracks, library_stats, 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 :library_stats: Dictionary containing the data mined from user's library
:user: a User object representing the user whose library we are parsing
:returns: None :returns: None
@ -25,20 +26,25 @@ def parse_library(headers, tracks, library_stats):
# 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
feature_data_points = 0 # number of feature data analyses (some tracks do not have analyses available)
for _ in range(0, tracks, limit): for _ in range(0, tracks, limit):
payload['offset'] = str(offset) payload['offset'] = str(offset)
saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks', headers=headers, params=payload).json() saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks', headers=headers, params=payload).json()
num_samples = offset
for track_dict in saved_tracks_response['items']: for track_dict in saved_tracks_response['items']:
# Track the number of samples for calculating
# audio feature averages and standard deviations on the fly
num_samples += 1
get_track_info(track_dict['track'], library_stats, num_samples) get_track_info(track_dict['track'], library_stats, num_samples)
# get_genre(headers, track_dict['track']['album']['id']) # get_genre(headers, track_dict['track']['album']['id'])
audio_features_dict = get_audio_features(headers, track_dict['track']['id']) audio_features_dict = get_audio_features(headers, track_dict['track']['id'])
if len(audio_features_dict) != 0: if len(audio_features_dict) != 0:
num_samples += 1
# Track the number of audio analyses for calculating
# audio feature averages and standard deviations on the fly
feature_data_points += 1
for feature, feature_data in audio_features_dict.items(): for feature, feature_data in audio_features_dict.items():
update_audio_feature_stats(feature, feature_data, num_samples, library_stats)
update_audio_feature_stats(feature, feature_data, feature_data_points, library_stats)
for artist_dict in track_dict['track']['artists']: for artist_dict in track_dict['track']['artists']:
increase_artist_count(headers, artist_dict['name'], artist_dict['id'], library_stats) increase_artist_count(headers, artist_dict['name'], artist_dict['id'], library_stats)
# calculates num_songs with offset + songs retrieved # calculates num_songs with offset + songs retrieved

17
spotifyvis/views.py

@ -11,11 +11,12 @@ import json
import pprint import pprint
from datetime import datetime from datetime import datetime
from .utils import parse_library, process_library_stats from .utils import parse_library, process_library_stats
from .models import User, Track, AudioFeatures, Artist
# }}} imports # # }}} imports #
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}
TRACKS_TO_QUERY = 5
# generate_random_string {{{ # # generate_random_string {{{ #
@ -137,12 +138,18 @@ def user_data(request):
} }
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()
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']
user = None # will be set to the current user object later
# try:
# user = User.objects.get(user_id=request.session['user_id'])
# except User.DoesNotExist:
# user = User.objects.create(user_id=request.session['user_id'], user_name=request.session['user_name'])
context = { context = {
'user_name': user_data_response['display_name'],
'id': user_data_response['id'],
'user_name': user_data_response['display_name'],
'id': user_data_response['id'],
} }
tracks_to_query = 5
library_stats = { library_stats = {
"audio_features":{}, "audio_features":{},
"genres":{}, "genres":{},
@ -155,7 +162,7 @@ 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, user)
processed_library_stats = process_library_stats(library_stats) processed_library_stats = process_library_stats(library_stats)
print("================================================") print("================================================")
print("Processed data follows\n") print("Processed data follows\n")

Loading…
Cancel
Save