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.

248 lines
8.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # imports {{{ #
  2. import requests
  3. import math
  4. import pprint
  5. import os
  6. import json
  7. from django.db.models import Count, Q, F
  8. from django.http import JsonResponse
  9. from django.core import serializers
  10. from django.utils import timezone
  11. from .models import *
  12. from login.models import User
  13. # }}} imports #
  14. console_logging = True
  15. # console_logging = False
  16. artists_genre_processed = 0
  17. features_processed = 0
  18. # update_track_genres {{{ #
  19. def update_track_genres(user_obj):
  20. """Updates user_obj's tracks with the most common genre associated with the
  21. songs' artist(s).
  22. :user_obj: User object who's tracks are being updated.
  23. :returns: None
  24. """
  25. tracks_processed = 0
  26. user_tracks = Track.objects.filter(users__exact=user_obj)
  27. for track in user_tracks:
  28. # just using this variable to save another call to db
  29. track_artists = track.artists.all()
  30. # set genres to first artist's genres then find intersection with others
  31. shared_genres = track_artists.first().genres.all()
  32. for artist in track_artists:
  33. shared_genres = shared_genres.intersection(artist.genres.all())
  34. shared_genres = shared_genres.order_by('-num_songs')
  35. undefined_genre_obj = Genre.objects.get(name="undefined")
  36. most_common_genre = shared_genres.first() if shared_genres.first() is \
  37. not undefined_genre_obj else shared_genres[1]
  38. track.genre = most_common_genre if most_common_genre is not None \
  39. else undefined_genre_obj
  40. track.save()
  41. tracks_processed += 1
  42. if console_logging:
  43. print("Added '{}' as genre for song #{} - '{}'".format(
  44. track.genre,
  45. tracks_processed,
  46. track.name,
  47. ))
  48. # }}} update_track_genres #
  49. # save_track_obj {{{ #
  50. def save_track_obj(track_dict, artists, user_obj):
  51. """Make an entry in the database for this track if it doesn't exist already.
  52. :track_dict: dictionary from the API call containing track information.
  53. :artists: artists of the song, passed in as a list of Artist objects.
  54. :user_obj: User object for which this Track is to be associated with.
  55. :returns: (The created/retrieved Track object, created)
  56. """
  57. track_query = Track.objects.filter(id__exact=track_dict['id'])
  58. if len(track_query) != 0:
  59. return track_query[0], False
  60. else:
  61. new_track = Track.objects.create(
  62. id=track_dict['id'],
  63. year=track_dict['album']['release_date'].split('-')[0],
  64. popularity=int(track_dict['popularity']),
  65. runtime=int(float(track_dict['duration_ms']) / 1000),
  66. name=track_dict['name'],
  67. )
  68. # have to add artists and user_obj after saving object since track needs to
  69. # have ID before filling in m2m field
  70. for artist in artists:
  71. new_track.artists.add(artist)
  72. new_track.users.add(user_obj)
  73. new_track.save()
  74. return new_track, True
  75. # }}} save_track_obj #
  76. # get_audio_features {{{ #
  77. def get_audio_features(headers, track_objs):
  78. """Creates and saves a new AudioFeatures objects for the respective
  79. track_objs. track_objs should contain the API limit for a single call
  80. (FEATURES_LIMIT) for maximum efficiency.
  81. :headers: headers containing the API token
  82. :track_objs: Track objects to associate with the new AudioFeatures object
  83. :returns: None
  84. """
  85. track_ids = str.join(",", [track_obj.id for track_obj in track_objs])
  86. params = {'ids': track_ids}
  87. features_response = requests.get("https://api.spotify.com/v1/audio-features",
  88. headers=headers,
  89. params={'ids': track_ids}
  90. ).json()['audio_features']
  91. # pprint.pprint(features_response)
  92. useless_keys = [ "key", "mode", "type", "liveness", "id", "uri",
  93. "track_href", "analysis_url", "time_signature", ]
  94. for i in range(len(track_objs)):
  95. if features_response[i] is not None:
  96. # Data that we don't need
  97. cur_features_obj = AudioFeatures()
  98. cur_features_obj.track = track_objs[i]
  99. for key, val in features_response[i].items():
  100. if key not in useless_keys:
  101. setattr(cur_features_obj, key, val)
  102. cur_features_obj.save()
  103. if console_logging:
  104. global features_processed
  105. features_processed += 1
  106. print("Added features for song #{} - {}".format(
  107. features_processed, track_objs[i].name))
  108. # }}} get_audio_features #
  109. # process_artist_genre {{{ #
  110. def process_artist_genre(genre_name, artist_obj):
  111. """Increase count for correspoding Genre object to genre_name and add that
  112. Genre to artist_obj.
  113. :genre_name: Name of genre.
  114. :artist_obj: Artist object to add Genre object to.
  115. :returns: None
  116. """
  117. genre_obj, created = Genre.objects.get_or_create(name=genre_name,
  118. defaults={'num_songs':1})
  119. if not created:
  120. genre_obj.num_songs = F('num_songs') + 1
  121. genre_obj.save()
  122. artist_obj.genres.add(genre_obj)
  123. artist_obj.save()
  124. # }}} process_artist_genre #
  125. # add_artist_genres {{{ #
  126. def add_artist_genres(headers, artist_objs):
  127. """Adds genres to artist_objs and increases the count the respective Genre
  128. object. artist_objs should contain the API limit for a single call
  129. (ARTIST_LIMIT) for maximum efficiency.
  130. :headers: For making the API call.
  131. :artist_objs: List of Artist objects for which to add/tally up genres for.
  132. :returns: None
  133. """
  134. artist_ids = str.join(",", [artist_obj.id for artist_obj in artist_objs])
  135. params = {'ids': artist_ids}
  136. artists_response = requests.get('https://api.spotify.com/v1/artists/',
  137. headers=headers,
  138. params=params,
  139. ).json()['artists']
  140. for i in range(len(artist_objs)):
  141. if len(artists_response[i]['genres']) == 0:
  142. process_artist_genre("undefined", artist_objs[i])
  143. else:
  144. for genre in artists_response[i]['genres']:
  145. process_artist_genre(genre, artist_objs[i])
  146. if console_logging:
  147. global artists_genre_processed
  148. artists_genre_processed += 1
  149. print("Added genres for artist #{} - {}".format(
  150. artists_genre_processed, artist_objs[i].name))
  151. # }}} add_artist_genres #
  152. # get_artists_in_genre {{{ #
  153. def get_artists_in_genre(user, genre, max_songs):
  154. """Return count of artists in genre.
  155. :user: User object to return data for.
  156. :genre: genre to count artists for.
  157. :max_songs: max total songs to include to prevent overflow due to having
  158. multiple artists on each track.
  159. :returns: dict of artists in the genre along with the number of songs they
  160. have.
  161. """
  162. genre_obj = Genre.objects.get(name=genre)
  163. artist_counts = (Artist.objects.filter(track__users=user)
  164. .filter(genres=genre_obj)
  165. .annotate(num_songs=Count('track', distinct=True))
  166. .order_by('-num_songs')
  167. )
  168. processed_artist_counts = {}
  169. songs_added = 0
  170. for artist in artist_counts:
  171. # hacky way to not have total count overflow due to there being multiple
  172. # artists on a track
  173. if songs_added + artist.num_songs <= max_songs:
  174. processed_artist_counts[artist.name] = artist.num_songs
  175. songs_added += artist.num_songs
  176. # processed_artist_counts = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts]
  177. # processed_artist_counts = {artist.name: artist.num_songs for artist in artist_counts}
  178. # pprint.pprint(processed_artist_counts)
  179. return processed_artist_counts
  180. # }}} get_artists_in_genre #
  181. def get_user_header(user_obj):
  182. """Returns the authorization string needed to make an API call.
  183. :user_obj: User to return the auth string for.
  184. :returns: the authorization string used for the header in a Spotify API
  185. call.
  186. """
  187. seconds_elapsed = (timezone.now() -
  188. user_obj.access_obtained_at).total_seconds()
  189. if seconds_elapsed >= user_obj.access_expires_in:
  190. req_body = {
  191. 'grant_type': 'refresh_token',
  192. 'refresh_token': user_obj.refresh_token,
  193. 'client_id': os.environ['SPOTIFY_CLIENT_ID'],
  194. 'client_secret': os.environ['SPOTIFY_CLIENT_SECRET']
  195. }
  196. token_response = requests.post('https://accounts.spotify.com/api/token',
  197. data=req_body).json()
  198. user_obj.access_token = token_response['access_token']
  199. user_obj.access_expires_in = token_response['expires_in']
  200. user_obj.save()
  201. return {'Authorization': "Bearer " + user_obj.access_token}