Split spotifyvis code into different apps (#47)
Server is able to start, but none of the apps are linked together yet.
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
from django.contrib import admin
|
||||
from .models import Track, Artist, AudioFeatures, User
|
||||
|
||||
# Register your models here.
|
||||
admin.site.register(Track)
|
||||
admin.site.register(Artist)
|
||||
admin.site.register(AudioFeatures)
|
||||
admin.site.register(User)
|
||||
@@ -1,5 +0,0 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class SpotifyvisConfig(AppConfig):
|
||||
name = 'spotifyvis'
|
||||
@@ -1,104 +0,0 @@
|
||||
from django.db import models
|
||||
|
||||
# id's are 22 in length in examples but set to 30 for buffer
|
||||
MAX_ID = 30
|
||||
|
||||
# Genre {{{ #
|
||||
|
||||
class Genre(models.Model):
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Genre"
|
||||
verbose_name_plural = "Genres"
|
||||
|
||||
name = models.CharField(primary_key=True, max_length=50)
|
||||
num_songs = models.PositiveIntegerField()
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
# }}} Genre #
|
||||
|
||||
# Artist {{{ #
|
||||
|
||||
|
||||
class Artist(models.Model):
|
||||
class Meta:
|
||||
verbose_name = "Artist"
|
||||
verbose_name_plural = "Artists"
|
||||
|
||||
artist_id = models.CharField(primary_key=True, max_length=MAX_ID)
|
||||
# unique since only storing one genre per artist right now
|
||||
name = models.CharField(unique=True, max_length=50)
|
||||
genres = models.ManyToManyField(Genre, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
# }}} Artist #
|
||||
|
||||
# User {{{ #
|
||||
|
||||
class User(models.Model):
|
||||
class Meta:
|
||||
verbose_name = "User"
|
||||
verbose_name_plural = "Users"
|
||||
|
||||
user_id = models.CharField(primary_key=True, max_length=MAX_ID) # the user's Spotify ID
|
||||
user_secret = models.CharField(max_length=50, default='')
|
||||
|
||||
def __str__(self):
|
||||
return self.user_id
|
||||
|
||||
# }}} User #
|
||||
|
||||
# Track {{{ #
|
||||
|
||||
class Track(models.Model):
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Track"
|
||||
verbose_name_plural = "Tracks"
|
||||
|
||||
track_id = models.CharField(primary_key=True, max_length=MAX_ID)
|
||||
# artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
|
||||
artists = models.ManyToManyField(Artist, blank=True)
|
||||
year = models.PositiveSmallIntegerField()
|
||||
popularity = models.PositiveSmallIntegerField()
|
||||
runtime = models.PositiveSmallIntegerField()
|
||||
name = models.CharField(max_length=200)
|
||||
users = models.ManyToManyField(User, blank=True)
|
||||
genre = models.ForeignKey(Genre, on_delete=models.CASCADE, blank=True,
|
||||
null=True)
|
||||
|
||||
def __str__(self):
|
||||
track_str = "{}, genre: {}, artists: [".format(self.name, self.genre)
|
||||
for artist in self.artists.all():
|
||||
track_str += "{}, ".format(artist.name)
|
||||
track_str += "]"
|
||||
return track_str
|
||||
|
||||
# }}} Track #
|
||||
|
||||
# AudioFeatures {{{ #
|
||||
|
||||
class AudioFeatures(models.Model):
|
||||
|
||||
class Meta:
|
||||
verbose_name = "AudioFeatures"
|
||||
verbose_name_plural = "AudioFeatures"
|
||||
|
||||
track = models.OneToOneField(Track, on_delete=models.CASCADE, primary_key=True,)
|
||||
acousticness = models.DecimalField(decimal_places=3, max_digits=3)
|
||||
danceability = models.DecimalField(decimal_places=3, max_digits=3)
|
||||
energy = models.DecimalField(decimal_places=3, max_digits=3)
|
||||
instrumentalness = models.DecimalField(decimal_places=3, max_digits=3)
|
||||
loudness = models.DecimalField(decimal_places=3, max_digits=6)
|
||||
speechiness = models.DecimalField(decimal_places=3, max_digits=3)
|
||||
tempo = models.DecimalField(decimal_places=3, max_digits=6)
|
||||
valence = models.DecimalField(decimal_places=3, max_digits=3)
|
||||
|
||||
def __str__(self):
|
||||
return super(AudioFeatures, self).__str__()
|
||||
|
||||
# }}} AudioFeatures #
|
||||
127
spotifyvis/settings.py
Normal file
127
spotifyvis/settings.py
Normal file
@@ -0,0 +1,127 @@
|
||||
"""
|
||||
Django settings for musicdata project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 2.0.5.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/2.0/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/2.0/ref/settings/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = 'm(gh1&063)haj#^t+h-qh@o6#%w=ruqi_#=6yzi=#@q6#0p*gv'
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
ALLOWED_HOSTS = []
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'login.apps.LoginConfig',
|
||||
'api.apps.ApiConfig',
|
||||
'graphs.apps.GraphsConfig',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'spotifyvis.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [],
|
||||
'APP_DIRS': True,
|
||||
'OPTIONS': {
|
||||
'context_processors': [
|
||||
'django.template.context_processors.debug',
|
||||
'django.template.context_processors.request',
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = 'spotifyvis.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.postgresql_psycopg2',
|
||||
'NAME': 'spotifyvis',
|
||||
'USER': 'django',
|
||||
'PASSWORD': 'django',
|
||||
'HOST': 'localhost',
|
||||
'PORT': '',
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/2.0/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
TIME_ZONE = 'America/Toronto'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_L10N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/2.0/howto/static-files/
|
||||
|
||||
STATIC_URL = '/static/'
|
||||
@@ -1,8 +0,0 @@
|
||||
body {
|
||||
background-color: #1e1e1e;
|
||||
}
|
||||
|
||||
h1,p {
|
||||
color: grey;
|
||||
}
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
/**
|
||||
* Draws the artist count graph as a bubble chart, and appends it the a designated parent element
|
||||
* @param artistData: the artist counts data as an array of objects, of the format {'name': artist name, 'num_songs': 50}
|
||||
* @param parentElem: the DOM element to append the artist graph to (as a string)
|
||||
*/
|
||||
function drawArtistGraph(artistData, parentElem) {
|
||||
let margin = {top: 20, right: 30, bottom: 30, left: 40};
|
||||
let width = 960 - margin.right - margin.left;
|
||||
let height = 540 - margin.top - margin.bottom;
|
||||
|
||||
let color = d3.scaleOrdinal(d3.schemeCategory10);
|
||||
let bubble = d3.pack(artistData)
|
||||
.size([width, height])
|
||||
.padding(1.5);
|
||||
|
||||
let svg = d3.select(parentElem)
|
||||
.append("svg")
|
||||
.attr("width", width + margin.right + margin.left)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.attr("class", "bubble");
|
||||
|
||||
let nodes = d3.hierarchy(artistData)
|
||||
.sum(function(d) { return d.num_songs; });
|
||||
|
||||
let node = svg.selectAll(".node")
|
||||
.data(bubble(nodes).descendants())
|
||||
.enter()
|
||||
.filter(function(d) {
|
||||
return !d.children;
|
||||
})
|
||||
.append("g")
|
||||
.attr("class", "node")
|
||||
.attr("transform", function(d) {
|
||||
return "translate(" + d.x + "," + d.y + ")";
|
||||
});
|
||||
|
||||
node.append("title")
|
||||
.text(function(d) {
|
||||
return `${d.name}: ${d.num_songs}`;
|
||||
});
|
||||
|
||||
node.append("circle")
|
||||
.attr("r", function(d) {
|
||||
return d.r;
|
||||
})
|
||||
.style("fill", function(d,i) {
|
||||
return color(i);
|
||||
});
|
||||
|
||||
// artist name text
|
||||
node.append("text")
|
||||
.attr("dy", ".2em")
|
||||
.style("text-anchor", "middle")
|
||||
.text(function(d) {
|
||||
return d.data.name.substring(0, d.r / 3);
|
||||
})
|
||||
.attr("font-family", "sans-serif")
|
||||
.attr("font-size", function(d){
|
||||
return d.r/5;
|
||||
})
|
||||
.attr("fill", "white");
|
||||
|
||||
// artist song count text
|
||||
node.append("text")
|
||||
.attr("dy", "1.3em")
|
||||
.style("text-anchor", "middle")
|
||||
.text(function(d) {
|
||||
return d.data.num_songs;
|
||||
})
|
||||
.attr("font-family", "Gill Sans", "Gill Sans MT")
|
||||
.attr("font-size", function(d){
|
||||
return d.r/5;
|
||||
})
|
||||
.attr("fill", "white");
|
||||
|
||||
d3.select(self.frameElement)
|
||||
.style("height", height + "px");
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
function create_genre_graph(data) {
|
||||
// convert strings to nums {{{ //
|
||||
|
||||
data.forEach(function(d) {
|
||||
d.num_songs = +d.num_songs;
|
||||
console.log(d.genre, d.num_songs);
|
||||
var artist_names = Object.keys(d.artists);
|
||||
artist_names.forEach(function(e) {
|
||||
d.artists[e] = +d.artists[e];
|
||||
console.log(e, d.artists[e]);
|
||||
//console.log(e, d.artists[e], d.artists[e] + 1);
|
||||
});
|
||||
});
|
||||
|
||||
// }}} convert strings to nums //
|
||||
|
||||
// domains {{{ //
|
||||
|
||||
data.sort(function(a, b) {
|
||||
return b.num_songs - a.num_songs;
|
||||
});
|
||||
x.domain(data.map(function(d) {
|
||||
return d.genre;
|
||||
}));
|
||||
//y.domain([0, d3.max(data, function(d) { return d.num_songs; }) * 1.25]).nice();
|
||||
y.domain([0, d3.max(data, function(d) {
|
||||
return d.num_songs;
|
||||
})]).nice();
|
||||
|
||||
// }}} domains //
|
||||
|
||||
// setup bar colors {{{ //
|
||||
|
||||
var max_artists = d3.max(data, function(d) {
|
||||
return Object.keys(d.artists).length;
|
||||
});
|
||||
var z = d3.scaleOrdinal().range(randomColor({
|
||||
count: max_artists,
|
||||
luminosity: 'light',
|
||||
}));
|
||||
|
||||
// }}} setup bar colors //
|
||||
|
||||
for (var genre_dict of data) {
|
||||
|
||||
// process artist breakdown {{{ //
|
||||
|
||||
var keys = Object.keys(genre_dict.artists);
|
||||
var stack = d3.stack()
|
||||
//.order(d3.stackOrderAscending)
|
||||
.order(d3.stackOrderDescending)
|
||||
.keys(keys)([genre_dict.artists])
|
||||
//unpack the column
|
||||
.map((d, i) => {
|
||||
return {
|
||||
key: keys[i],
|
||||
data: d[0]
|
||||
}
|
||||
});
|
||||
|
||||
// }}} process artist breakdown //
|
||||
|
||||
// add bars {{{ //
|
||||
|
||||
g.append("g")
|
||||
.selectAll("rect")
|
||||
.data(stack)
|
||||
.enter().append("rect")
|
||||
.attr("x", x(genre_dict.genre))
|
||||
.attr("y", function(d) {
|
||||
return y(d.data[1]);
|
||||
})
|
||||
.attr("height", d => y(d.data[0]) - y(d.data[1]))
|
||||
.attr("width", x.bandwidth())
|
||||
.attr('fill', (d, i) => z(i))
|
||||
.append('title').text(d => d.key + ': ' + (d.data[1] - d.data[0]));
|
||||
|
||||
// }}} add bars //
|
||||
|
||||
// x-axis {{{ //
|
||||
|
||||
g.append("g")
|
||||
.attr("class", "axis")
|
||||
.attr("transform", "translate(0," + height + ")")
|
||||
.call(d3.axisBottom(x))
|
||||
.selectAll(".tick text")
|
||||
.call(wrap, x.bandwidth());
|
||||
|
||||
// }}} x-axis //
|
||||
|
||||
// y-axis {{{ //
|
||||
|
||||
g.append("g")
|
||||
.attr("class", "axis")
|
||||
.call(d3.axisLeft(y).ticks(null, "s"))
|
||||
.append("text")
|
||||
.attr("x", 2)
|
||||
.attr("y", y(y.ticks().pop()) + 0.5)
|
||||
.attr("dy", "0.32em")
|
||||
.attr("fill", "#000")
|
||||
.attr("font-weight", "bold")
|
||||
.attr("text-anchor", "start")
|
||||
.text("Songs");
|
||||
|
||||
// }}} y-axis //
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// wrap text {{{ //
|
||||
|
||||
// https://gist.github.com/guypursey/f47d8cd11a8ff24854305505dbbd8c07#file-index-html
|
||||
function wrap(text, width) {
|
||||
text.each(function() {
|
||||
var text = d3.select(this),
|
||||
words = text.text().split(/\s+/).reverse(),
|
||||
word,
|
||||
line = [],
|
||||
lineNumber = 0,
|
||||
lineHeight = 1.1, // ems
|
||||
y = text.attr("y"),
|
||||
dy = parseFloat(text.attr("dy")),
|
||||
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em")
|
||||
while (word = words.pop()) {
|
||||
line.push(word)
|
||||
tspan.text(line.join(" "))
|
||||
if (tspan.node().getComputedTextLength() > width) {
|
||||
line.pop()
|
||||
tspan.text(line.join(" "))
|
||||
line = [word]
|
||||
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", `${++lineNumber * lineHeight + dy}em`).text(word)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// }}} wrap text //
|
||||
@@ -1,42 +0,0 @@
|
||||
document.getElementById("login-btn").addEventListener("click", function() {
|
||||
let httpRequest = new XMLHttpRequest();
|
||||
|
||||
/*
|
||||
* Handler for the response
|
||||
*/
|
||||
httpRequest.onreadystatechange = function() {
|
||||
if (httpRequest.readyState === XMLHttpRequest.DONE) {
|
||||
if (httpRequest.status === 200) {
|
||||
// hide the login button
|
||||
document.getElementById('login').setAttribute("display", "none");
|
||||
|
||||
let responseData = JSON.parse(httpRequest.responseText);
|
||||
let dataList = document.getElementById("data-list");
|
||||
|
||||
|
||||
for (let key in responseData) {
|
||||
let newLi = document.createElement("li");
|
||||
let innerList = document.createElement("ul");
|
||||
|
||||
let dataLabel = document.createElement("li");
|
||||
dataLabel.innerText = key;
|
||||
|
||||
let dataValue = document.createElement("li");
|
||||
dataValue.innerText = responseData[key];
|
||||
|
||||
innerList.appendChild(dataLabel);
|
||||
innerList.appendChild(dataValue);
|
||||
|
||||
newLi.appendChild(innerList);
|
||||
dataList.appendChild(newLi);
|
||||
}
|
||||
} else {
|
||||
alert("There was a problem with the login request, please try again!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
httpRequest.open('GET', '/login', true);
|
||||
httpRequest.send();
|
||||
});
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
{% load static %}
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Artist Graphs</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Logged in as {{ user_id }}</p>
|
||||
<script src="https://d3js.org/d3.v5.js"></script>
|
||||
<script src="{% static "spotifyvis/scripts/artist_graph.js" %}"></script>
|
||||
<script>
|
||||
d3.json("{% url "get_artist_data" user_secret %}").then(function(data) {
|
||||
for (let index = 0; index < data.length; index++) {
|
||||
console.log(data[index].name);
|
||||
console.log(data[index].num_songs);
|
||||
}
|
||||
// this is the data format needed for bubble charts
|
||||
data = {
|
||||
"children": data
|
||||
};
|
||||
drawArtistGraph(data, "body");
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,142 +0,0 @@
|
||||
{% load static %}
|
||||
<!DOCTYPE html>
|
||||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
|
||||
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
|
||||
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
|
||||
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<title>User Spotify Data</title>
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<style>
|
||||
.tick {
|
||||
font-size: 15px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<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]-->
|
||||
<p>Logged in as {{ user_id }}</p>
|
||||
<script src="https://d3js.org/d3.v5.js"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
/** Queries the backend for audio feature data, draws the bar chart
|
||||
* illustrating the frequencies of values, and appends the chart to
|
||||
* a designated parent element
|
||||
*
|
||||
* @param audioFeature: the name of the audio feature (string)
|
||||
* @param intervalEndPoints: a sorted array of 5 real numbers defining the intervals (categories) of values,
|
||||
* for example:
|
||||
* [0, 0.25, 0.5, 0.75, 1.0] for instrumentalness would define ranges
|
||||
* (0-0.25), (0.25-0.5), (0.5-0.75), (0.75-1.0)
|
||||
* @param parentElem: the DOM element to append the graph to (a selector string)
|
||||
* @return None
|
||||
*/
|
||||
function drawAudioFeatGraph(audioFeature, intervalEndPoints, parentElem) {
|
||||
// TODO: Not hard code the dimensions?
|
||||
let margin = {top: 20, right: 30, bottom: 30, left: 40};
|
||||
let width = 480 - margin.left - margin.right,
|
||||
height = 270 - margin.top - margin.bottom;
|
||||
|
||||
let featureData = {};
|
||||
// Create the keys first in order
|
||||
for (let index = 0; index < intervalEndPoints.length - 1; index++) {
|
||||
let key = `${intervalEndPoints[index]} ~ ${intervalEndPoints[index + 1]}`;
|
||||
featureData[key] = 0;
|
||||
}
|
||||
// define the vertical scaling function
|
||||
let vScale = d3.scaleLinear().range([height, 0]);
|
||||
|
||||
d3.json(`/api/audio_features/${audioFeature}/{{ user_secret }}`)
|
||||
.then(function(response) {
|
||||
// categorize the data points
|
||||
for (let dataPoint of response.data_points) {
|
||||
dataPoint = parseFloat(dataPoint);
|
||||
let index = intervalEndPoints.length - 2;
|
||||
// find the index of the first element greater than dataPoint
|
||||
while (dataPoint < intervalEndPoints[index]) {
|
||||
index -= 1;
|
||||
}
|
||||
let key = `${intervalEndPoints[index]} ~ ${intervalEndPoints[index + 1]}`;
|
||||
featureData[key] += 1;
|
||||
}
|
||||
|
||||
let dataSet = Object.values(featureData);
|
||||
let dataRanges = Object.keys(featureData); // Ranges of audio features, e.g. 0-0.25, 0.25-0.5, etc
|
||||
let dataArr = [];
|
||||
// turn the counts into an array of objects, e.g. {range: "0-0.25", counts: 5}
|
||||
for (let i = 0; i < dataRanges.length; i++) {
|
||||
dataArr.push({
|
||||
range: dataRanges[i],
|
||||
counts: featureData[dataRanges[i]]
|
||||
});
|
||||
}
|
||||
vScale.domain([0, d3.max(dataSet)]).nice();
|
||||
|
||||
let hScale = d3.scaleBand().domain(dataRanges).rangeRound([0, width]).padding(0.5);
|
||||
|
||||
let xAxis = d3.axisBottom().scale(hScale);
|
||||
let yAxis = d3.axisLeft().scale(vScale);
|
||||
|
||||
let featureSVG = d3.select(parentElem)
|
||||
.append('svg').attr('width', width + margin.left + margin.right)
|
||||
.attr('height', height + margin.top + margin.bottom);
|
||||
|
||||
let featureGraph = featureSVG.append("g")
|
||||
.attr("transform", `translate(${margin.left}, ${margin.top})`)
|
||||
.attr("fill", "teal");
|
||||
|
||||
featureGraph.selectAll(".bar")
|
||||
.data(dataArr)
|
||||
.enter().append('rect')
|
||||
.attr('class', 'bar')
|
||||
.attr('x', function(d) { return hScale(d.range); })
|
||||
.attr('y', function(d) { return vScale(d.counts); })
|
||||
.attr("height", function(d) { return height - vScale(d.counts); })
|
||||
.attr("width", hScale.bandwidth());
|
||||
|
||||
// function(d) { return hScale(d.range); }
|
||||
|
||||
featureGraph.append('g')
|
||||
.attr('class', 'axis')
|
||||
.attr('transform', `translate(0, ${height})`)
|
||||
.call(xAxis);
|
||||
|
||||
featureGraph.append('g')
|
||||
.attr('class', 'axis')
|
||||
.call(yAxis);
|
||||
|
||||
featureSVG.append("text")
|
||||
.attr('x', (width / 2))
|
||||
.attr('y', (margin.top / 2))
|
||||
.attr('text-anchor', 'middle')
|
||||
.style('font-size', '14px')
|
||||
.text(`${capFeatureStr(audioFeature)}`);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the audio feature name string with the first letter capitalized
|
||||
* @param audioFeature: the name of the audio feature
|
||||
* @returns the audio feature name string with the first letter capitalized
|
||||
*/
|
||||
function capFeatureStr(audioFeature) {
|
||||
return audioFeature.charAt(0).toUpperCase() + audioFeature.slice(1);
|
||||
}
|
||||
|
||||
drawAudioFeatGraph("instrumentalness", [0, 0.25, 0.5, 0.75, 1.0], 'body');
|
||||
drawAudioFeatGraph("valence", [0, 0.25, 0.5, 0.75, 1.0], 'body');
|
||||
drawAudioFeatGraph("energy", [0, 0.25, 0.5, 0.75, 1.0], 'body');
|
||||
drawAudioFeatGraph("tempo", [40, 80, 120, 160, 200], 'body');
|
||||
drawAudioFeatGraph("danceability", [0, 0.25, 0.5, 0.75, 1.0], 'body');
|
||||
drawAudioFeatGraph("acousticness", [0, 0.25, 0.5, 0.75, 1.0], 'body');
|
||||
drawAudioFeatGraph("loudness", [-60, -45, -30, -15, 0], 'body');
|
||||
drawAudioFeatGraph("speechiness", [0, 0.25, 0.5, 0.75, 1.0], 'body');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,44 +0,0 @@
|
||||
<!-- header {{{ -->
|
||||
|
||||
<!DOC
|
||||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
|
||||
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
|
||||
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
|
||||
<!--[if gt IE 8]><!-->
|
||||
{% load static %}
|
||||
<html class="no-js"> <!--<![endif]-->
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<title>Test DB Page</title>
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="{% static 'spotifyvis/css/dark_bg.css' %}">
|
||||
</head>
|
||||
|
||||
<!-- }}} header -->
|
||||
|
||||
<body>
|
||||
<script src="https://d3js.org/d3.v5.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/randomcolor/0.5.2/randomColor.min.js"></script>
|
||||
{% load static %}
|
||||
<script src="{% static "spotifyvis/scripts/genre_graph.js" %}"></script>
|
||||
|
||||
<svg width="1920" height="740"></svg>
|
||||
<script>
|
||||
var svg = d3.select("svg"),
|
||||
margin = {top: 20, right: 20, bottom: 30, left: 40},
|
||||
width = +svg.attr("width") - margin.left - margin.right,
|
||||
height = +svg.attr("height") - margin.top - margin.bottom,
|
||||
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
var x = d3.scaleBand()
|
||||
.rangeRound([0, width])
|
||||
.paddingInner(0.05)
|
||||
.align(0.1);
|
||||
var y = d3.scaleLinear()
|
||||
.rangeRound([height, 0]);
|
||||
|
||||
d3.json("{% url "get_genre_data" user_secret %}").then(create_genre_graph);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,31 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
{% load static %}
|
||||
<html>
|
||||
<head>
|
||||
<title>User Login</title>
|
||||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="{% static 'spotifyvis/css/dark_bg.css' %}">
|
||||
<style type="text/css">
|
||||
.text-overflow {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
width: 500px;
|
||||
}
|
||||
</style>
|
||||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
<div id="login">
|
||||
<h1>spotify-lib-vis</h1>
|
||||
<a href="/login" class="btn btn-primary">Scan Library</a>
|
||||
<a href="{% url "admin_graphs" %}" class="btn btn-primary">Admin Graphs</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<script src="{% static 'spotifyvis/scripts/index.js' %}"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,20 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
{% load static %}
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Logged In</title>
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="{% static 'spotifyvis/css/dark_bg.css' %}">
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{ user_id }}'s Graphs</h1>
|
||||
<a class="btn btn-primary" href="{% url "display_audio_features" user_secret %}"
|
||||
role="button">Audio Features</a>
|
||||
<a class="btn btn-primary" href="{% url "display_genre_graph" user_secret %}"
|
||||
role="button">Genres</a>
|
||||
<a class="btn btn-primary" href="{% url "display_artist_graph" user_secret %}" role="button">
|
||||
Artists
|
||||
</a>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,21 +0,0 @@
|
||||
{% load static %}
|
||||
<!DOCTYPE html>
|
||||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
|
||||
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
|
||||
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
|
||||
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<title>User Spotify Data</title>
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="{% static 'spotifyvis/css/dark_bg.css' %}">
|
||||
</head>
|
||||
<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]-->
|
||||
<p>Logged in as {{ id }}</p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,67 +0,0 @@
|
||||
from django.test import TestCase
|
||||
from .utils import update_std_dev
|
||||
import math
|
||||
# Create your tests here.
|
||||
|
||||
class UpdateStdDevTest(TestCase):
|
||||
|
||||
def test_two_data_points(self):
|
||||
"""
|
||||
tests if update_std_dev behaves correctly for two data points
|
||||
"""
|
||||
cur_mean = 5
|
||||
cur_std_dev = 0
|
||||
|
||||
new_mean, new_std_dev = update_std_dev(cur_mean, cur_std_dev, 10, 2)
|
||||
|
||||
self.assertTrue(math.isclose(new_mean, 7.5, rel_tol=0.01))
|
||||
self.assertTrue(math.isclose(new_std_dev, 3.5355, rel_tol=0.01))
|
||||
|
||||
|
||||
def test_three_data_points(self):
|
||||
"""
|
||||
tests if update_std_dev behaves correctly for three data points
|
||||
"""
|
||||
cur_mean = 7.5
|
||||
cur_std_dev = 3.5355
|
||||
|
||||
new_mean, new_std_dev = update_std_dev(cur_mean, cur_std_dev, 15, 3)
|
||||
|
||||
self.assertTrue(math.isclose(new_mean, 10, rel_tol=0.01))
|
||||
self.assertTrue(math.isclose(new_std_dev, 5, rel_tol=0.01))
|
||||
|
||||
|
||||
def test_four_data_points(self):
|
||||
"""
|
||||
tests if update_std_dev behaves correctly for four data points
|
||||
"""
|
||||
cur_mean = 10
|
||||
cur_std_dev = 5
|
||||
|
||||
new_mean, new_std_dev = update_std_dev(cur_mean, cur_std_dev, 20, 4)
|
||||
self.assertTrue(math.isclose(new_mean, 12.5, rel_tol=0.01))
|
||||
self.assertTrue(math.isclose(new_std_dev, 6.455, rel_tol=0.01))
|
||||
|
||||
|
||||
def test_five_data_points(self):
|
||||
"""
|
||||
tests if update_std_dev behaves correctly for five data points
|
||||
"""
|
||||
cur_mean = 12.5
|
||||
cur_std_dev = 6.455
|
||||
|
||||
new_mean, new_std_dev = update_std_dev(cur_mean, cur_std_dev, 63, 5)
|
||||
self.assertTrue(math.isclose(new_mean, 22.6, rel_tol=0.01))
|
||||
self.assertTrue(math.isclose(new_std_dev, 23.2658, rel_tol=0.01))
|
||||
|
||||
|
||||
def test_sixteen_data_points(self):
|
||||
"""
|
||||
tests if update_std_dev behaves correctly for sixteen data points
|
||||
"""
|
||||
cur_mean = 0.4441
|
||||
cur_std_dev = 0.2855
|
||||
|
||||
new_mean, new_std_dev = update_std_dev(cur_mean, cur_std_dev, 0.7361, 16)
|
||||
self.assertTrue(math.isclose(new_mean, 0.4624, rel_tol=0.01))
|
||||
self.assertTrue(math.isclose(new_std_dev, 0.2853, rel_tol=0.01))
|
||||
@@ -1,19 +1,24 @@
|
||||
"""musicdata URL Configuration
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/2.0/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
|
||||
from .views import *
|
||||
|
||||
urlpatterns = [
|
||||
path('', index, name='index'),
|
||||
path('login', login, name='login'),
|
||||
path('callback', callback, name='callback'),
|
||||
path('user_data', user_data, name='user_data'),
|
||||
path('admin_graphs', admin_graphs, name='admin_graphs'),
|
||||
path('api/user_artists/<str:user_secret>', get_artist_data, name='get_artist_data'),
|
||||
path('artists/<str:user_secret>', artist_data, name='display_artist_graph'),
|
||||
path('api/user_genres/<str:user_secret>', get_genre_data, name='get_genre_data'),
|
||||
path('graphs/genre/<str:user_secret>', display_genre_graph,
|
||||
name='display_genre_graph'),
|
||||
path('audio_features/<str:user_secret>', audio_features, name='display_audio_features'),
|
||||
path('api/audio_features/<str:audio_feature>/<str:user_secret>',
|
||||
get_audio_feature_data, name='get_audio_feature_data'),
|
||||
path('admin/', admin.site.urls),
|
||||
path('login/', include('login.urls')),
|
||||
path('api/', include('api.urls')),
|
||||
path('graphs/', include('graphs.urls')),
|
||||
]
|
||||
|
||||
@@ -1,281 +0,0 @@
|
||||
# imports {{{ #
|
||||
import requests
|
||||
import math
|
||||
import pprint
|
||||
|
||||
from .models import *
|
||||
from django.db.models import Count, Q, F
|
||||
from django.http import JsonResponse
|
||||
from django.core import serializers
|
||||
import json
|
||||
|
||||
# }}} imports #
|
||||
|
||||
USER_TRACKS_LIMIT = 50
|
||||
ARTIST_LIMIT = 50
|
||||
FEATURES_LIMIT = 100
|
||||
# ARTIST_LIMIT = 25
|
||||
# FEATURES_LIMIT = 25
|
||||
|
||||
# parse_library {{{ #
|
||||
|
||||
def parse_library(headers, tracks, user):
|
||||
"""Scans user's library for certain number of tracks and store the information in a database
|
||||
|
||||
:headers: For API call.
|
||||
:tracks: Number of tracks to get from user's library.
|
||||
:user: a User object representing the user whose library we are parsing
|
||||
|
||||
:returns: None
|
||||
|
||||
"""
|
||||
# TODO: implement importing entire library with 0 as tracks param
|
||||
# keeps track of point to get songs from
|
||||
offset = 0
|
||||
payload = {'limit': str(USER_TRACKS_LIMIT)}
|
||||
artist_genre_queue = []
|
||||
features_queue = []
|
||||
|
||||
# iterate until hit requested num of tracks
|
||||
for i in range(0, tracks, USER_TRACKS_LIMIT):
|
||||
payload['offset'] = str(offset)
|
||||
saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks',
|
||||
headers=headers,
|
||||
params=payload).json()
|
||||
|
||||
for track_dict in saved_tracks_response['items']:
|
||||
# add artists {{{ #
|
||||
|
||||
# update artist info before track so that Track object can reference
|
||||
# Artist object
|
||||
track_artists = []
|
||||
for artist_dict in track_dict['track']['artists']:
|
||||
artist_obj, artist_created = Artist.objects.get_or_create(
|
||||
artist_id=artist_dict['id'],
|
||||
name=artist_dict['name'],)
|
||||
# only add/tally up artist genres if new
|
||||
if artist_created:
|
||||
artist_genre_queue.append(artist_obj)
|
||||
if len(artist_genre_queue) == ARTIST_LIMIT:
|
||||
add_artist_genres(headers, artist_genre_queue)
|
||||
artist_genre_queue = []
|
||||
track_artists.append(artist_obj)
|
||||
|
||||
# }}} add artists #
|
||||
|
||||
# TODO: fix this, don't need any more
|
||||
top_genre = ""
|
||||
track_obj, track_created = save_track_obj(track_dict['track'],
|
||||
track_artists, top_genre, user)
|
||||
|
||||
# add audio features {{{ #
|
||||
|
||||
# if a new track is not created, the associated audio feature does
|
||||
# not need to be created again
|
||||
if track_created:
|
||||
features_queue.append(track_obj)
|
||||
if len(features_queue) == FEATURES_LIMIT:
|
||||
get_audio_features(headers, features_queue)
|
||||
features_queue = []
|
||||
|
||||
# }}} add audio features #
|
||||
|
||||
# temporary console logging
|
||||
print("#{}-{}: {} - {}".format(offset + 1,
|
||||
offset + USER_TRACKS_LIMIT,
|
||||
track_obj.artists.first(),
|
||||
track_obj.name))
|
||||
|
||||
# calculates num_songs with offset + songs retrieved
|
||||
offset += USER_TRACKS_LIMIT
|
||||
|
||||
# clean-up {{{ #
|
||||
|
||||
# update remaining artists without genres and songs without features if
|
||||
# there are any
|
||||
if len(artist_genre_queue) > 0:
|
||||
add_artist_genres(headers, artist_genre_queue)
|
||||
if len(features_queue) > 0:
|
||||
get_audio_features(headers, features_queue)
|
||||
|
||||
# }}} clean-up #
|
||||
|
||||
update_track_genres(user)
|
||||
|
||||
# }}} parse_library #
|
||||
|
||||
# update_track_genres {{{ #
|
||||
|
||||
def update_track_genres(user):
|
||||
"""Updates user's tracks with the most common genre associated with the
|
||||
songs' artist(s).
|
||||
|
||||
:user: User object who's tracks are being updated.
|
||||
|
||||
:returns: None
|
||||
|
||||
"""
|
||||
user_tracks = Track.objects.filter(users__exact=user)
|
||||
for track in user_tracks:
|
||||
# just using this variable to save another call to db
|
||||
track_artists = track.artists.all()
|
||||
# set genres to first artist's genres then find intersection with others
|
||||
shared_genres = track_artists.first().genres.all()
|
||||
for artist in track_artists:
|
||||
shared_genres = shared_genres.intersection(artist.genres.all())
|
||||
shared_genres = shared_genres.order_by('-num_songs')
|
||||
|
||||
undefined_genre_obj = Genre.objects.get(name="undefined")
|
||||
most_common_genre = shared_genres.first() if shared_genres.first() is \
|
||||
not undefined_genre_obj else shared_genres[1]
|
||||
track.genre = most_common_genre if most_common_genre is not None \
|
||||
else undefined_genre_obj
|
||||
track.save()
|
||||
# print(track.name, track.genre)
|
||||
|
||||
# }}} update_track_genres #
|
||||
|
||||
# save_track_obj {{{ #
|
||||
|
||||
def save_track_obj(track_dict, artists, top_genre, user):
|
||||
"""Make an entry in the database for this track if it doesn't exist already.
|
||||
|
||||
:track_dict: dictionary from the API call containing track information.
|
||||
:artists: artists of the song, passed in as a list of Artist objects.
|
||||
:top_genre: top genre associated with this track (see get_top_genre).
|
||||
:user: User object for which this Track is to be associated with.
|
||||
|
||||
:returns: (The created/retrieved Track object, created)
|
||||
|
||||
"""
|
||||
track_query = Track.objects.filter(track_id__exact=track_dict['id'])
|
||||
if len(track_query) != 0:
|
||||
return track_query[0], False
|
||||
else:
|
||||
new_track = Track.objects.create(
|
||||
track_id=track_dict['id'],
|
||||
year=track_dict['album']['release_date'].split('-')[0],
|
||||
popularity=int(track_dict['popularity']),
|
||||
runtime=int(float(track_dict['duration_ms']) / 1000),
|
||||
name=track_dict['name'],
|
||||
# genre=top_genre,
|
||||
)
|
||||
|
||||
# have to add artists and user after saving object since track needs to
|
||||
# have ID before filling in m2m field
|
||||
for artist in artists:
|
||||
new_track.artists.add(artist)
|
||||
new_track.users.add(user)
|
||||
new_track.save()
|
||||
return new_track, True
|
||||
|
||||
# }}} save_track_obj #
|
||||
|
||||
# get_audio_features {{{ #
|
||||
|
||||
def get_audio_features(headers, track_objs):
|
||||
"""Creates and saves a new AudioFeatures objects for the respective
|
||||
track_objs. track_objs should contain the API limit for a single call
|
||||
(FEATURES_LIMIT) for maximum efficiency.
|
||||
|
||||
:headers: headers containing the API token
|
||||
:track_objs: Track objects to associate with the new AudioFeatures object
|
||||
|
||||
:returns: None
|
||||
"""
|
||||
track_ids = str.join(",", [track_obj.track_id for track_obj in track_objs])
|
||||
params = {'ids': track_ids}
|
||||
features_response = requests.get("https://api.spotify.com/v1/audio-features",
|
||||
headers=headers,params=params).json()['audio_features']
|
||||
# pprint.pprint(features_response)
|
||||
|
||||
useless_keys = [ "key", "mode", "type", "liveness", "id", "uri", "track_href", "analysis_url", "time_signature", ]
|
||||
for i in range(len(track_objs)):
|
||||
if features_response[i] is not None:
|
||||
# Data that we don't need
|
||||
cur_features_obj = AudioFeatures()
|
||||
cur_features_obj.track = track_objs[i]
|
||||
for key, val in features_response[i].items():
|
||||
if key not in useless_keys:
|
||||
setattr(cur_features_obj, key, val)
|
||||
cur_features_obj.save()
|
||||
|
||||
# }}} get_audio_features #
|
||||
|
||||
def process_artist_genre(genre_name, artist_obj):
|
||||
"""Increase count for correspoding Genre object to genre_name and add that
|
||||
Genre to artist_obj.
|
||||
|
||||
:genre_name: Name of genre.
|
||||
:artist_obj: Artist object to add Genre object to.
|
||||
:returns: None
|
||||
|
||||
"""
|
||||
genre_obj, created = Genre.objects.get_or_create(name=genre_name,
|
||||
defaults={'num_songs':1})
|
||||
if not created:
|
||||
genre_obj.num_songs = F('num_songs') + 1
|
||||
genre_obj.save()
|
||||
artist_obj.genres.add(genre_obj)
|
||||
artist_obj.save()
|
||||
|
||||
# add_artist_genres {{{ #
|
||||
|
||||
def add_artist_genres(headers, artist_objs):
|
||||
"""Adds genres to artist_objs and increases the count the respective Genre
|
||||
object. artist_objs should contain the API limit for a single call
|
||||
(ARTIST_LIMIT) for maximum efficiency.
|
||||
|
||||
:headers: For making the API call.
|
||||
:artist_objs: List of Artist objects for which to add/tally up genres for.
|
||||
|
||||
:returns: None
|
||||
|
||||
"""
|
||||
artist_ids = str.join(",", [artist_obj.artist_id for artist_obj in artist_objs])
|
||||
params = {'ids': artist_ids}
|
||||
artists_response = requests.get('https://api.spotify.com/v1/artists/',
|
||||
headers=headers, params=params).json()['artists']
|
||||
# pprint.pprint(artists_response)
|
||||
for i in range(len(artist_objs)):
|
||||
if len(artists_response[i]['genres']) == 0:
|
||||
process_artist_genre("undefined", artist_objs[i])
|
||||
else:
|
||||
for genre in artists_response[i]['genres']:
|
||||
process_artist_genre(genre, artist_objs[i])
|
||||
|
||||
# }}} add_artist_genres #
|
||||
|
||||
# get_artists_in_genre {{{ #
|
||||
|
||||
def get_artists_in_genre(user, genre, max_songs):
|
||||
"""Return count of artists in genre.
|
||||
|
||||
:user: User object to return data for.
|
||||
:genre: genre to count artists for.
|
||||
:max_songs: max total songs to include to prevent overflow due to having
|
||||
multiple artists on each track.
|
||||
|
||||
:returns: dict of artists in the genre along with the number of songs they
|
||||
have.
|
||||
"""
|
||||
genre_obj = Genre.objects.get(name=genre)
|
||||
artist_counts = (Artist.objects.filter(track__users=user)
|
||||
.filter(genres=genre_obj)
|
||||
.annotate(num_songs=Count('track', distinct=True))
|
||||
.order_by('-num_songs')
|
||||
)
|
||||
processed_artist_counts = {}
|
||||
songs_added = 0
|
||||
for artist in artist_counts:
|
||||
# hacky way to not have total count overflow due to there being multiple
|
||||
# artists on a track
|
||||
if songs_added + artist.num_songs <= max_songs:
|
||||
processed_artist_counts[artist.name] = artist.num_songs
|
||||
songs_added += artist.num_songs
|
||||
# processed_artist_counts = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts]
|
||||
# processed_artist_counts = {artist.name: artist.num_songs for artist in artist_counts}
|
||||
# pprint.pprint(processed_artist_counts)
|
||||
return processed_artist_counts
|
||||
|
||||
# }}} get_artists_in_genre #
|
||||
@@ -1,288 +0,0 @@
|
||||
# imports {{{ #
|
||||
|
||||
import math
|
||||
import random
|
||||
import requests
|
||||
import os
|
||||
import urllib
|
||||
import secrets
|
||||
import pprint
|
||||
import string
|
||||
from datetime import datetime
|
||||
|
||||
from django.shortcuts import render, redirect
|
||||
from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
|
||||
from django.db.models import Count, Q
|
||||
from .utils import parse_library, get_artists_in_genre, update_track_genres
|
||||
from .models import User, Track, AudioFeatures, Artist
|
||||
|
||||
# }}} imports #
|
||||
|
||||
TIME_FORMAT = '%Y-%m-%d-%H-%M-%S'
|
||||
TRACKS_TO_QUERY = 200
|
||||
|
||||
# generate_random_string {{{ #
|
||||
|
||||
|
||||
def generate_random_string(length):
|
||||
"""Generates a random string of a certain length
|
||||
|
||||
Args:
|
||||
length: the desired length of the randomized string
|
||||
|
||||
Returns:
|
||||
A random string
|
||||
"""
|
||||
all_chars = string.ascii_letters + string.digits
|
||||
rand_str = "".join(random.choice(all_chars) for _ in range(length))
|
||||
|
||||
return rand_str
|
||||
|
||||
# }}} generate_random_string #
|
||||
|
||||
# token_expired {{{ #
|
||||
|
||||
def token_expired(token_obtained_at, valid_for):
|
||||
"""Returns True if token expired, False if otherwise
|
||||
|
||||
Args:
|
||||
token_obtained_at: datetime object representing the date and time when the token was obtained
|
||||
valid_for: the time duration for which the token is valid, in seconds
|
||||
"""
|
||||
time_elapsed = (datetime.today() - token_obtained_at).total_seconds()
|
||||
return time_elapsed >= valid_for
|
||||
|
||||
# }}} token_expired #
|
||||
|
||||
# index {{{ #
|
||||
|
||||
# Create your views here.
|
||||
def index(request):
|
||||
return render(request, 'spotifyvis/index.html')
|
||||
|
||||
# }}} index #
|
||||
|
||||
# login {{{ #
|
||||
|
||||
# uses Authorization Code flow
|
||||
def login(request):
|
||||
# use a randomly generated state string to prevent cross-site request forgery attacks
|
||||
state_str = generate_random_string(16)
|
||||
request.session['state_string'] = state_str
|
||||
|
||||
payload = {
|
||||
'client_id': os.environ['SPOTIFY_CLIENT_ID'],
|
||||
'response_type': 'code',
|
||||
'redirect_uri': 'http://localhost:8000/callback',
|
||||
'state': state_str,
|
||||
'scope': 'user-library-read',
|
||||
'show_dialog': False
|
||||
}
|
||||
|
||||
params = urllib.parse.urlencode(payload) # turn the payload dict into a query string
|
||||
authorize_url = "https://accounts.spotify.com/authorize/?{}".format(params)
|
||||
return redirect(authorize_url)
|
||||
|
||||
# }}} login #
|
||||
|
||||
# callback {{{ #
|
||||
|
||||
def callback(request):
|
||||
# Attempt to retrieve the authorization code from the query string
|
||||
try:
|
||||
code = request.GET['code']
|
||||
except KeyError:
|
||||
return HttpResponseBadRequest("<h1>Problem with login</h1>")
|
||||
|
||||
payload = {
|
||||
'grant_type': 'authorization_code',
|
||||
'code': code,
|
||||
'redirect_uri': 'http://localhost:8000/callback',
|
||||
'client_id': os.environ['SPOTIFY_CLIENT_ID'],
|
||||
'client_secret': os.environ['SPOTIFY_CLIENT_SECRET'],
|
||||
}
|
||||
|
||||
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)
|
||||
request.session['access_token'] = response['access_token']
|
||||
request.session['refresh_token'] = response['refresh_token']
|
||||
request.session['valid_for'] = response['expires_in']
|
||||
# print(response)
|
||||
|
||||
return redirect('user_data')
|
||||
|
||||
# }}} callback #
|
||||
|
||||
# user_data {{{ #
|
||||
|
||||
|
||||
def user_data(request):
|
||||
|
||||
# get user token {{{ #
|
||||
|
||||
token_obtained_at = datetime.strptime(request.session['token_obtained_at'], TIME_FORMAT)
|
||||
valid_for = int(request.session['valid_for'])
|
||||
|
||||
if token_expired(token_obtained_at, valid_for):
|
||||
req_body = {
|
||||
'grant_type': 'refresh_token',
|
||||
'refresh_token': request.session['refresh_token'],
|
||||
'client_id': os.environ['SPOTIFY_CLIENT_ID'],
|
||||
'client_secret': os.environ['SPOTIFY_CLIENT_SECRET']
|
||||
}
|
||||
|
||||
refresh_token_response = requests.post('https://accounts.spotify.com/api/token', data=req_body).json()
|
||||
request.session['access_token'] = refresh_token_response['access_token']
|
||||
request.session['valid_for'] = refresh_token_response['expires_in']
|
||||
|
||||
# }}} get user token #
|
||||
|
||||
auth_token_str = "Bearer " + request.session['access_token']
|
||||
headers = {
|
||||
'Authorization': auth_token_str
|
||||
}
|
||||
|
||||
user_data_response = requests.get('https://api.spotify.com/v1/me', headers = headers).json()
|
||||
# store the user_id so it may be used to create model
|
||||
request.session['user_id'] = user_data_response['id']
|
||||
|
||||
# create user obj {{{ #
|
||||
|
||||
try:
|
||||
user = User.objects.get(user_id=user_data_response['id'])
|
||||
except User.DoesNotExist:
|
||||
# Python docs recommends 32 bytes of randomness against brute force attacks
|
||||
user = User(user_id=user_data_response['id'], user_secret=secrets.token_urlsafe(32))
|
||||
request.session['user_secret'] = user.user_secret
|
||||
user.save()
|
||||
|
||||
# }}} create user obj #
|
||||
|
||||
context = {
|
||||
'user_id': user.user_id,
|
||||
'user_secret': user.user_secret,
|
||||
}
|
||||
|
||||
parse_library(headers, TRACKS_TO_QUERY, user)
|
||||
return render(request, 'spotifyvis/logged_in.html', context)
|
||||
|
||||
# }}} user_data #
|
||||
|
||||
def admin_graphs(request):
|
||||
"""TODO
|
||||
"""
|
||||
user_id = "polarbier"
|
||||
# user_id = "chrisshyi13"
|
||||
user_obj = User.objects.get(user_id=user_id)
|
||||
context = {
|
||||
'user_id': user_id,
|
||||
'user_secret': user_obj.user_secret,
|
||||
}
|
||||
update_track_genres(user_obj)
|
||||
return render(request, 'spotifyvis/logged_in.html', context)
|
||||
|
||||
|
||||
def artist_data(request, user_secret):
|
||||
"""Renders the artist data graph display page
|
||||
|
||||
:param request: the HTTP request
|
||||
:param user_secret: the user secret used for identification
|
||||
:return: render the artist data graph display page
|
||||
"""
|
||||
user = User.objects.get(user_secret=user_secret)
|
||||
context = {
|
||||
'user_id': user.user_id,
|
||||
'user_secret': user_secret,
|
||||
}
|
||||
return render(request, "spotifyvis/artist_graph.html", context)
|
||||
|
||||
# get_artist_data {{{ #
|
||||
|
||||
|
||||
def get_artist_data(request, user_secret):
|
||||
"""Returns artist data as a JSON serialized list of dictionaries
|
||||
The (key, value) pairs are (artist name, song count for said artist)
|
||||
|
||||
:param request: the HTTP request
|
||||
:param user_secret: the user secret used for identification
|
||||
:return: a JsonResponse
|
||||
"""
|
||||
user = User.objects.get(user_secret=user_secret)
|
||||
artist_counts = Artist.objects.annotate(num_songs=Count('track',
|
||||
filter=Q(track__users=user)))
|
||||
processed_artist_counts = [{'name': artist.name,
|
||||
'num_songs': artist.num_songs} for artist in artist_counts]
|
||||
return JsonResponse(data=processed_artist_counts, safe=False)
|
||||
|
||||
# }}} get_artist_data #
|
||||
|
||||
|
||||
def display_genre_graph(request, user_secret):
|
||||
user = User.objects.get(user_secret=user_secret)
|
||||
context = {
|
||||
'user_secret': user_secret,
|
||||
}
|
||||
return render(request, "spotifyvis/genre_graph.html", context)
|
||||
|
||||
|
||||
def audio_features(request, user_secret):
|
||||
"""Renders the audio features page
|
||||
|
||||
:param request: the HTTP request
|
||||
:param user_secret: user secret used for identification
|
||||
:return: renders the audio features page
|
||||
"""
|
||||
user = User.objects.get(user_secret=user_secret)
|
||||
context = {
|
||||
'user_id': user.user_id,
|
||||
'user_secret': user_secret,
|
||||
}
|
||||
return render(request, "spotifyvis/audio_features.html", context)
|
||||
|
||||
# get_audio_feature_data {{{ #
|
||||
|
||||
def get_audio_feature_data(request, audio_feature, user_secret):
|
||||
"""Returns all data points for a given audio feature
|
||||
|
||||
Args:
|
||||
request: the HTTP request
|
||||
audio_feature: The audio feature to be queried
|
||||
user_secret: client secret, used to identify the user
|
||||
"""
|
||||
user = User.objects.get(user_secret=user_secret)
|
||||
user_tracks = Track.objects.filter(users=user)
|
||||
response_payload = {
|
||||
'data_points': [],
|
||||
}
|
||||
for track in user_tracks:
|
||||
try:
|
||||
audio_feature_obj = AudioFeatures.objects.get(track=track)
|
||||
response_payload['data_points'].append(getattr(audio_feature_obj, audio_feature))
|
||||
except AudioFeatures.DoesNotExist:
|
||||
continue
|
||||
return JsonResponse(response_payload)
|
||||
|
||||
# }}} get_audio_feature_data #
|
||||
|
||||
# get_genre_data {{{ #
|
||||
|
||||
def get_genre_data(request, user_secret):
|
||||
"""Return genre data needed to create the graph user.
|
||||
TODO
|
||||
"""
|
||||
user = User.objects.get(user_secret=user_secret)
|
||||
genre_counts = (Track.objects.filter(users__exact=user)
|
||||
.values('genre')
|
||||
.order_by('genre')
|
||||
.annotate(num_songs=Count('genre'))
|
||||
)
|
||||
for genre_dict in genre_counts:
|
||||
genre_dict['artists'] = get_artists_in_genre(user, genre_dict['genre'],
|
||||
genre_dict['num_songs'])
|
||||
print("*** Genre Breakdown ***")
|
||||
pprint.pprint(list(genre_counts))
|
||||
return JsonResponse(data=list(genre_counts), safe=False)
|
||||
|
||||
# }}} get_genre_data #
|
||||
16
spotifyvis/wsgi.py
Normal file
16
spotifyvis/wsgi.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
WSGI config for musicdata project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "spotifyvis.settings")
|
||||
|
||||
application = get_wsgi_application()
|
||||
Reference in New Issue
Block a user