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.

51 lines
1.4 KiB

  1. from django.shortcuts import render, redirect
  2. from django.http import HttpResponse
  3. import math
  4. import random
  5. import requests
  6. import os
  7. import urllib
  8. def generate_random_string(length):
  9. """Generates a random string of a certain length
  10. Args:
  11. length: the desired length of the randomized string
  12. Returns:
  13. A random string
  14. """
  15. rand_str = ""
  16. possible_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  17. for _ in range(length):
  18. rand_str += possible_chars[random.randint(0, len(possible_chars) - 1)]
  19. return rand_str
  20. # Create your views here.
  21. def index(request):
  22. return render(request, 'spotifyvis/index.html')
  23. def login(request):
  24. state_str = generate_random_string(16)
  25. # use a randomly generated state string to prevent cross-site request forgery attacks
  26. request.session['state_string'] = state_str
  27. payload = {
  28. 'client_id': os.environ['SPOTIFY_CLIENT_ID'],
  29. 'response_type': 'code',
  30. 'redirect_uri': 'http://localhost:8000/callback',
  31. 'state': state_str,
  32. 'scope': 'user-library-read',
  33. 'show_dialog': False
  34. }
  35. params = urllib.parse.urlencode(payload) # turn the payload dict into a query string
  36. authorize_url = "https://accounts.spotify.com/authorize/?{}".format(params)
  37. return redirect(authorize_url)
  38. def callback(request):
  39. return HttpResponse("At callback")