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.

281 lines
10 KiB

7 years ago
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 and store the information in a database
  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. # TODO: fix this, don't need any more
  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 = shared_genres.intersection(artist.genres.all())
  98. shared_genres = shared_genres.order_by('-num_songs')
  99. undefined_genre_obj = Genre.objects.get(name="undefined")
  100. most_common_genre = shared_genres.first() if shared_genres.first() is \
  101. not undefined_genre_obj else shared_genres[1]
  102. track.genre = most_common_genre if most_common_genre is not None \
  103. else undefined_genre_obj
  104. track.save()
  105. # print(track.name, track.genre)
  106. # }}} update_track_genres #
  107. # save_track_obj {{{ #
  108. def save_track_obj(track_dict, artists, top_genre, user):
  109. """Make an entry in the database for this track if it doesn't exist already.
  110. :track_dict: dictionary from the API call containing track information.
  111. :artists: artists of the song, passed in as a list of Artist objects.
  112. :top_genre: top genre associated with this track (see get_top_genre).
  113. :user: User object for which this Track is to be associated with.
  114. :returns: (The created/retrieved Track object, created)
  115. """
  116. track_query = Track.objects.filter(track_id__exact=track_dict['id'])
  117. if len(track_query) != 0:
  118. return track_query[0], False
  119. else:
  120. new_track = Track.objects.create(
  121. track_id=track_dict['id'],
  122. year=track_dict['album']['release_date'].split('-')[0],
  123. popularity=int(track_dict['popularity']),
  124. runtime=int(float(track_dict['duration_ms']) / 1000),
  125. name=track_dict['name'],
  126. # genre=top_genre,
  127. )
  128. # have to add artists and user after saving object since track needs to
  129. # have ID before filling in m2m field
  130. for artist in artists:
  131. new_track.artists.add(artist)
  132. new_track.users.add(user)
  133. new_track.save()
  134. return new_track, True
  135. # }}} save_track_obj #
  136. # get_audio_features {{{ #
  137. def get_audio_features(headers, track_objs):
  138. """Creates and saves a new AudioFeatures objects for the respective
  139. track_objs. track_objs should contain the API limit for a single call
  140. (FEATURES_LIMIT) for maximum efficiency.
  141. :headers: headers containing the API token
  142. :track_objs: Track objects to associate with the new AudioFeatures object
  143. :returns: None
  144. """
  145. track_ids = str.join(",", [track_obj.track_id for track_obj in track_objs])
  146. params = {'ids': track_ids}
  147. features_response = requests.get("https://api.spotify.com/v1/audio-features",
  148. headers=headers,params=params).json()['audio_features']
  149. # pprint.pprint(features_response)
  150. useless_keys = [ "key", "mode", "type", "liveness", "id", "uri", "track_href", "analysis_url", "time_signature", ]
  151. for i in range(len(track_objs)):
  152. if features_response[i] is not None:
  153. # Data that we don't need
  154. cur_features_obj = AudioFeatures()
  155. cur_features_obj.track = track_objs[i]
  156. for key, val in features_response[i].items():
  157. if key not in useless_keys:
  158. setattr(cur_features_obj, key, val)
  159. cur_features_obj.save()
  160. # }}} get_audio_features #
  161. def process_artist_genre(genre_name, artist_obj):
  162. """Increase count for correspoding Genre object to genre_name and add that
  163. Genre to artist_obj.
  164. :genre_name: Name of genre.
  165. :artist_obj: Artist object to add Genre object to.
  166. :returns: None
  167. """
  168. genre_obj, created = Genre.objects.get_or_create(name=genre_name,
  169. defaults={'num_songs':1})
  170. if not created:
  171. genre_obj.num_songs = F('num_songs') + 1
  172. genre_obj.save()
  173. artist_obj.genres.add(genre_obj)
  174. artist_obj.save()
  175. # add_artist_genres {{{ #
  176. def add_artist_genres(headers, artist_objs):
  177. """Adds genres to artist_objs and increases the count the respective Genre
  178. object. artist_objs should contain the API limit for a single call
  179. (ARTIST_LIMIT) for maximum efficiency.
  180. :headers: For making the API call.
  181. :artist_objs: List of Artist objects for which to add/tally up genres for.
  182. :returns: None
  183. """
  184. artist_ids = str.join(",", [artist_obj.artist_id for artist_obj in artist_objs])
  185. params = {'ids': artist_ids}
  186. artists_response = requests.get('https://api.spotify.com/v1/artists/',
  187. headers=headers, params=params).json()['artists']
  188. # pprint.pprint(artists_response)
  189. for i in range(len(artist_objs)):
  190. if len(artists_response[i]['genres']) == 0:
  191. process_artist_genre("undefined", artist_objs[i])
  192. else:
  193. for genre in artists_response[i]['genres']:
  194. process_artist_genre(genre, artist_objs[i])
  195. # }}} add_artist_genres #
  196. # get_artists_in_genre {{{ #
  197. def get_artists_in_genre(user, genre, max_songs):
  198. """Return count of artists in genre.
  199. :user: User object to return data for.
  200. :genre: genre to count artists for.
  201. :max_songs: max total songs to include to prevent overflow due to having
  202. multiple artists on each track.
  203. :returns: dict of artists in the genre along with the number of songs they
  204. have.
  205. """
  206. genre_obj = Genre.objects.get(name=genre)
  207. artist_counts = (Artist.objects.filter(track__users=user)
  208. .filter(genres=genre_obj)
  209. .annotate(num_songs=Count('track', distinct=True))
  210. .order_by('-num_songs')
  211. )
  212. processed_artist_counts = {}
  213. songs_added = 0
  214. for artist in artist_counts:
  215. # hacky way to not have total count overflow due to there being multiple
  216. # artists on a track
  217. if songs_added + artist.num_songs <= max_songs:
  218. processed_artist_counts[artist.name] = artist.num_songs
  219. songs_added += artist.num_songs
  220. # processed_artist_counts = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts]
  221. # processed_artist_counts = {artist.name: artist.num_songs for artist in artist_counts}
  222. # pprint.pprint(processed_artist_counts)
  223. return processed_artist_counts
  224. # }}} get_artists_in_genre #