Graphs and tables for your Spotify account.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
6.1 KiB

  1. # imports {{{ #
  2. import math
  3. import random
  4. import requests
  5. import urllib
  6. import secrets
  7. import pprint
  8. import string
  9. from django.shortcuts import render, redirect
  10. from django.http import JsonResponse
  11. from django.db.models import Count, Q
  12. from .utils import *
  13. from .models import *
  14. from login.models import User
  15. from login.utils import get_user_context
  16. # }}} imports #
  17. USER_TRACKS_LIMIT = 50
  18. ARTIST_LIMIT = 50
  19. FEATURES_LIMIT = 100
  20. # ARTIST_LIMIT = 25
  21. # FEATURES_LIMIT = 25
  22. TRACKS_TO_QUERY = 100
  23. console_logging = True
  24. # parse_library {{{ #
  25. def parse_library(request, user_secret):
  26. """Scans user's library for num_tracks and store the information in a
  27. database.
  28. :user_secret: secret for User object who's library is being scanned.
  29. :returns: None
  30. """
  31. offset = 0
  32. payload = {'limit': str(USER_TRACKS_LIMIT)}
  33. artist_genre_queue = []
  34. features_queue = []
  35. user_obj = User.objects.get(secret=user_secret)
  36. user_headers = get_user_header(user_obj)
  37. # create this obj so loop runs at least once
  38. saved_tracks_response = [0]
  39. # scan until reach num_tracks or no tracks left if scanning entire library
  40. while (TRACKS_TO_QUERY == 0 or offset < TRACKS_TO_QUERY) and len(saved_tracks_response) > 0:
  41. payload['offset'] = str(offset)
  42. saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks',
  43. headers=user_headers,
  44. params=payload).json()['items']
  45. if console_logging:
  46. tracks_processed = 0
  47. for track_dict in saved_tracks_response:
  48. # add artists {{{ #
  49. # update artist info before track so that Track object can reference
  50. # Artist object
  51. track_artists = []
  52. for artist_dict in track_dict['track']['artists']:
  53. artist_obj, artist_created = Artist.objects.get_or_create(
  54. id=artist_dict['id'],
  55. name=artist_dict['name'],)
  56. # only add/tally up artist genres if new
  57. if artist_created:
  58. artist_genre_queue.append(artist_obj)
  59. if len(artist_genre_queue) == ARTIST_LIMIT:
  60. add_artist_genres(user_headers, artist_genre_queue)
  61. artist_genre_queue = []
  62. track_artists.append(artist_obj)
  63. # }}} add artists #
  64. track_obj, track_created = save_track_obj(track_dict['track'],
  65. track_artists, user_obj)
  66. # add audio features {{{ #
  67. # if a new track is not created, the associated audio feature does
  68. # not need to be created again
  69. if track_created:
  70. features_queue.append(track_obj)
  71. if len(features_queue) == FEATURES_LIMIT:
  72. get_audio_features(user_headers, features_queue)
  73. features_queue = []
  74. # }}} add audio features #
  75. if console_logging:
  76. tracks_processed += 1
  77. print("Added track #{}: {} - {}".format(
  78. offset + tracks_processed,
  79. track_obj.artists.first(),
  80. track_obj.name,
  81. ))
  82. # calculates num_songs with offset + songs retrieved
  83. offset += USER_TRACKS_LIMIT
  84. # clean-up {{{ #
  85. # update remaining artists without genres and songs without features if
  86. # there are any
  87. if len(artist_genre_queue) > 0:
  88. add_artist_genres(user_headers, artist_genre_queue)
  89. if len(features_queue) > 0:
  90. get_audio_features(user_headers, features_queue)
  91. # }}} clean-up #
  92. update_track_genres(user_obj)
  93. return render(request, 'graphs/logged_in.html', get_user_context(user_obj))
  94. # }}} parse_library #
  95. # get_artist_data {{{ #
  96. def get_artist_data(request, user_secret):
  97. """Returns artist data as a JSON serialized list of dictionaries
  98. The (key, value) pairs are (artist name, song count for said artist)
  99. :param request: the HTTP request
  100. :param user_secret: the user secret used for identification
  101. :return: a JsonResponse
  102. """
  103. user = User.objects.get(secret=user_secret)
  104. artist_counts = Artist.objects.annotate(num_songs=Count('track',
  105. filter=Q(track__users=user)))
  106. processed_artist_counts = [{'name': artist.name, 'num_songs': artist.num_songs}
  107. for artist in artist_counts]
  108. pprint.pprint(processed_artist_counts)
  109. return JsonResponse(data=processed_artist_counts, safe=False)
  110. # }}} get_artist_data #
  111. # get_audio_feature_data {{{ #
  112. def get_audio_feature_data(request, audio_feature, user_secret):
  113. """Returns all data points for a given audio feature
  114. Args:
  115. request: the HTTP request
  116. audio_feature: The audio feature to be queried
  117. user_secret: client secret, used to identify the user
  118. """
  119. user = User.objects.get(secret=user_secret)
  120. user_tracks = Track.objects.filter(users=user)
  121. response_payload = {
  122. 'data_points': [],
  123. }
  124. for track in user_tracks:
  125. try:
  126. audio_feature_obj = AudioFeatures.objects.get(track=track)
  127. response_payload['data_points'].append(getattr(audio_feature_obj, audio_feature))
  128. except AudioFeatures.DoesNotExist:
  129. continue
  130. return JsonResponse(response_payload)
  131. # }}} get_audio_feature_data #
  132. # get_genre_data {{{ #
  133. def get_genre_data(request, user_secret):
  134. """Return genre data needed to create the graph user.
  135. TODO
  136. """
  137. user = User.objects.get(secret=user_secret)
  138. genre_counts = (Track.objects.filter(users__exact=user)
  139. .values('genre')
  140. .order_by('genre')
  141. .annotate(num_songs=Count('genre'))
  142. )
  143. for genre_dict in genre_counts:
  144. genre_dict['artists'] = get_artists_in_genre(user, genre_dict['genre'],
  145. genre_dict['num_songs'])
  146. print("*** Genre Breakdown ***")
  147. pprint.pprint(list(genre_counts))
  148. return JsonResponse(data=list(genre_counts), safe=False)
  149. # }}} get_genre_data #