From 5a6862c5484775182a8b9d0bd45d7954406be179 Mon Sep 17 00:00:00 2001 From: novatorem Date: Mon, 20 Jul 2020 22:33:20 -0400 Subject: [PATCH] Code cleanup --- api/spotify-playing.py | 67 +++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 44 deletions(-) diff --git a/api/spotify-playing.py b/api/spotify-playing.py index 5f12942..e21bb4e 100644 --- a/api/spotify-playing.py +++ b/api/spotify-playing.py @@ -2,7 +2,6 @@ from flask import Flask, Response, jsonify, render_template from base64 import b64encode from dotenv import load_dotenv, find_dotenv - load_dotenv(find_dotenv()) import requests @@ -10,14 +9,11 @@ import json import os import random -print("Starting Server") - - SPOTIFY_CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID") SPOTIFY_SECRET_ID = os.getenv("SPOTIFY_SECRET_ID") SPOTIFY_REFRESH_TOKEN = os.getenv("SPOTIFY_REFRESH_TOKEN") -# scope user-read-currently-playing,user-read-recently-played +# scope user-read-currently-playing/user-read-recently-played 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" @@ -26,43 +22,37 @@ SPOTIFY_URL_RECENTLY_PLAY = "https://api.spotify.com/v1/me/player/recently-playe app = Flask(__name__) -def get_authorization(): +def getAuth(): return b64encode(f"{SPOTIFY_CLIENT_ID}:{SPOTIFY_SECRET_ID}".encode()).decode("ascii") -def refresh_token(): +def refreshToken(): data = { "grant_type": "refresh_token", "refresh_token": SPOTIFY_REFRESH_TOKEN, } - headers = {"Authorization": "Basic {}".format(get_authorization())} + headers = {"Authorization": "Basic {}".format(getAuth())} response = requests.post(SPOTIFY_URL_REFRESH_TOKEN, data=data, headers=headers) - repsonse_json = response.json() - return repsonse_json["access_token"] - + return response.json()["access_token"] def get_recently_play(): - token = refresh_token() - + token = refreshToken() headers = {"Authorization": f"Bearer {token}"} - response = requests.get(SPOTIFY_URL_RECENTLY_PLAY, headers=headers) if response.status_code == 204: return {} - repsonse_json = response.json() - return repsonse_json - + return response.json() -def get_now_playing(): +def nowPlaying(): - token = refresh_token() + token = refreshToken() headers = {"Authorization": f"Bearer {token}"} @@ -71,43 +61,34 @@ def get_now_playing(): if response.status_code == 204: return {} - repsonse_json = response.json() - return repsonse_json + return response.json() - -def generate_css_bar(num_bar=85): - css_bar = "" +def barGen(num_bar=85): + barCSS = "" left = 1 for i in range(1, num_bar + 1): anim = random.randint(350, 500) - css_bar += ".bar:nth-child({}) {{ left: {}px; animation-duration: {}ms; }}".format( + barCSS += ".bar:nth-child({}) {{ left: {}px; animation-duration: {}ms; }}".format( i, left, anim ) left += 4 - return css_bar - - -def load_image_b64(url): + return barCSS +def loadImageB64(url): resposne = requests.get(url) return b64encode(resposne.content).decode("ascii") - -def make_svg(data): +def makeSVG(data): height = 445 - num_bar = 75 - title_text = "Now playing" + num_bar = 85 content_bar = "".join(["
" for i in range(num_bar)]) - css_bar = generate_css_bar(num_bar) + css_bar = barGen(num_bar) if data == {}: - # Get recently play - title_text = "Last listening to" content_bar = "" - recent_plays = get_recently_play() size_recent_play = len(recent_plays["items"]) idx = random.randint(0, size_recent_play - 1) @@ -115,9 +96,9 @@ def make_svg(data): else: item = data["item"] - img = load_image_b64(item["album"]["images"][1]["url"]) - artist_name = item["artists"][0]["name"] - song_name = item["name"] + img = loadImageB64(item["album"]["images"][1]["url"]) + artist_name = item["artists"][0]["name"].replace("&", "&") + song_name = item["name"].replace("&", "&") url = item["external_urls"]["spotify"] rendered_data = { @@ -134,19 +115,17 @@ def make_svg(data): return render_template("spotify.html.j2", **rendered_data) - @app.route("/", defaults={"path": ""}) @app.route("/") def catch_all(path): - data = get_now_playing() - svg = make_svg(data) + data = nowPlaying() + svg = makeSVG(data) resp = Response(svg, mimetype="image/svg+xml") resp.headers["Cache-Control"] = "s-maxage=1" return resp - if __name__ == "__main__": app.run(debug=True)