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.

321 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. # }}} increase_artist_count #
  147. # update_popularity_stats {{{ #
  148. def update_popularity_stats(new_data_point, library_stats, sample_size):
  149. """Updates the popularity statistics in library_stats
  150. Args:
  151. new_data_point: new data to update the popularity stats with
  152. library_stats: Dictionary containing data mined from user's Spotify library
  153. sample_size: The sample size including the new data
  154. Returns:
  155. None
  156. """
  157. if sample_size < 2:
  158. library_stats['popularity'] = {
  159. "average": new_data_point,
  160. "std_dev": 0,
  161. }
  162. else :
  163. cur_mean_popularity = library_stats['popularity']['average']
  164. cur_popularity_stdev = library_stats['popularity']['std_dev']
  165. new_mean, new_std_dev = update_std_dev(
  166. cur_mean_popularity, cur_popularity_stdev, new_data_point, sample_size)
  167. library_stats['popularity'] = {
  168. "average": new_mean,
  169. "std_dev": new_std_dev,
  170. }
  171. # }}} update_popularity_stats #
  172. # get_track_info {{{ #
  173. def get_track_info(track_dict, library_stats, sample_size):
  174. """Get all the info from the track_dict directly returned by the API call in parse_library.
  175. :track_dict: Dict returned from the API call containing the track info.
  176. :library_stats: Dictionary containing the data mined from user's Spotify library
  177. :sample_size: The sample size so far including this track
  178. :returns: None
  179. """
  180. # popularity
  181. update_popularity_stats(track_dict['popularity'], library_stats, sample_size)
  182. # year
  183. year_released = track_dict['album']['release_date'].split('-')[0]
  184. increase_nested_key('year_released', year_released, library_stats)
  185. # artist
  186. # artist_names = [artist['name'] for artist in track_dict['artists']]
  187. # for artist_name in artist_names:
  188. # increase_nested_key('artists', artist_name)
  189. # runtime
  190. library_stats['total_runtime'] += float(track_dict['duration_ms']) / (1000 * 60)
  191. # }}} get_track_info #
  192. # calculate_genres_from_artists {{{ #
  193. def calculate_genres_from_artists(headers, library_stats):
  194. """Tallies up genre counts based on artists in library_stats.
  195. :headers: For making the API call.
  196. :library_stats: Dictionary containing the data mined from user's Spotify library
  197. :returns: None
  198. """
  199. for artist_entry in library_stats['artists'].values():
  200. artist_response = requests.get('https://api.spotify.com/v1/artists/' + artist_entry['id'], headers=headers).json()
  201. # increase each genre count by artist count
  202. for genre in artist_response['genres']:
  203. increase_nested_key('genres', genre, library_stats, artist_entry['count'])
  204. # }}} calculate_genres_from_artists #
  205. # process_library_stats {{{ #
  206. def process_library_stats(library_stats):
  207. """Processes library_stats into format more suitable for D3 consumption
  208. Args:
  209. library_stats: Dictionary containing the data mined from user's Spotify library
  210. Returns:
  211. A new dictionary that contains the data in library_stats, in a format more suitable for D3 consumption
  212. """
  213. processed_library_stats = {}
  214. for key in library_stats:
  215. if key == 'artists' or key == 'genres' or key == 'year_released':
  216. for inner_key in library_stats[key]:
  217. if key not in processed_library_stats:
  218. processed_library_stats[key] = []
  219. processed_item_key = '' # identifier key for each dict in the list
  220. count = 0
  221. if 'artist' in key:
  222. processed_item_key = 'name'
  223. count = library_stats[key][inner_key]['count']
  224. elif 'genre' in key:
  225. processed_item_key = 'genre'
  226. count = library_stats[key][inner_key]
  227. else:
  228. processed_item_key = 'year'
  229. count = library_stats[key][inner_key]
  230. processed_library_stats[key].append({
  231. processed_item_key: inner_key,
  232. "count": count
  233. })
  234. elif key == 'audio_features':
  235. for audio_feature in library_stats[key]:
  236. if 'audio_features' not in processed_library_stats:
  237. processed_library_stats['audio_features'] = []
  238. processed_library_stats['audio_features'].append({
  239. 'feature': audio_feature,
  240. 'average': library_stats[key][audio_feature]['average'],
  241. 'std_dev': library_stats[key][audio_feature]['std_dev']
  242. })
  243. # TODO: Not sure about final form for 'popularity'
  244. # elif key == 'popularity':
  245. # processed_library_stats[key] = []
  246. # processed_library_stats[key].append({
  247. # })
  248. elif key == 'num_songs' or key == 'total_runtime' or key == 'popularity':
  249. processed_library_stats[key] = library_stats[key]
  250. return processed_library_stats
  251. # }}} process_library_stats #