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.

277 lines
9.7 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. from .models import *
  6. from django.db.models import Count, Q, F
  7. from django.http import JsonResponse
  8. from django.core import serializers
  9. import json
  10. # }}} imports #
  11. USER_TRACKS_LIMIT = 50
  12. ARTIST_LIMIT = 50
  13. FEATURES_LIMIT = 100
  14. # ARTIST_LIMIT = 25
  15. # FEATURES_LIMIT = 25
  16. # parse_library {{{ #
  17. def parse_library(headers, tracks, user):
  18. """Scans user's library for certain number of tracks to update library_stats with.
  19. :headers: For API call.
  20. :tracks: Number of tracks to get from user's library.
  21. :user: a User object representing the user whose library we are parsing
  22. :returns: None
  23. """
  24. # TODO: implement importing entire library with 0 as tracks param
  25. # keeps track of point to get songs from
  26. offset = 0
  27. payload = {'limit': str(USER_TRACKS_LIMIT)}
  28. artist_genre_queue = []
  29. features_queue = []
  30. # iterate until hit requested num of tracks
  31. for i in range(0, tracks, USER_TRACKS_LIMIT):
  32. payload['offset'] = str(offset)
  33. saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks',
  34. headers=headers,
  35. params=payload).json()
  36. for track_dict in saved_tracks_response['items']:
  37. # add artists {{{ #
  38. # update artist info before track so that Track object can reference
  39. # Artist object
  40. track_artists = []
  41. for artist_dict in track_dict['track']['artists']:
  42. artist_obj, artist_created = Artist.objects.get_or_create(
  43. artist_id=artist_dict['id'],
  44. name=artist_dict['name'],)
  45. # only add/tally up artist genres if new
  46. if artist_created:
  47. artist_genre_queue.append(artist_obj)
  48. if len(artist_genre_queue) == ARTIST_LIMIT:
  49. add_artist_genres(headers, artist_genre_queue)
  50. artist_genre_queue = []
  51. track_artists.append(artist_obj)
  52. # }}} add artists #
  53. # WIP: get most common genre
  54. top_genre = ""
  55. track_obj, track_created = save_track_obj(track_dict['track'],
  56. track_artists, top_genre, user)
  57. # add audio features {{{ #
  58. # if a new track is not created, the associated audio feature does
  59. # not need to be created again
  60. if track_created:
  61. features_queue.append(track_obj)
  62. if len(features_queue) == FEATURES_LIMIT:
  63. get_audio_features(headers, features_queue)
  64. features_queue = []
  65. # }}} add audio features #
  66. # temporary console logging
  67. print("#{}-{}: {} - {}".format(offset + 1,
  68. offset + USER_TRACKS_LIMIT,
  69. track_obj.artists.first(),
  70. track_obj.name))
  71. # calculates num_songs with offset + songs retrieved
  72. offset += USER_TRACKS_LIMIT
  73. # clean-up {{{ #
  74. # update remaining artists without genres and songs without features if
  75. # there are any
  76. if len(artist_genre_queue) > 0:
  77. add_artist_genres(headers, artist_genre_queue)
  78. if len(features_queue) > 0:
  79. get_audio_features(headers, features_queue)
  80. # }}} clean-up #
  81. update_track_genres(user)
  82. # }}} parse_library #
  83. # update_track_genres {{{ #
  84. def update_track_genres(user):
  85. """Updates user's tracks with the most common genre associated with the
  86. songs' artist(s).
  87. :user: User object who's tracks are being updated.
  88. :returns: None
  89. """
  90. user_tracks = Track.objects.filter(users__exact=user)
  91. for track in user_tracks:
  92. # just using this variable to save another call to db
  93. track_artists = track.artists.all()
  94. # set genres to first artist's genres then find intersection with others
  95. shared_genres = track_artists.first().genres.all()
  96. for artist in track_artists:
  97. shared_genres.intersection(artist.genres.all())
  98. most_common_genre = shared_genres.order_by('-num_songs').first()
  99. track.genre = most_common_genre if most_common_genre is not None \
  100. else "undefined"
  101. track.save()
  102. # print(track.name, track.genre)
  103. # }}} update_track_genres #
  104. # save_track_obj {{{ #
  105. def save_track_obj(track_dict, artists, top_genre, user):
  106. """Make an entry in the database for this track if it doesn't exist already.
  107. :track_dict: dictionary from the API call containing track information.
  108. :artists: artists of the song, passed in as a list of Artist objects.
  109. :top_genre: top genre associated with this track (see get_top_genre).
  110. :user: User object for which this Track is to be associated with.
  111. :returns: (The created/retrieved Track object, created)
  112. """
  113. track_query = Track.objects.filter(track_id__exact=track_dict['id'])
  114. if len(track_query) != 0:
  115. return track_query[0], False
  116. else:
  117. new_track = Track.objects.create(
  118. track_id=track_dict['id'],
  119. year=track_dict['album']['release_date'].split('-')[0],
  120. popularity=int(track_dict['popularity']),
  121. runtime=int(float(track_dict['duration_ms']) / 1000),
  122. name=track_dict['name'],
  123. # genre=top_genre,
  124. )
  125. # have to add artists and user after saving object since track needs to
  126. # have ID before filling in m2m field
  127. for artist in artists:
  128. new_track.artists.add(artist)
  129. new_track.users.add(user)
  130. new_track.save()
  131. return new_track, True
  132. # }}} save_track_obj #
  133. # get_audio_features {{{ #
  134. def get_audio_features(headers, track_objs):
  135. """Creates and saves a new AudioFeatures objects for the respective
  136. track_objs. track_objs should contain the API limit for a single call
  137. (FEATURES_LIMIT) for maximum efficiency.
  138. :headers: headers containing the API token
  139. :track_objs: Track objects to associate with the new AudioFeatures object
  140. :returns: None
  141. """
  142. track_ids = str.join(",", [track_obj.track_id for track_obj in track_objs])
  143. params = {'ids': track_ids}
  144. features_response = requests.get("https://api.spotify.com/v1/audio-features",
  145. headers=headers,params=params).json()['audio_features']
  146. # pprint.pprint(features_response)
  147. useless_keys = [ "key", "mode", "type", "liveness", "id", "uri", "track_href", "analysis_url", "time_signature", ]
  148. for i in range(len(track_objs)):
  149. if features_response[i] is not None:
  150. # Data that we don't need
  151. cur_features_obj = AudioFeatures()
  152. cur_features_obj.track = track_objs[i]
  153. for key, val in features_response[i].items():
  154. if key not in useless_keys:
  155. setattr(cur_features_obj, key, val)
  156. cur_features_obj.save()
  157. # }}} get_audio_features #
  158. def process_artist_genre(genre_name, artist_obj):
  159. """Increase count for correspoding Genre object to genre_name and add that
  160. Genre to artist_obj.
  161. :genre_name: Name of genre.
  162. :artist_obj: Artist object to add Genre object to.
  163. :returns: None
  164. """
  165. genre_obj, created = Genre.objects.get_or_create(name=genre_name,
  166. defaults={'num_songs':1})
  167. if not created:
  168. genre_obj.num_songs = F('num_songs') + 1
  169. genre_obj.save()
  170. artist_obj.genres.add(genre_obj)
  171. artist_obj.save()
  172. # add_artist_genres {{{ #
  173. def add_artist_genres(headers, artist_objs):
  174. """Adds genres to artist_objs and increases the count the respective Genre
  175. object. artist_objs should contain the API limit for a single call
  176. (ARTIST_LIMIT) for maximum efficiency.
  177. :headers: For making the API call.
  178. :artist_objs: List of Artist objects for which to add/tally up genres for.
  179. :returns: None
  180. """
  181. artist_ids = str.join(",", [artist_obj.artist_id for artist_obj in artist_objs])
  182. params = {'ids': artist_ids}
  183. artists_response = requests.get('https://api.spotify.com/v1/artists/',
  184. headers=headers, params=params).json()['artists']
  185. # pprint.pprint(artists_response)
  186. for i in range(len(artist_objs)):
  187. if len(artists_response[i]['genres']) == 0:
  188. process_artist_genre("undefined", artist_objs[i])
  189. else:
  190. for genre in artists_response[i]['genres']:
  191. process_artist_genre(genre, artist_objs[i])
  192. # }}} add_artist_genres #
  193. # get_artists_in_genre {{{ #
  194. def get_artists_in_genre(user, genre, max_songs):
  195. """Return count of artists in genre.
  196. :user: User object to return data for.
  197. :genre: genre to count artists for.
  198. :max_songs: max total songs to include to prevent overflow due to having
  199. multiple artists on each track.
  200. :returns: dict of artists in the genre along with the number of songs they
  201. have.
  202. """
  203. artist_counts = (Artist.objects.filter(track__users=user)
  204. .filter(track__genre=genre)
  205. .annotate(num_songs=Count('track', distinct=True))
  206. .order_by('-num_songs')
  207. )
  208. processed_artist_counts = {}
  209. songs_added = 0
  210. for artist in artist_counts:
  211. # hacky way to not have total count overflow due to there being multiple
  212. # artists on a track
  213. if songs_added + artist.num_songs <= max_songs:
  214. processed_artist_counts[artist.name] = artist.num_songs
  215. songs_added += artist.num_songs
  216. # processed_artist_counts = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts]
  217. # processed_artist_counts = {artist.name: artist.num_songs for artist in artist_counts}
  218. # pprint.pprint(processed_artist_counts)
  219. return processed_artist_counts
  220. # }}} get_artists_in_genre #