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.

167 lines
5.4 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. from .utils import parse_library, process_library_stats
  12. from .models import User, Track, AudioFeatures, Artist
  13. TIME_FORMAT = '%Y-%m-%d-%H-%M-%S'
  14. TRACKS_TO_QUERY = 5
  15. # generate_random_string {{{ #
  16. def generate_random_string(length):
  17. """Generates a random string of a certain length
  18. Args:
  19. length: the desired length of the randomized string
  20. Returns:
  21. A random string
  22. """
  23. rand_str = ""
  24. possible_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  25. for _ in range(length):
  26. rand_str += possible_chars[random.randint(0, len(possible_chars) - 1)]
  27. return rand_str
  28. # }}} generate_random_string #
  29. # token_expired {{{ #
  30. def token_expired(token_obtained_at, valid_for):
  31. """Returns True if token expired, False if otherwise
  32. Args:
  33. token_obtained_at: datetime object representing the date and time when the token was obtained
  34. valid_for: the time duration for which the token is valid, in seconds
  35. """
  36. time_elapsed = (datetime.today() - token_obtained_at).total_seconds()
  37. return time_elapsed >= valid_for
  38. # }}} token_expired #
  39. # index {{{ #
  40. # Create your views here.
  41. def index(request):
  42. return render(request, 'spotifyvis/index.html')
  43. # }}} index #
  44. # login {{{ #
  45. def login(request):
  46. # use a randomly generated state string to prevent cross-site request forgery attacks
  47. state_str = generate_random_string(16)
  48. request.session['state_string'] = state_str
  49. payload = {
  50. 'client_id': os.environ['SPOTIFY_CLIENT_ID'],
  51. 'response_type': 'code',
  52. 'redirect_uri': 'http://localhost:8000/callback',
  53. 'state': state_str,
  54. 'scope': 'user-library-read',
  55. 'show_dialog': False
  56. }
  57. params = urllib.parse.urlencode(payload) # turn the payload dict into a query string
  58. authorize_url = "https://accounts.spotify.com/authorize/?{}".format(params)
  59. return redirect(authorize_url)
  60. # }}} login #
  61. # callback {{{ #
  62. def callback(request):
  63. # Attempt to retrieve the authorization code from the query string
  64. try:
  65. code = request.GET['code']
  66. except KeyError:
  67. return HttpResponseBadRequest("<h1>Problem with login</h1>")
  68. payload = {
  69. 'grant_type': 'authorization_code',
  70. 'code': code,
  71. 'redirect_uri': 'http://localhost:8000/callback',
  72. 'client_id': os.environ['SPOTIFY_CLIENT_ID'],
  73. 'client_secret': os.environ['SPOTIFY_CLIENT_SECRET'],
  74. }
  75. response = requests.post('https://accounts.spotify.com/api/token', data = payload).json()
  76. # despite its name, datetime.today() returns a datetime object, not a date object
  77. # use datetime.strptime() to get a datetime object from a string
  78. request.session['token_obtained_at'] = datetime.strftime(datetime.today(), TIME_FORMAT)
  79. request.session['access_token'] = response['access_token']
  80. request.session['refresh_token'] = response['refresh_token']
  81. request.session['valid_for'] = response['expires_in']
  82. # print(response)
  83. return redirect('user_data')
  84. # }}} callback #
  85. # user_data {{{ #
  86. def user_data(request):
  87. token_obtained_at = datetime.strptime(request.session['token_obtained_at'], TIME_FORMAT)
  88. valid_for = int(request.session['valid_for'])
  89. if token_expired(token_obtained_at, valid_for):
  90. req_body = {
  91. 'grant_type': 'refresh_token',
  92. 'refresh_token': request.session['refresh_token'],
  93. 'client_id': os.environ['SPOTIFY_CLIENT_ID'],
  94. 'client_secret': os.environ['SPOTIFY_CLIENT_SECRET']
  95. }
  96. refresh_token_response = requests.post('https://accounts.spotify.com/api/token', data = req_body).json()
  97. request.session['access_token'] = refresh_token_response['access_token']
  98. request.session['valid_for'] = refresh_token_response['expires_in']
  99. auth_token_str = "Bearer " + request.session['access_token']
  100. headers = {
  101. 'Authorization': auth_token_str
  102. }
  103. user_data_response = requests.get('https://api.spotify.com/v1/me', headers = headers).json()
  104. request.session['user_id'] = user_data_response['id'] # store the user_id so it may be used to create model
  105. request.session['user_name'] = user_data_response['display_name']
  106. user = None # will be set to the current user object later
  107. try:
  108. user = User.objects.get(user_id=request.session['user_id'])
  109. except User.DoesNotExist:
  110. user = User.objects.create(user_id=request.session['user_id'], user_name=request.session['user_name'])
  111. # context = {
  112. # 'user_name': user_data_response['display_name'],
  113. # 'id': user_data_response['id'],
  114. # }
  115. library_stats = {
  116. "audio_features":{},
  117. "genres":{},
  118. "year_released":{},
  119. "artists":{},
  120. "num_songs": 0,
  121. "popularity": {
  122. "average": 0,
  123. "std_dev": 0,
  124. },
  125. "total_runtime": 0
  126. }
  127. parse_library(headers, TRACKS_TO_QUERY, library_stats, user)
  128. processed_library_stats = process_library_stats(library_stats)
  129. print("================================================")
  130. print("Processed data follows\n")
  131. pprint.pprint(processed_library_stats)
  132. return render(request, 'spotifyvis/user_data.html', context)
  133. # }}} user_data #