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.

300 lines
11 KiB

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