Include artist breakdown in get_genre_data
This commit is contained in:
@@ -1,31 +1,26 @@
|
|||||||
<!DOCTYPE html>
|
<!DOC
|
||||||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
|
<!--[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 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
|
||||||
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
|
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
|
||||||
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
|
<!--[if gt IE 8]><!-->
|
||||||
<head>
|
<html class="no-js"> <!--<![endif]-->
|
||||||
<meta charset="utf-8">
|
<head>
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
<meta charset="utf-8">
|
||||||
<title>Test DB Page</title>
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
<meta name="description" content="">
|
<title>Test DB Page</title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="description" content="">
|
||||||
</head>
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<body>
|
</head>
|
||||||
<script src="https://d3js.org/d3.v5.min.js"></script>
|
<body>
|
||||||
<script>
|
<script src="https://d3js.org/d3.v5.min.js"></script>
|
||||||
/* console.log("artists");
|
<svg width="960" height="500"></svg>
|
||||||
d3.json("{% url "get_artist_data" user_secret %}").then(function(data) {
|
<script>
|
||||||
data.forEach(function(d) {
|
console.log("genres");
|
||||||
console.log(d.name, d.num_songs);
|
d3.json("{% url "get_genre_data" user_secret %}").then(function(data) {
|
||||||
});
|
data.forEach(function(d) {
|
||||||
}); */
|
console.log(d.genre, d.num_songs);
|
||||||
|
});
|
||||||
console.log("genres");
|
});
|
||||||
d3.json("{% url "get_genre_data" user_secret %}").then(function(data) {
|
</script>
|
||||||
data.forEach(function(d) {
|
</body>
|
||||||
console.log(d.genre, d.num_songs);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import math
|
|||||||
import pprint
|
import pprint
|
||||||
|
|
||||||
from .models import Artist, User, Track, AudioFeatures
|
from .models import Artist, User, Track, AudioFeatures
|
||||||
from django.db.models import Count
|
from django.db.models import Count, Q
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.core import serializers
|
from django.core import serializers
|
||||||
import json
|
import json
|
||||||
@@ -317,7 +317,7 @@ def update_artist_genre(headers, artist_obj):
|
|||||||
|
|
||||||
# }}} #
|
# }}} #
|
||||||
|
|
||||||
# {{{ #
|
# get_top_genre {{{ #
|
||||||
|
|
||||||
def get_top_genre(headers, top_artist_id):
|
def get_top_genre(headers, top_artist_id):
|
||||||
"""Updates the top genre for a track by querying the Spotify API
|
"""Updates the top genre for a track by querying the Spotify API
|
||||||
@@ -339,7 +339,6 @@ def get_top_genre(headers, top_artist_id):
|
|||||||
|
|
||||||
# process_library_stats {{{ #
|
# process_library_stats {{{ #
|
||||||
|
|
||||||
|
|
||||||
def process_library_stats(library_stats):
|
def process_library_stats(library_stats):
|
||||||
"""Processes library_stats into format more suitable for D3 consumption
|
"""Processes library_stats into format more suitable for D3 consumption
|
||||||
|
|
||||||
@@ -393,3 +392,22 @@ def process_library_stats(library_stats):
|
|||||||
|
|
||||||
# }}} process_library_stats #
|
# }}} process_library_stats #
|
||||||
|
|
||||||
|
# get_artists_in_genre {{{ #
|
||||||
|
|
||||||
|
def get_artists_in_genre(user, genre):
|
||||||
|
"""Return count of artists in genre.
|
||||||
|
|
||||||
|
:genre: genre to count artists for.
|
||||||
|
|
||||||
|
:returns: dict of artists in the genre along with the number of songs they
|
||||||
|
have.
|
||||||
|
"""
|
||||||
|
artist_counts = (Artist.objects.filter(track__users=user)
|
||||||
|
.filter(track__genre=genre)
|
||||||
|
.annotate(num_songs=Count('track', distinct=True))
|
||||||
|
)
|
||||||
|
processed_artist_counts = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts]
|
||||||
|
# pprint.pprint(processed_artist_counts)
|
||||||
|
return processed_artist_counts
|
||||||
|
|
||||||
|
# }}} get_artists_in_genre #
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ from datetime import datetime
|
|||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import render, redirect
|
||||||
from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
|
from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
|
||||||
from django.db.models import Count, Q
|
from django.db.models import Count, Q
|
||||||
from .utils import parse_library, process_library_stats
|
from .utils import parse_library, process_library_stats, get_artists_in_genre
|
||||||
from .models import User, Track, AudioFeatures, Artist
|
from .models import User, Track, AudioFeatures, Artist
|
||||||
|
|
||||||
# }}} imports #
|
# }}} imports #
|
||||||
@@ -165,6 +165,7 @@ def test_db(request):
|
|||||||
"""TODO
|
"""TODO
|
||||||
"""
|
"""
|
||||||
user_id = "polarbier"
|
user_id = "polarbier"
|
||||||
|
# user_id = "35kxo00qqo9pd1comj6ylxjq7"
|
||||||
context = {
|
context = {
|
||||||
'user_secret': User.objects.get(user_id=user_id).user_secret,
|
'user_secret': User.objects.get(user_id=user_id).user_secret,
|
||||||
}
|
}
|
||||||
@@ -180,8 +181,9 @@ def get_artist_data(request, user_secret):
|
|||||||
user = User.objects.get(user_id=user_secret)
|
user = User.objects.get(user_id=user_secret)
|
||||||
artist_counts = Artist.objects.annotate(num_songs=Count('track',
|
artist_counts = Artist.objects.annotate(num_songs=Count('track',
|
||||||
filter=Q(track__users=user)))
|
filter=Q(track__users=user)))
|
||||||
processed_artist_data = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts]
|
processed_artist_counts = [{'name': artist.name,
|
||||||
return JsonResponse(data=processed_artist_data, safe=False)
|
'num_songs': artist.num_songs} for artist in artist_counts]
|
||||||
|
return JsonResponse(data=processed_artist_counts, safe=False)
|
||||||
|
|
||||||
# }}} get_artist_data #
|
# }}} get_artist_data #
|
||||||
|
|
||||||
@@ -214,12 +216,14 @@ def get_genre_data(request, user_secret):
|
|||||||
TODO
|
TODO
|
||||||
"""
|
"""
|
||||||
user = User.objects.get(user_secret=user_secret)
|
user = User.objects.get(user_secret=user_secret)
|
||||||
genre_counts = (Track.objects.filter(users=user)
|
genre_counts = (Track.objects.filter(users__exact=user)
|
||||||
.values('genre')
|
.values('genre')
|
||||||
.order_by('genre')
|
.order_by('genre')
|
||||||
.annotate(num_songs=Count('genre'))
|
.annotate(num_songs=Count('genre'))
|
||||||
)
|
)
|
||||||
# pprint.pprint(genre_counts)
|
for genre_dict in genre_counts:
|
||||||
|
genre_dict['artists'] = get_artists_in_genre(user, genre_dict['genre'])
|
||||||
|
pprint.pprint(list(genre_counts))
|
||||||
return JsonResponse(data=list(genre_counts), safe=False)
|
return JsonResponse(data=list(genre_counts), safe=False)
|
||||||
|
|
||||||
# }}} get_genre_data #
|
# }}} get_genre_data #
|
||||||
|
|||||||
Reference in New Issue
Block a user