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.

393 lines
13 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # imports {{{ #
  2. import requests
  3. import math
  4. import os
  5. import json
  6. from django.db.models import Count, F, Max
  7. from django.db import IntegrityError
  8. from django.http import JsonResponse
  9. from django.core import serializers
  10. from django.utils import timezone
  11. from .models import *
  12. from . import views
  13. from login.models import User
  14. from pprint import pprint
  15. from dateutil.parser import parse
  16. from datetime import datetime
  17. from django.db.models import FloatField
  18. from django.db.models.functions import Cast
  19. HISTORY_ENDPOINT = 'https://api.spotify.com/v1/me/player/recently-played'
  20. # }}} imports #
  21. # console_logging = True
  22. console_logging = False
  23. artists_genre_processed = 0
  24. features_processed = 0
  25. # update_track_genres {{{ #
  26. def update_track_genres(user_obj):
  27. """Updates user_obj's tracks with the most common genre associated with the
  28. songs' artist(s).
  29. :user_obj: User object who's tracks are being updated.
  30. :returns: None
  31. """
  32. tracks_processed = 0
  33. user_tracks = Track.objects.filter(users__exact=user_obj)
  34. for track in user_tracks:
  35. # just using this variable to save another call to db
  36. track_artists = list(track.artists.all())
  37. # TODO: Use the most popular genre of the first artist as the Track genre
  38. first_artist_genres = track_artists[0].genres.all().order_by('-num_songs')
  39. undefined_genre_obj = Genre.objects.get(name="undefined")
  40. most_common_genre = first_artist_genres.first() if first_artist_genres.first() is \
  41. not undefined_genre_obj else first_artist_genres[1]
  42. track.genre = most_common_genre if most_common_genre is not None \
  43. else undefined_genre_obj
  44. track.save()
  45. tracks_processed += 1
  46. if console_logging:
  47. print("Added '{}' as genre for song #{} - '{}'".format(
  48. track.genre,
  49. tracks_processed,
  50. track.name,
  51. ))
  52. # }}} update_track_genres #
  53. # save_track_obj {{{ #
  54. def save_track_obj(track_dict, artists, user_obj):
  55. """Make an entry in the database for this track if it doesn't exist already.
  56. :track_dict: dictionary from the API call containing track information.
  57. :artists: artists of the song, passed in as a list of Artist objects.
  58. :user_obj: User object for which this Track is to be associated with.
  59. :returns: (The created/retrieved Track object, created)
  60. """
  61. track_query = Track.objects.filter(id__exact=track_dict['id'])
  62. if len(track_query) != 0:
  63. return track_query[0], False
  64. else:
  65. # check if track is simple or full, simple Track object won't have year
  66. # if 'album' in track_dict:
  67. if 'release_date' in track_dict['album']:
  68. # try:
  69. new_track = Track.objects.create(
  70. id=track_dict['id'],
  71. year=track_dict['album']['release_date'].split('-')[0],
  72. popularity=int(track_dict['popularity']),
  73. runtime=int(float(track_dict['duration_ms']) / 1000),
  74. name=track_dict['name'],
  75. )
  76. else:
  77. # except (IntegrityError, KeyError) as e:
  78. new_track = Track.objects.create(
  79. id=track_dict['id'],
  80. popularity=int(track_dict['popularity']),
  81. runtime=int(float(track_dict['duration_ms']) / 1000),
  82. name=track_dict['name'],
  83. )
  84. # have to add artists and user_obj after saving object since track needs to
  85. # have ID before filling in m2m field
  86. for artist in artists:
  87. new_track.artists.add(artist)
  88. # print(new_track.name, artist.name)
  89. if user_obj != None:
  90. new_track.users.add(user_obj)
  91. new_track.save()
  92. return new_track, True
  93. # }}} save_track_obj #
  94. # get_audio_features {{{ #
  95. def get_audio_features(headers, track_objs):
  96. """Creates and saves a new AudioFeatures objects for the respective
  97. track_objs. track_objs should contain the API limit for a single call
  98. (FEATURES_LIMIT) for maximum efficiency.
  99. :headers: headers containing the API token
  100. :track_objs: Track objects to associate with the new AudioFeatures object
  101. :returns: None
  102. """
  103. track_ids = str.join(",", [track_obj.id for track_obj in track_objs])
  104. params = {'ids': track_ids}
  105. features_response = requests.get("https://api.spotify.com/v1/audio-features",
  106. headers=headers,
  107. params={'ids': track_ids}
  108. ).json()['audio_features']
  109. # pprint.pprint(features_response)
  110. useless_keys = [ "key", "mode", "type", "liveness", "id", "uri",
  111. "track_href", "analysis_url", "time_signature", ]
  112. for i in range(len(track_objs)):
  113. if features_response[i] is not None:
  114. # Data that we don't need
  115. cur_features_obj = AudioFeatures()
  116. cur_features_obj.track = track_objs[i]
  117. for key, val in features_response[i].items():
  118. if key not in useless_keys:
  119. setattr(cur_features_obj, key, val)
  120. cur_features_obj.save()
  121. if console_logging:
  122. global features_processed
  123. features_processed += 1
  124. print("Added features for song #{} - {}".format(
  125. features_processed, track_objs[i].name))
  126. # }}} get_audio_features #
  127. # process_artist_genre {{{ #
  128. def process_artist_genre(genre_name, artist_obj):
  129. """Increase count for corresponding Genre object to genre_name and associate that
  130. Genre object with artist_obj.
  131. :genre_name: Name of genre.
  132. :artist_obj: Artist object to associate Genre object with
  133. :returns: None
  134. """
  135. genre_obj, created = Genre.objects.get_or_create(name=genre_name, defaults={'num_songs': 1})
  136. if not created:
  137. genre_obj.num_songs = F('num_songs') + 1
  138. genre_obj.save()
  139. artist_obj.genres.add(genre_obj)
  140. artist_obj.save()
  141. # }}} process_artist_genre #
  142. # add_artist_genres {{{ #
  143. def add_artist_genres(headers, artist_objs):
  144. """Adds genres to artist_objs and increases the count the respective Genre
  145. object. artist_objs should contain the API limit for a single call
  146. (ARTIST_LIMIT) for maximum efficiency.
  147. :headers: For making the API call.
  148. :artist_objs: List of Artist objects for which to add/tally up genres for.
  149. :returns: None
  150. """
  151. artist_ids = str.join(",", [artist_obj.id for artist_obj in artist_objs])
  152. artists_response = requests.get('https://api.spotify.com/v1/artists/',
  153. headers=headers,
  154. params={'ids': artist_ids},
  155. ).json()['artists']
  156. for i in range(len(artist_objs)):
  157. if len(artists_response[i]['genres']) == 0:
  158. process_artist_genre("undefined", artist_objs[i])
  159. else:
  160. for genre in artists_response[i]['genres']:
  161. process_artist_genre(genre, artist_objs[i])
  162. # print(artist_objs[i].name, genre)
  163. if console_logging:
  164. global artists_genre_processed
  165. artists_genre_processed += 1
  166. print("Added genres for artist #{} - {}".format(
  167. artists_genre_processed, artist_objs[i].name))
  168. # }}} add_artist_genres #
  169. # get_artists_in_genre {{{ #
  170. def get_artists_in_genre(user, genre):
  171. """Return count of artists in genre.
  172. :user: User object to return data for.
  173. :genre: genre to count artists for. (string)
  174. :returns: dict of artists in the genre along with the number of songs they
  175. have.
  176. """
  177. genre_obj = Genre.objects.get(name=genre)
  178. tracks_in_genre = Track.objects.filter(genre=genre_obj, users=user)
  179. track_count = tracks_in_genre.count()
  180. user_artists = Artist.objects.filter(track__users=user) # use this variable to save on db queries
  181. total_artist_counts = tracks_in_genre.aggregate(counts=Count('artists'))['counts']
  182. processed_artist_counts = {}
  183. for artist in user_artists:
  184. # TODO: figure out collab problem #
  185. # artist_count = math.floor(artist.track_set.filter(genre=genre_obj,
  186. # users=user).count() * track_count / total_artist_counts)
  187. artist_count = artist.track_set.filter(genre=genre_obj,
  188. users=user).count()
  189. if artist_count > 0:
  190. processed_artist_counts[artist.name] = artist_count
  191. return processed_artist_counts
  192. # }}} get_artists_in_genre #
  193. # save_track_artists {{{ #
  194. def save_track_artists(track_dict, artist_genre_queue, user_headers):
  195. """ Update artist info before creating Track so that Track object can
  196. reference Artist object.
  197. :track_dict: response from Spotify API for track
  198. :returns: list of Artist objects in Track
  199. """
  200. track_artists = []
  201. for artist_dict in track_dict['artists']:
  202. artist_obj, artist_created = Artist.objects.get_or_create(
  203. id=artist_dict['id'],
  204. name=artist_dict['name'],)
  205. # only add/tally up artist genres if new
  206. if artist_created:
  207. artist_genre_queue.append(artist_obj)
  208. if len(artist_genre_queue) == views.ARTIST_LIMIT:
  209. add_artist_genres(user_headers, artist_genre_queue)
  210. artist_genre_queue[:] = []
  211. track_artists.append(artist_obj)
  212. return track_artists
  213. # }}} save_track_artists #
  214. # get_user_header {{{ #
  215. def get_user_header(user_obj):
  216. """Returns the authorization string needed to make an API call.
  217. :user_obj: User to return the auth string for.
  218. :returns: the authorization string used for the header in a Spotify API
  219. call.
  220. """
  221. seconds_elapsed = (timezone.now() -
  222. user_obj.access_obtained_at).total_seconds()
  223. if seconds_elapsed >= user_obj.access_expires_in:
  224. req_body = {
  225. 'grant_type': 'refresh_token',
  226. 'refresh_token': user_obj.refresh_token,
  227. 'client_id': os.environ['SPOTIFY_CLIENT_ID'],
  228. 'client_secret': os.environ['SPOTIFY_CLIENT_SECRET']
  229. }
  230. token_response = requests.post('https://accounts.spotify.com/api/token',
  231. data=req_body).json()
  232. user_obj.access_token = token_response['access_token']
  233. user_obj.access_expires_in = token_response['expires_in']
  234. user_obj.save()
  235. return {'Authorization': "Bearer " + user_obj.access_token}
  236. # }}} get_user_header #
  237. # save_history_obj {{{ #
  238. def save_history_obj (user, timestamp, track):
  239. """Return (get/create) a History object with the specified parameters. Can't
  240. use built-in get_or_create since don't know auto PK.
  241. :user: User object History should be associated with
  242. :timestamp: time at which song was listened to
  243. :track: Track object for song
  244. :returns: History object
  245. """
  246. history_query = History.objects.filter(user__exact=user,
  247. timestamp__exact=timestamp)
  248. if len(history_query) == 0:
  249. history_obj = History.objects.create(user=user, timestamp=timestamp,
  250. track=track)
  251. else:
  252. history_obj = history_query[0]
  253. return history_obj
  254. # }}} save_history_obj #
  255. # get_next_history_row {{{ #
  256. def get_next_history_row(csv_reader, headers, prev_info):
  257. """Return formatted information from next row in history CSV file.
  258. :csv_reader: TODO
  259. :headers:
  260. :prev_info: history_obj_info of last row in case no more rows
  261. :returns: (boolean of if last row, dict with information of next row)
  262. """
  263. try:
  264. row = next(csv_reader)
  265. # if Track.objects.filter(id__exact=row[1]).exists():
  266. history_obj_info = {}
  267. for i in range(len(headers)):
  268. history_obj_info[headers[i]] = row[i]
  269. return False, history_obj_info
  270. except StopIteration:
  271. return True, prev_info
  272. # }}} get_next_history_row #
  273. # parse_history {{{ #
  274. def parse_history(user_secret):
  275. """Scans user's listening history and stores the information in a
  276. database.
  277. :user_secret: secret for User object who's library is being scanned.
  278. :returns: None
  279. """
  280. user_obj = User.objects.get(secret=user_secret)
  281. payload = {'limit': str(views.USER_TRACKS_LIMIT)}
  282. last_time_played = History.objects.filter(user=user_obj).aggregate(Max('timestamp'))['timestamp__max']
  283. if last_time_played is not None:
  284. payload['after'] = last_time_played.isoformat()
  285. artist_genre_queue = []
  286. user_headers = get_user_header(user_obj)
  287. history_response = requests.get(HISTORY_ENDPOINT,
  288. headers=user_headers,
  289. params=payload).json()['items']
  290. # pprint(history_response)
  291. tracks_processed = 0
  292. for track_dict in history_response:
  293. # don't associate history track with User, not necessarily in their
  294. # library
  295. # track_obj, track_created = save_track_obj(track_dict['track'],
  296. # track_artists, None)
  297. track_artists = save_track_artists(track_dict['track'], artist_genre_queue,
  298. user_headers)
  299. track_obj, track_created = save_track_obj(track_dict['track'],
  300. track_artists, None)
  301. history_obj = save_history_obj(user_obj, parse(track_dict['played_at']),
  302. track_obj)
  303. tracks_processed += 1
  304. if console_logging:
  305. print("Added history track #{}: {}".format(
  306. tracks_processed, history_obj,))
  307. if len(artist_genre_queue) > 0:
  308. add_artist_genres(user_headers, artist_genre_queue)
  309. # TODO: update track genres from History relation
  310. # update_track_genres(user_obj)
  311. print("Scanned {} history tracks for user {} at {}.".format(
  312. tracks_processed, user_obj.id, datetime.now()))
  313. # }}} get_history #