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.

110 lines
3.3 KiB

6 years ago
  1. # imports {{{ #
  2. import math
  3. import os
  4. import urllib
  5. import pprint
  6. from datetime import datetime
  7. from django.shortcuts import render, redirect
  8. from django.http import HttpResponseBadRequest
  9. from .models import *
  10. from .utils import *
  11. # }}} imports #
  12. TIME_FORMAT = '%Y-%m-%d-%H-%M-%S'
  13. TRACKS_TO_QUERY = 200
  14. AUTH_SCOPE = ['user-library-read', 'user-read-recently-played', ]
  15. # ROOT_URL = 'http://localhost:8000'
  16. ROOT_URL = 'http://spotify-lib-vis.ml'
  17. # index {{{ #
  18. def index(request):
  19. return render(request, 'login/index.html')
  20. # }}} index #
  21. # spotify_login {{{ #
  22. def spotify_login(request):
  23. """ Step 1 in authorization flow: Have your application request
  24. authorization; the user logs in and authorizes access.
  25. """
  26. # use a randomly generated state string to prevent cross-site request forgery attacks
  27. state_str = generate_random_string(16)
  28. request.session['state_string'] = state_str
  29. payload = {
  30. 'client_id': os.environ['SPOTIFY_CLIENT_ID'],
  31. 'response_type': 'code',
  32. 'redirect_uri': ROOT_URL + '/login/callback',
  33. 'state': state_str,
  34. 'scope': " ".join(AUTH_SCOPE),
  35. 'show_dialog': False
  36. }
  37. params = urllib.parse.urlencode(payload) # turn the payload dict into a query string
  38. authorize_url = "https://accounts.spotify.com/authorize/?{}".format(params)
  39. return redirect(authorize_url)
  40. # }}} spotify_login #
  41. # callback {{{ #
  42. def callback(request):
  43. """ Step 2 in authorization flow: Have your application request refresh and
  44. access tokens; Spotify returns access and refresh tokens.
  45. """
  46. # Attempt to retrieve the authorization code from the query string
  47. try:
  48. code = request.GET['code']
  49. except KeyError:
  50. return HttpResponseBadRequest("<h1>Problem with login</h1>")
  51. payload = {
  52. 'grant_type': 'authorization_code',
  53. 'code': code,
  54. 'redirect_uri': ROOT_URL + '/login/callback',
  55. 'client_id': os.environ['SPOTIFY_CLIENT_ID'],
  56. 'client_secret': os.environ['SPOTIFY_CLIENT_SECRET'],
  57. }
  58. token_response = requests.post('https://accounts.spotify.com/api/token', data=payload).json()
  59. user_obj = create_user(token_response['refresh_token'],
  60. token_response['access_token'],
  61. token_response['expires_in'])
  62. request.session['user_id'] = user_obj.id
  63. request.session['user_secret'] = user_obj.secret
  64. return render(request, 'login/scan.html', get_scan_context(request))
  65. # }}} callback #
  66. # admin_graphs {{{ #
  67. def admin_graphs(request):
  68. """TODO
  69. """
  70. user_id = "polarbier"
  71. # user_id = "chrisshyi13"
  72. request.session['user_id'] = user_id
  73. # request.session['user_secret'] = user_obj.secret
  74. request.session['user_secret'] = User.objects.get(id=user_id).secret
  75. user_obj = User.objects.get(id=user_id)
  76. return render(request, 'graphs/logged_in.html', get_user_context(user_obj))
  77. # }}} admin_graphs #
  78. def upload_history(request):
  79. if request.method == 'POST':
  80. form = HistoryUploadForm(request.POST, request.FILES)
  81. if form.is_valid():
  82. upload_obj = form.save()
  83. # return redirect('graphs:display_history_table')
  84. return redirect('api:import_history', upload_id=upload_obj.id)
  85. return render(request, 'login/scan.html', get_scan_context(request))