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.

315 lines
11 KiB

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