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.

325 lines
11 KiB

  1. from django.shortcuts import render, redirect
  2. from django.http import HttpResponse, HttpResponseBadRequest
  3. import math
  4. import random
  5. import requests
  6. import os
  7. import urllib
  8. import json
  9. import pprint
  10. from datetime import datetime
  11. TIME_FORMAT = '%Y-%m-%d-%H-%M-%S'
  12. library_stats = {"audio_features":{}, "genres":{}, "year_released":{}, "artists":{}, "num_songs":0, "popularity":[], "total_runtime":0}
  13. # generate_random_string {{{ #
  14. def generate_random_string(length):
  15. """Generates a random string of a certain length
  16. Args:
  17. length: the desired length of the randomized string
  18. Returns:
  19. A random string
  20. """
  21. rand_str = ""
  22. possible_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  23. for _ in range(length):
  24. rand_str += possible_chars[random.randint(0, len(possible_chars) - 1)]
  25. return rand_str
  26. # }}} generate_random_string #
  27. # token_expired {{{ #
  28. def token_expired(token_obtained_at, valid_for):
  29. """Returns True if token expired, False if otherwise
  30. Args:
  31. token_obtained_at: datetime object representing the date and time when the token was obtained
  32. valid_for: the time duration for which the token is valid, in seconds
  33. """
  34. time_elapsed = (datetime.today() - token_obtained_at).total_seconds()
  35. return time_elapsed >= valid_for
  36. # }}} token_expired #
  37. # index {{{ #
  38. # Create your views here.
  39. def index(request):
  40. return render(request, 'spotifyvis/index.html')
  41. # }}} index #
  42. # login {{{ #
  43. def login(request):
  44. # use a randomly generated state string to prevent cross-site request forgery attacks
  45. state_str = generate_random_string(16)
  46. request.session['state_string'] = state_str
  47. payload = {
  48. 'client_id': os.environ['SPOTIFY_CLIENT_ID'],
  49. 'response_type': 'code',
  50. 'redirect_uri': 'http://localhost:8000/callback',
  51. 'state': state_str,
  52. 'scope': 'user-library-read',
  53. 'show_dialog': False
  54. }
  55. params = urllib.parse.urlencode(payload) # turn the payload dict into a query string
  56. authorize_url = "https://accounts.spotify.com/authorize/?{}".format(params)
  57. return redirect(authorize_url)
  58. # }}} login #
  59. # callback {{{ #
  60. def callback(request):
  61. # Attempt to retrieve the authorization code from the query string
  62. try:
  63. code = request.GET['code']
  64. except KeyError:
  65. return HttpResponseBadRequest("<h1>Problem with login</h1>")
  66. payload = {
  67. 'grant_type': 'authorization_code',
  68. 'code': code,
  69. 'redirect_uri': 'http://localhost:8000/callback',
  70. 'client_id': os.environ['SPOTIFY_CLIENT_ID'],
  71. 'client_secret': os.environ['SPOTIFY_CLIENT_SECRET'],
  72. }
  73. response = requests.post('https://accounts.spotify.com/api/token', data = payload).json()
  74. # despite its name, datetime.today() returns a datetime object, not a date object
  75. # use datetime.strptime() to get a datetime object from a string
  76. request.session['token_obtained_at'] = datetime.strftime(datetime.today(), TIME_FORMAT)
  77. request.session['access_token'] = response['access_token']
  78. request.session['refresh_token'] = response['refresh_token']
  79. request.session['valid_for'] = response['expires_in']
  80. # print(response)
  81. return redirect('user_data')
  82. # }}} callback #
  83. # user_data {{{ #
  84. def user_data(request):
  85. token_obtained_at = datetime.strptime(request.session['token_obtained_at'], TIME_FORMAT)
  86. valid_for = int(request.session['valid_for'])
  87. if token_expired(token_obtained_at, valid_for):
  88. req_body = {
  89. 'grant_type': 'refresh_token',
  90. 'refresh_token': request.session['refresh_token'],
  91. 'client_id': os.environ['SPOTIFY_CLIENT_ID'],
  92. 'client_secret': os.environ['SPOTIFY_CLIENT_SECRET']
  93. }
  94. refresh_token_response = requests.post('https://accounts.spotify.com/api/token', data = req_body).json()
  95. request.session['access_token'] = refresh_token_response['access_token']
  96. request.session['valid_for'] = refresh_token_response['expires_in']
  97. auth_token_str = "Bearer " + request.session['access_token']
  98. headers = {
  99. 'Authorization': auth_token_str
  100. }
  101. user_data_response = requests.get('https://api.spotify.com/v1/me', headers = headers).json()
  102. context = {
  103. 'user_name': user_data_response['display_name'],
  104. 'id': user_data_response['id'],
  105. }
  106. tracks_to_query = 5
  107. parse_library(headers, tracks_to_query)
  108. return render(request, 'spotifyvis/user_data.html', context)
  109. # }}} user_data #
  110. def get_audio_features(track_id, headers):
  111. """Returns the audio features of a soundtrack
  112. Args:
  113. track_id: the id of the soundtrack, needed to query the Spotify API
  114. headers: headers containing the API token
  115. Returns:
  116. A dictionary with the features as its keys
  117. """
  118. response = requests.get("https://api.spotify.com/v1/audio-features/{}".format(track_id), headers = headers).json()
  119. features_dict = {}
  120. # Data that we don't need
  121. useless_keys = [
  122. "key", "mode", "type", "liveness", "id", "uri", "track_href", "analysis_url", "time_signature",
  123. ]
  124. for key, val in response.items():
  125. if key not in useless_keys:
  126. features_dict[key] = val
  127. return features_dict
  128. def update_std_dev(cur_mean, new_data_point, sample_size):
  129. """Calculates the standard deviation for a sample without storing all data points
  130. Args:
  131. cur_mean: the current mean for N = (sample_size - 1)
  132. new_data_point: a new data point
  133. sample_size: sample size including the new data point
  134. Returns:
  135. (updated_mean, std_dev)
  136. """
  137. # This is an implementationof Welford's method
  138. # http://jonisalonen.com/2013/deriving-welfords-method-for-computing-variance/
  139. new_mean = ((sample_size - 1) * cur_mean + new_data_point) / sample_size
  140. std_dev = (new_data_point - new_mean) * (new_data_point - cur_mean)
  141. return new_mean, std_dev
  142. def update_audio_feature_stats(feature, new_data_point, sample_size):
  143. """Updates the audio feature statistics in library_stats
  144. Args:
  145. feature: the audio feature to be updated (string)
  146. new_data_point: new data to update the stats with
  147. sample_size: sample size including the new data point
  148. Returns:
  149. None
  150. """
  151. # first time the feature is considered
  152. if sample_size < 2:
  153. library_stats['audio_features'][feature] = {
  154. "average": new_data_point,
  155. "std_dev": 0,
  156. }
  157. else:
  158. current_mean = library_stats['audio_features'][feature]['average']
  159. updated_mean, std_dev = update_std_dev(current_mean, new_data_point, sample_size)
  160. library_stats['audio_features'][feature]['average'] = updated_mean
  161. library_stats['audio_features'][feature]['std_dev'] = std_dev
  162. # parse_library {{{ #
  163. def parse_library(headers, tracks):
  164. """Scans user's library for certain number of tracks to update library_stats with.
  165. :headers: For API call.
  166. :tracks: Number of tracks to get from user's library.
  167. :returns: None
  168. """
  169. # TODO: implement importing entire library with 0 as tracks param
  170. # number of tracks to get with each call
  171. limit = 5
  172. # keeps track of point to get songs from
  173. offset = 0
  174. payload = {'limit': str(limit)}
  175. for i in range(0, tracks, limit):
  176. payload['offset'] = str(offset)
  177. saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks', headers=headers, params=payload).json()
  178. for track_dict in saved_tracks_response['items']:
  179. get_track_info(track_dict['track'])
  180. # get_genre(headers, track_dict['track']['album']['id'])
  181. for artist_dict in track_dict['track']['artists']:
  182. increase_artist_count(headers, artist_dict['name'], artist_dict['id'])
  183. # calculates num_songs with offset + songs retrieved
  184. library_stats['num_songs'] = offset + len(saved_tracks_response['items'])
  185. offset += limit
  186. calculate_genres_from_artists(headers)
  187. pprint.pprint(library_stats)
  188. # }}} parse_library #
  189. # increase_nested_key {{{ #
  190. def increase_nested_key(top_key, nested_key, amount=1):
  191. """Increases count for the value of library_stats[top_key][nested_key]. Checks if nested_key exists already and takes
  192. appropriate action.
  193. :top_key: First key of library_stats.
  194. :nested_key: Key in top_key's dict for which we want to increase value of.
  195. :returns: None
  196. """
  197. if nested_key not in library_stats[top_key]:
  198. library_stats[top_key][nested_key] = amount
  199. else:
  200. library_stats[top_key][nested_key] += amount
  201. # }}} increase_nested_key #
  202. # increase_artist_count {{{ #
  203. def increase_artist_count(headers, artist_name, artist_id):
  204. """Increases count for artist in library_stats and stores the artist_id.
  205. :headers: For making the API call.
  206. :artist_name: Artist to increase count for.
  207. :artist_id: The Spotify ID for the artist.
  208. :returns: None
  209. """
  210. if artist_name not in library_stats['artists']:
  211. library_stats['artists'][artist_name] = {}
  212. library_stats['artists'][artist_name]['count'] = 1
  213. library_stats['artists'][artist_name]['id'] = artist_id
  214. else:
  215. library_stats['artists'][artist_name]['count'] += 1
  216. # }}} increase_artist_count #
  217. # get_track_info {{{ #
  218. def get_track_info(track_dict):
  219. """Get all the info from the track_dict directly returned by the API call in parse_library.
  220. :track_dict: Dict returned from the API call containing the track info.
  221. :returns: None
  222. """
  223. # popularity
  224. library_stats['popularity'].append(track_dict['popularity'])
  225. # year
  226. year_released = track_dict['album']['release_date'].split('-')[0]
  227. increase_nested_key('year_released', year_released)
  228. # artist
  229. # artist_names = [artist['name'] for artist in track_dict['artists']]
  230. # for artist_name in artist_names:
  231. # increase_nested_key('artists', artist_name)
  232. # runtime
  233. library_stats['total_runtime'] += float(track_dict['duration_ms']) / 60
  234. # }}} get_track_info #
  235. # calculate_genres_from_artists {{{ #
  236. def calculate_genres_from_artists(headers):
  237. """Tallies up genre counts based on artists in library_stats.
  238. :headers: For making the API call.
  239. :returns: None
  240. """
  241. for artist_entry in library_stats['artists'].values():
  242. artist_response = requests.get('https://api.spotify.com/v1/artists/' + artist_entry['id'], headers=headers).json()
  243. # increase each genre count by artist count
  244. for genre in artist_response['genres']:
  245. increase_nested_key('genres', genre, artist_entry['count'])
  246. # }}} calculate_genres_from_artists #