from django.shortcuts import render, redirect from django.http import HttpResponse, HttpResponseBadRequest import math import random import requests import os import urllib import json import pprint from datetime import datetime from .utils import * TIME_FORMAT = '%Y-%m-%d-%H-%M-%S' library_stats = {"audio_features":{}, "genres":{}, "year_released":{}, "artists":{}, "num_songs":0, "popularity":[], "total_runtime":0} # generate_random_string {{{ # def generate_random_string(length): """Generates a random string of a certain length Args: length: the desired length of the randomized string Returns: A random string """ rand_str = "" possible_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" for _ in range(length): rand_str += possible_chars[random.randint(0, len(possible_chars) - 1)] return rand_str # }}} generate_random_string # # token_expired {{{ # def token_expired(token_obtained_at, valid_for): """Returns True if token expired, False if otherwise Args: token_obtained_at: datetime object representing the date and time when the token was obtained valid_for: the time duration for which the token is valid, in seconds """ time_elapsed = (datetime.today() - token_obtained_at).total_seconds() return time_elapsed >= valid_for # }}} token_expired # # index {{{ # # Create your views here. def index(request): return render(request, 'spotifyvis/index.html') # }}} index # # login {{{ # def login(request): # use a randomly generated state string to prevent cross-site request forgery attacks state_str = generate_random_string(16) request.session['state_string'] = state_str payload = { 'client_id': os.environ['SPOTIFY_CLIENT_ID'], 'response_type': 'code', 'redirect_uri': 'http://localhost:8000/callback', 'state': state_str, 'scope': 'user-library-read', 'show_dialog': False } params = urllib.parse.urlencode(payload) # turn the payload dict into a query string authorize_url = "https://accounts.spotify.com/authorize/?{}".format(params) return redirect(authorize_url) # }}} login # # callback {{{ # def callback(request): # Attempt to retrieve the authorization code from the query string try: code = request.GET['code'] except KeyError: return HttpResponseBadRequest("