Browse Source

Added most common genre for tracks with 1 artist

Still have to find shared genres for songs with multiple artists (see #34).
master
Kevin Mok 7 years ago
parent
commit
709ed9b491
  1. 22
      spotifyvis/utils.py
  2. 6
      spotifyvis/views.py

22
spotifyvis/utils.py

@ -76,9 +76,27 @@ def parse_library(headers, tracks, user):
# calculates num_songs with offset + songs retrieved # calculates num_songs with offset + songs retrieved
offset += limit offset += limit
# pprint.pprint(library_stats) # pprint.pprint(library_stats)
update_track_genres(user)
# }}} parse_library # # }}} parse_library #
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:
track_artists = list(track.artists.all())
if len(track_artists) == 1:
track.genre = track_artists[0].genres.all().order_by('-num_songs').first()
track.save()
# print(track_artists, track.genre)
# save_track_obj {{{ # # save_track_obj {{{ #
def save_track_obj(track_dict, artists, top_genre, user): def save_track_obj(track_dict, artists, top_genre, user):
@ -341,6 +359,8 @@ def get_top_genre(headers, top_artist_id):
# }}} # # }}} #
# add_artist_genres {{{ #
def add_artist_genres(headers, artist_obj): def add_artist_genres(headers, artist_obj):
"""Adds genres to artist_obj and increases the count the respective Genre """Adds genres to artist_obj and increases the count the respective Genre
object. Should be called when a new Artist object is created. object. Should be called when a new Artist object is created.
@ -362,6 +382,8 @@ def add_artist_genres(headers, artist_obj):
artist_obj.genres.add(genre_obj) artist_obj.genres.add(genre_obj)
artist_obj.save() artist_obj.save()
# }}} add_artist_genres #
# process_library_stats {{{ # # process_library_stats {{{ #
def process_library_stats(library_stats): def process_library_stats(library_stats):

6
spotifyvis/views.py

@ -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, get_artists_in_genre
from .utils import parse_library, process_library_stats, get_artists_in_genre, update_track_genres
from .models import User, Track, AudioFeatures, Artist from .models import User, Track, AudioFeatures, Artist
# }}} imports # # }}} imports #
@ -165,10 +165,12 @@ def test_db(request):
"""TODO """TODO
""" """
user_id = "polarbier" user_id = "polarbier"
user_obj = User.objects.get(user_id=user_id)
# user_id = "35kxo00qqo9pd1comj6ylxjq7" # user_id = "35kxo00qqo9pd1comj6ylxjq7"
context = { context = {
'user_secret': User.objects.get(user_id=user_id).user_secret,
'user_secret': user_obj.user_secret,
} }
update_track_genres(user_obj)
return render(request, 'spotifyvis/test_db.html', context) return render(request, 'spotifyvis/test_db.html', context)
# }}} test_db # # }}} test_db #

Loading…
Cancel
Save