diff --git a/api/spotify.py b/api/spotify.py index da0a4ea..66cd1a0 100644 --- a/api/spotify.py +++ b/api/spotify.py @@ -9,21 +9,27 @@ from flask import Flask, Response, jsonify, render_template load_dotenv(find_dotenv()) -# Spotify: +# 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") -SPOTIFY_URL_REFRESH_TOKEN = "https://accounts.spotify.com/api/token" -SPOTIFY_URL_NOW_PLAYING = "https://api.spotify.com/v1/me/player/currently-playing" -SPOTIFY_URL_RECENTLY_PLAY = "https://api.spotify.com/v1/me/player/recently-played?limit=10" +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") + return b64encode(f"{SPOTIFY_CLIENT_ID}:{SPOTIFY_SECRET_ID}".encode()).decode( + "ascii" + ) + def refreshToken(): data = { @@ -33,53 +39,57 @@ def refreshToken(): headers = {"Authorization": "Basic {}".format(getAuth())} - response = requests.post(SPOTIFY_URL_REFRESH_TOKEN, data=data, headers=headers) + response = requests.post(REFRESH_TOKEN_URL, data=data, headers=headers) return response.json()["access_token"] + def recentlyPlayed(): token = refreshToken() headers = {"Authorization": f"Bearer {token}"} - response = requests.get(SPOTIFY_URL_RECENTLY_PLAY, headers=headers) + 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(SPOTIFY_URL_NOW_PLAYING, headers=headers) + 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 + 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 = "Last seen playing:" + 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) @@ -87,7 +97,6 @@ def makeSVG(data): else: item = data["item"] currentStatus = "Vibing to:" - image = loadImageB64(item["album"]["images"][1]["url"]) artistName = item["artists"][0]["name"].replace("&", "&") songName = item["name"].replace("&", "&") @@ -98,11 +107,12 @@ def makeSVG(data): "artistName": artistName, "songName": songName, "image": image, - "status": currentStatus + "status": currentStatus, } return render_template("spotify.html.j2", **dataDict) + @app.route("/", defaults={"path": ""}) @app.route("/") def catch_all(path): @@ -114,5 +124,6 @@ def catch_all(path): return resp + if __name__ == "__main__": app.run(debug=True) diff --git a/api/templates/spotify.html.j2 b/api/templates/spotify.html.j2 index 1d458ce..346a627 100644 --- a/api/templates/spotify.html.j2 +++ b/api/templates/spotify.html.j2 @@ -2,19 +2,15 @@