import os import json import random import requests from base64 import b64encode from dotenv import load_dotenv, find_dotenv from flask import Flask, Response, jsonify, render_template load_dotenv(find_dotenv()) # Spotify scopes: # user-read-currently-playing # user-read-recently-played SPOTIFY_CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID") SPOTIFY_SECRET_ID = os.getenv("SPOTIFY_SECRET_ID") SPOTIFY_REFRESH_TOKEN = os.getenv("SPOTIFY_REFRESH_TOKEN") REFRESH_TOKEN_URL = "https://accounts.spotify.com/api/token" NOW_PLAYING_URL = "https://api.spotify.com/v1/me/player/currently-playing" RECENTLY_PLAYING_URL = ( "https://api.spotify.com/v1/me/player/recently-played?limit=10" ) app = Flask(__name__) def getAuth(): return b64encode(f"{SPOTIFY_CLIENT_ID}:{SPOTIFY_SECRET_ID}".encode()).decode( "ascii" ) def refreshToken(): data = { "grant_type": "refresh_token", "refresh_token": SPOTIFY_REFRESH_TOKEN, } headers = {"Authorization": "Basic {}".format(getAuth())} response = requests.post(REFRESH_TOKEN_URL, data=data, headers=headers) try: return response.json()["access_token"] except KeyError: print(json.dumps(response.json())) print("\n---\n") raise KeyError(str(response.json())) def recentlyPlayed(): token = refreshToken() headers = {"Authorization": f"Bearer {token}"} response = requests.get(RECENTLY_PLAYING_URL, headers=headers) if response.status_code == 204: return {} return response.json() def nowPlaying(): token = refreshToken() headers = {"Authorization": f"Bearer {token}"} response = requests.get(NOW_PLAYING_URL, headers=headers) if response.status_code == 204: return {} return response.json() def barGen(barCount): barCSS = "" left = 1 for i in range(1, barCount + 1): anim = random.randint(1000, 1350) barCSS += ( ".bar:nth-child({}) {{ left: {}px; animation-duration: {}ms; }}".format( i, left, anim ) ) left += 4 return barCSS def loadImageB64(url): resposne = requests.get(url) return b64encode(resposne.content).decode("ascii") def makeSVG(data): barCount = 84 contentBar = "".join(["
" for i in range(barCount)]) barCSS = barGen(barCount) if data == {} or data["item"] == "None": # contentBar = "" #Shows/Hides the EQ bar if no song is currently playing currentStatus = "Was playing:" recentPlays = recentlyPlayed() recentPlaysLength = len(recentPlays["items"]) itemIndex = random.randint(0, recentPlaysLength - 1) item = recentPlays["items"][itemIndex]["track"] else: item = data["item"] currentStatus = "Vibing to:" image = loadImageB64(item["album"]["images"][1]["url"]) artistName = item["artists"][0]["name"].replace("&", "&") songName = item["name"].replace("&", "&") dataDict = { "contentBar": contentBar, "barCSS": barCSS, "artistName": artistName, "songName": songName, "image": image, "status": currentStatus, } return render_template("spotify.html.j2", **dataDict) @app.route("/", defaults={"path": ""}) @app.route("/