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.

286 lines
9.5 KiB

  1. # imports {{{ #
  2. import math
  3. import random
  4. import requests
  5. import urllib
  6. import secrets
  7. import string
  8. import csv
  9. from django.shortcuts import render, redirect
  10. from django.http import JsonResponse
  11. from django.db.models import Count, Q, Max
  12. from django.core.files import File
  13. from .utils import *
  14. from .models import *
  15. from login.models import User
  16. from login.utils import get_user_context
  17. from dateutil.parser import parse
  18. from pprint import pprint
  19. from login.models import HistoryUpload
  20. # }}} imports #
  21. # constants {{{ #
  22. USER_TRACKS_LIMIT = 50
  23. TRACKS_LIMIT = 50
  24. HISTORY_LIMIT = 50
  25. ARTIST_LIMIT = 50
  26. FEATURES_LIMIT = 100
  27. # ARTIST_LIMIT = 25
  28. # FEATURES_LIMIT = 25
  29. # TRACKS_TO_QUERY = 100
  30. TRACKS_TO_QUERY = 500
  31. TRACKS_ENDPOINT = 'https://api.spotify.com/v1/tracks'
  32. CONSOLE_LOGGING = True
  33. # CONSOLE_LOGGING = False
  34. # }}} constants #
  35. # parse_library {{{ #
  36. def parse_library(request, user_secret):
  37. """Scans user's library for num_tracks and store the information in a
  38. database.
  39. :user_secret: secret for User object who's library is being scanned.
  40. :returns: None
  41. """
  42. offset = 0
  43. payload = {'limit': str(USER_TRACKS_LIMIT)}
  44. artist_genre_queue = []
  45. features_queue = []
  46. user_obj = User.objects.get(secret=user_secret)
  47. user_headers = get_user_header(user_obj)
  48. # create this obj so loop runs at least once
  49. saved_tracks_response = [0]
  50. # scan until reach num_tracks or no tracks left if scanning entire library
  51. while ((TRACKS_TO_QUERY == 0 or offset < TRACKS_TO_QUERY) and
  52. len(saved_tracks_response) > 0):
  53. payload['offset'] = str(offset)
  54. saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks',
  55. headers=user_headers,
  56. params=payload).json()['items']
  57. tracks_processed = 0
  58. for track_dict in saved_tracks_response:
  59. track_artists = save_track_artists(track_dict['track'], artist_genre_queue,
  60. user_headers)
  61. track_obj, track_created = save_track_obj(track_dict['track'],
  62. track_artists, user_obj)
  63. # add audio features {{{ #
  64. # if a new track is not created, the associated audio feature does
  65. # not need to be created again
  66. if track_created:
  67. features_queue.append(track_obj)
  68. if len(features_queue) == FEATURES_LIMIT:
  69. get_audio_features(user_headers, features_queue)
  70. features_queue = []
  71. # }}} add audio features #
  72. if CONSOLE_LOGGING:
  73. tracks_processed += 1
  74. print("Added track #{}: {} - {}".format(
  75. offset + tracks_processed,
  76. track_obj.artists.first(),
  77. track_obj.name,
  78. ))
  79. # calculates num_songs with offset + songs retrieved
  80. offset += USER_TRACKS_LIMIT
  81. # clean-up {{{ #
  82. # update remaining artists without genres and songs without features if
  83. # there are any
  84. if len(artist_genre_queue) > 0:
  85. add_artist_genres(user_headers, artist_genre_queue)
  86. if len(features_queue) > 0:
  87. get_audio_features(user_headers, features_queue)
  88. # }}} clean-up #
  89. update_track_genres(user_obj)
  90. return render(request, 'graphs/logged_in.html', get_user_context(user_obj))
  91. # }}} parse_library #
  92. # parse_history_request {{{ #
  93. def parse_history_request(request, user_secret):
  94. """Request function to call parse_history. Scans user's listening history
  95. and stores the information in a database.
  96. :user_secret: secret for User object who's library is being scanned.
  97. :returns: redirects user to logged in page
  98. """
  99. parse_history(user_secret)
  100. return render(request, 'graphs/logged_in.html',
  101. get_user_context(User.objects.get(secret=user_secret)))
  102. # }}} get_history #
  103. # get_artist_data {{{ #
  104. def get_artist_data(request, user_secret):
  105. """Returns artist data as a JSON serialized list of dictionaries
  106. The (key, value) pairs are (artist name, song count for said artist)
  107. :param request: the HTTP request
  108. :param user_secret: the user secret used for identification
  109. :return: a JsonResponse
  110. """
  111. user = User.objects.get(secret=user_secret)
  112. artist_counts = Artist.objects.annotate(num_songs=Count('track',
  113. filter=Q(track__users=user)))
  114. processed_artist_counts = [{'name': artist.name, 'num_songs': artist.num_songs}
  115. for artist in artist_counts if artist.num_songs > 2]
  116. if CONSOLE_LOGGING:
  117. pprint(processed_artist_counts)
  118. return JsonResponse(data=processed_artist_counts, safe=False)
  119. # }}} get_artist_data #
  120. # get_audio_feature_data {{{ #
  121. def get_audio_feature_data(request, audio_feature, user_secret):
  122. """Returns all data points for a given audio feature
  123. Args:
  124. request: the HTTP request
  125. audio_feature: The audio feature to be queried
  126. user_secret: client secret, used to identify the user
  127. """
  128. user = User.objects.get(secret=user_secret)
  129. user_tracks = Track.objects.filter(users=user)
  130. response_payload = {
  131. 'data_points': [],
  132. }
  133. for track in user_tracks:
  134. try:
  135. audio_feature_obj = AudioFeatures.objects.get(track=track)
  136. response_payload['data_points'].append(getattr(audio_feature_obj, audio_feature))
  137. except AudioFeatures.DoesNotExist:
  138. continue
  139. return JsonResponse(response_payload)
  140. # }}} get_audio_feature_data #
  141. # get_genre_data {{{ #
  142. def get_genre_data(request, user_secret):
  143. """Return genre data needed to create the graph
  144. TODO
  145. """
  146. user = User.objects.get(secret=user_secret)
  147. genre_counts = (Track.objects.filter(users__exact=user)
  148. .values('genre')
  149. .order_by('genre')
  150. # annotates each genre and not each Track, due to the earlier values() call
  151. .annotate(num_songs=Count('genre'))
  152. )
  153. genre_counts = [genre_dict for genre_dict in genre_counts if
  154. genre_dict['num_songs'] > 3]
  155. # genre_counts is a QuerySet with the format
  156. '''
  157. Now genre_counts has the format [ {'genre': 'classical', 'num_songs': 100,
  158. 'artists': { 'Helene Grimaud': 40.5, 'Beethoven': 31.2, ... }},... ]
  159. '''
  160. for genre_dict in genre_counts:
  161. genre_dict['artists'] = get_artists_in_genre(user, genre_dict['genre'])
  162. if CONSOLE_LOGGING:
  163. print("*** Genre Breakdown ***")
  164. pprint(list(genre_counts))
  165. return JsonResponse(data=list(genre_counts), safe=False)
  166. # }}} get_genre_data #
  167. # import_history {{{ #
  168. def import_history(request, upload_id):
  169. """Import history for the user from the file they uploaded.
  170. :upload_id: ID (PK) of the HistoryUpload entry
  171. :returns: None
  172. """
  173. # setup {{{ #
  174. headers = ['timestamp', 'track_id']
  175. upload_obj = HistoryUpload.objects.get(id=upload_id)
  176. user_headers = get_user_header(upload_obj.user)
  177. with upload_obj.document.open('r') as f:
  178. csv_reader = csv.reader(f, delimiter=',')
  179. rows_read = 0
  180. history_obj_info_lst = []
  181. artist_genre_queue = []
  182. # skip header row
  183. last_row, history_obj_info = get_next_history_row(csv_reader, headers,
  184. {})
  185. while not last_row:
  186. last_row, history_obj_info = get_next_history_row(csv_reader,
  187. headers, history_obj_info)
  188. # }}} setup #
  189. history_obj_info_lst.append(history_obj_info)
  190. # PU: refactor saving History object right away if Track obj already
  191. # exists
  192. # PU: refactor below?
  193. rows_read += 1
  194. if (rows_read % TRACKS_LIMIT == 0) or last_row:
  195. # get tracks_response {{{ #
  196. track_ids_lst = [info['track_id'] for info in history_obj_info_lst]
  197. # print(len(track_ids_lst))
  198. track_ids = ','.join(track_ids_lst)
  199. payload = {'ids': track_ids}
  200. tracks_response = requests.get(TRACKS_ENDPOINT,
  201. headers=user_headers,
  202. params=payload).json()['tracks']
  203. responses_processed = 0
  204. # }}} get tracks_response #
  205. for track_dict in tracks_response:
  206. # don't associate history track with User, not necessarily in their
  207. # library
  208. track_artists = save_track_artists(track_dict, artist_genre_queue,
  209. user_headers)
  210. track_obj, track_created = save_track_obj(track_dict,
  211. track_artists, None)
  212. timestamp = \
  213. parse(history_obj_info_lst[responses_processed]['timestamp'])
  214. history_obj = save_history_obj(upload_obj.user, timestamp,
  215. track_obj)
  216. if CONSOLE_LOGGING:
  217. print("Processed row #{}: {}".format(
  218. (rows_read - TRACKS_LIMIT) + responses_processed, history_obj,))
  219. responses_processed += 1
  220. history_obj_info_lst = []
  221. if len(artist_genre_queue) > 0:
  222. add_artist_genres(user_headers, artist_genre_queue)
  223. # TODO: update track genres from History relation
  224. # update_track_genres(user_obj)
  225. return redirect('graphs:display_history_table')
  226. # }}} get_history #