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.

327 lines
12 KiB

  1. # imports {{{ #
  2. import requests
  3. import math
  4. import pprint
  5. from .models import Artist, User, Track, AudioFeatures
  6. # }}} imports #
  7. # parse_library {{{ #
  8. def parse_library(headers, tracks, library_stats, user):
  9. """Scans user's library for certain number of tracks to update library_stats with.
  10. :headers: For API call.
  11. :tracks: Number of tracks to get from user's library.
  12. :library_stats: Dictionary containing the data mined from user's library
  13. :user: a User object representing the user whose library we are parsing
  14. :returns: None
  15. """
  16. # TODO: implement importing entire library with 0 as tracks param
  17. # number of tracks to get with each call
  18. limit = 5
  19. # keeps track of point to get songs from
  20. offset = 0
  21. payload = {'limit': str(limit)}
  22. # use two separate variables to track, because the average popularity also requires num_samples
  23. num_samples = 0 # number of actual track samples
  24. feature_data_points = 0 # number of feature data analyses (some tracks do not have analyses available)
  25. for _ in range(0, tracks, limit):
  26. payload['offset'] = str(offset)
  27. saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks', headers=headers, params=payload).json()
  28. for track_dict in saved_tracks_response['items']:
  29. num_samples += 1
  30. get_track_info(track_dict['track'], library_stats, num_samples)
  31. # get_genre(headers, track_dict['track']['album']['id'])
  32. audio_features_dict = get_audio_features(headers, track_dict['track']['id'])
  33. if len(audio_features_dict) != 0:
  34. # Track the number of audio analyses for calculating
  35. # audio feature averages and standard deviations on the fly
  36. feature_data_points += 1
  37. for feature, feature_data in audio_features_dict.items():
  38. update_audio_feature_stats(feature, feature_data, feature_data_points, library_stats)
  39. for artist_dict in track_dict['track']['artists']:
  40. increase_artist_count(headers, artist_dict['name'], artist_dict['id'], library_stats)
  41. # calculates num_songs with offset + songs retrieved
  42. library_stats['num_songs'] = offset + len(saved_tracks_response['items'])
  43. offset += limit
  44. calculate_genres_from_artists(headers, library_stats)
  45. # pprint.pprint(library_stats)
  46. # }}} parse_library #
  47. # get_audio_features {{{ #
  48. def get_audio_features(headers, track_id):
  49. """Returns the audio features of a soundtrack
  50. Args:
  51. headers: headers containing the API token
  52. track_id: the id of the soundtrack, needed to query the Spotify API
  53. Returns:
  54. A dictionary with the features as its keys, if audio feature data is missing for the track,
  55. an empty dictionary is returned.
  56. """
  57. response = requests.get("https://api.spotify.com/v1/audio-features/{}".format(track_id), headers = headers).json()
  58. if 'error' in response:
  59. return {}
  60. features_dict = {}
  61. # Data that we don't need
  62. useless_keys = [
  63. "key", "mode", "type", "liveness", "id", "uri", "track_href", "analysis_url", "time_signature",
  64. ]
  65. for key, val in response.items():
  66. if key not in useless_keys:
  67. features_dict[key] = val
  68. return features_dict
  69. # }}} get_audio_features #
  70. # update_std_dev {{{ #
  71. def update_std_dev(cur_mean, cur_std_dev, new_data_point, sample_size):
  72. """Calculates the standard deviation for a sample without storing all data points
  73. Args:
  74. cur_mean: the current mean for N = (sample_size - 1)
  75. cur_std_dev: the current standard deviation for N = (sample_size - 1)
  76. new_data_point: a new data point
  77. sample_size: sample size including the new data point
  78. Returns:
  79. (new_mean, new_std_dev)
  80. """
  81. # This is an implementation of Welford's method
  82. # http://jonisalonen.com/2013/deriving-welfords-method-for-computing-variance/
  83. new_mean = ((sample_size - 1) * cur_mean + new_data_point) / sample_size
  84. delta_variance = (new_data_point - new_mean) * (new_data_point - cur_mean)
  85. new_std_dev = math.sqrt(
  86. (math.pow(cur_std_dev, 2) * (sample_size - 2) + delta_variance) / (
  87. sample_size - 1
  88. ))
  89. return new_mean, new_std_dev
  90. # }}} update_std_dev #
  91. # update_audio_feature_stats {{{ #
  92. def update_audio_feature_stats(feature, new_data_point, sample_size, library_stats):
  93. """Updates the audio feature statistics in library_stats
  94. Args:
  95. feature: the audio feature to be updated (string)
  96. new_data_point: new data to update the stats with
  97. sample_size: sample size including the new data point
  98. library_stats Dictionary containing the data mined from user's Spotify library
  99. Returns:
  100. None
  101. """
  102. # first time the feature is considered
  103. if sample_size < 2:
  104. library_stats['audio_features'][feature] = {
  105. "average": new_data_point,
  106. "std_dev": 0,
  107. }
  108. else:
  109. cur_mean = library_stats['audio_features'][feature]['average']
  110. cur_std_dev = library_stats['audio_features'][feature]['std_dev']
  111. new_mean, new_std_dev = update_std_dev(cur_mean, cur_std_dev, new_data_point, sample_size)
  112. library_stats['audio_features'][feature] = {
  113. "average": new_mean,
  114. "std_dev": new_std_dev
  115. }
  116. # }}} update_audio_feature_stats #
  117. # increase_nested_key {{{ #
  118. def increase_nested_key(top_key, nested_key, library_stats, amount=1):
  119. """Increases count for the value of library_stats[top_key][nested_key]. Checks if nested_key exists already and takes
  120. appropriate action.
  121. :top_key: First key of library_stats.
  122. :nested_key: Key in top_key's dict for which we want to increase value of.
  123. :library_stats: Dictionary containing the data mined from user's Spotify library
  124. :returns: None
  125. """
  126. if nested_key not in library_stats[top_key]:
  127. library_stats[top_key][nested_key] = amount
  128. else:
  129. library_stats[top_key][nested_key] += amount
  130. # }}} increase_nested_key #
  131. # increase_artist_count {{{ #
  132. def increase_artist_count(headers, artist_name, artist_id, library_stats):
  133. """Increases count for artist in library_stats and stores the artist_id.
  134. :headers: For making the API call.
  135. :artist_name: Artist to increase count for.
  136. :artist_id: The Spotify ID for the artist.
  137. :library_stats: Dictionary containing the data mined from user's Spotify library
  138. :returns: None
  139. """
  140. if artist_name not in library_stats['artists']:
  141. library_stats['artists'][artist_name] = {}
  142. library_stats['artists'][artist_name]['count'] = 1
  143. library_stats['artists'][artist_name]['id'] = artist_id
  144. else:
  145. library_stats['artists'][artist_name]['count'] += 1
  146. # add artist to database if new
  147. if len(Artist.objects.filter(artist_id__contains=artist_id)) == 0:
  148. new_artist = Artist(
  149. artist_id=artist_id,
  150. name=artist_name,
  151. )
  152. new_artist.save()
  153. # }}} increase_artist_count #
  154. # update_popularity_stats {{{ #
  155. def update_popularity_stats(new_data_point, library_stats, sample_size):
  156. """Updates the popularity statistics in library_stats
  157. Args:
  158. new_data_point: new data to update the popularity stats with
  159. library_stats: Dictionary containing data mined from user's Spotify library
  160. sample_size: The sample size including the new data
  161. Returns:
  162. None
  163. """
  164. if sample_size < 2:
  165. library_stats['popularity'] = {
  166. "average": new_data_point,
  167. "std_dev": 0,
  168. }
  169. else :
  170. cur_mean_popularity = library_stats['popularity']['average']
  171. cur_popularity_stdev = library_stats['popularity']['std_dev']
  172. new_mean, new_std_dev = update_std_dev(
  173. cur_mean_popularity, cur_popularity_stdev, new_data_point, sample_size)
  174. library_stats['popularity'] = {
  175. "average": new_mean,
  176. "std_dev": new_std_dev,
  177. }
  178. # }}} update_popularity_stats #
  179. # get_track_info {{{ #
  180. def get_track_info(track_dict, library_stats, sample_size):
  181. """Get all the info from the track_dict directly returned by the API call in parse_library.
  182. :track_dict: Dict returned from the API call containing the track info.
  183. :library_stats: Dictionary containing the data mined from user's Spotify library
  184. :sample_size: The sample size so far including this track
  185. :returns: None
  186. """
  187. # popularity
  188. update_popularity_stats(track_dict['popularity'], library_stats, sample_size)
  189. # year
  190. year_released = track_dict['album']['release_date'].split('-')[0]
  191. increase_nested_key('year_released', year_released, library_stats)
  192. # runtime
  193. library_stats['total_runtime'] += float(track_dict['duration_ms']) / (1000 * 60)
  194. # }}} get_track_info #
  195. # calculate_genres_from_artists {{{ #
  196. def calculate_genres_from_artists(headers, library_stats):
  197. """Tallies up genre counts based on artists in library_stats.
  198. :headers: For making the API call.
  199. :library_stats: Dictionary containing the data mined from user's Spotify library
  200. :returns: None
  201. """
  202. for artist_entry in library_stats['artists'].values():
  203. artist_response = requests.get('https://api.spotify.com/v1/artists/' + artist_entry['id'], headers=headers).json()
  204. # increase each genre count by artist count
  205. for genre in artist_response['genres']:
  206. increase_nested_key('genres', genre, library_stats, artist_entry['count'])
  207. # update genre for artist in database with top genre
  208. Artist.objects.filter(artist_id=artist_entry['id']).update(genre=artist_response['genres'][0])
  209. # }}} calculate_genres_from_artists #
  210. # process_library_stats {{{ #
  211. def process_library_stats(library_stats):
  212. """Processes library_stats into format more suitable for D3 consumption
  213. Args:
  214. library_stats: Dictionary containing the data mined from user's Spotify library
  215. Returns:
  216. A new dictionary that contains the data in library_stats, in a format more suitable for D3 consumption
  217. """
  218. processed_library_stats = {}
  219. for key in library_stats:
  220. if key == 'artists' or key == 'genres' or key == 'year_released':
  221. for inner_key in library_stats[key]:
  222. if key not in processed_library_stats:
  223. processed_library_stats[key] = []
  224. processed_item_key = '' # identifier key for each dict in the list
  225. count = 0
  226. if 'artist' in key:
  227. processed_item_key = 'name'
  228. count = library_stats[key][inner_key]['count']
  229. elif 'genre' in key:
  230. processed_item_key = 'genre'
  231. count = library_stats[key][inner_key]
  232. else:
  233. processed_item_key = 'year'
  234. count = library_stats[key][inner_key]
  235. processed_library_stats[key].append({
  236. processed_item_key: inner_key,
  237. "count": count
  238. })
  239. elif key == 'audio_features':
  240. for audio_feature in library_stats[key]:
  241. if 'audio_features' not in processed_library_stats:
  242. processed_library_stats['audio_features'] = []
  243. processed_library_stats['audio_features'].append({
  244. 'feature': audio_feature,
  245. 'average': library_stats[key][audio_feature]['average'],
  246. 'std_dev': library_stats[key][audio_feature]['std_dev']
  247. })
  248. # TODO: Not sure about final form for 'popularity'
  249. # elif key == 'popularity':
  250. # processed_library_stats[key] = []
  251. # processed_library_stats[key].append({
  252. # })
  253. elif key == 'num_songs' or key == 'total_runtime' or key == 'popularity':
  254. processed_library_stats[key] = library_stats[key]
  255. return processed_library_stats
  256. # }}} process_library_stats #