|
@ -2,7 +2,6 @@ from flask import Flask, Response, jsonify, render_template |
|
|
from base64 import b64encode |
|
|
from base64 import b64encode |
|
|
|
|
|
|
|
|
from dotenv import load_dotenv, find_dotenv |
|
|
from dotenv import load_dotenv, find_dotenv |
|
|
|
|
|
|
|
|
load_dotenv(find_dotenv()) |
|
|
load_dotenv(find_dotenv()) |
|
|
|
|
|
|
|
|
import requests |
|
|
import requests |
|
@ -10,14 +9,11 @@ import json |
|
|
import os |
|
|
import os |
|
|
import random |
|
|
import random |
|
|
|
|
|
|
|
|
print("Starting Server") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SPOTIFY_CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID") |
|
|
SPOTIFY_CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID") |
|
|
SPOTIFY_SECRET_ID = os.getenv("SPOTIFY_SECRET_ID") |
|
|
SPOTIFY_SECRET_ID = os.getenv("SPOTIFY_SECRET_ID") |
|
|
SPOTIFY_REFRESH_TOKEN = os.getenv("SPOTIFY_REFRESH_TOKEN") |
|
|
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_REFRESH_TOKEN = "https://accounts.spotify.com/api/token" |
|
|
SPOTIFY_URL_NOW_PLAYING = "https://api.spotify.com/v1/me/player/currently-playing" |
|
|
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" |
|
|
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__) |
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_authorization(): |
|
|
|
|
|
|
|
|
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 refresh_token(): |
|
|
|
|
|
|
|
|
def refreshToken(): |
|
|
|
|
|
|
|
|
data = { |
|
|
data = { |
|
|
"grant_type": "refresh_token", |
|
|
"grant_type": "refresh_token", |
|
|
"refresh_token": SPOTIFY_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) |
|
|
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(): |
|
|
def get_recently_play(): |
|
|
|
|
|
|
|
|
token = refresh_token() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
token = refreshToken() |
|
|
headers = {"Authorization": f"Bearer {token}"} |
|
|
headers = {"Authorization": f"Bearer {token}"} |
|
|
|
|
|
|
|
|
response = requests.get(SPOTIFY_URL_RECENTLY_PLAY, headers=headers) |
|
|
response = requests.get(SPOTIFY_URL_RECENTLY_PLAY, headers=headers) |
|
|
|
|
|
|
|
|
if response.status_code == 204: |
|
|
if response.status_code == 204: |
|
|
return {} |
|
|
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}"} |
|
|
headers = {"Authorization": f"Bearer {token}"} |
|
|
|
|
|
|
|
@ -71,43 +61,34 @@ def get_now_playing(): |
|
|
if response.status_code == 204: |
|
|
if response.status_code == 204: |
|
|
return {} |
|
|
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 |
|
|
left = 1 |
|
|
for i in range(1, num_bar + 1): |
|
|
for i in range(1, num_bar + 1): |
|
|
|
|
|
|
|
|
anim = random.randint(350, 500) |
|
|
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 |
|
|
i, left, anim |
|
|
) |
|
|
) |
|
|
left += 4 |
|
|
left += 4 |
|
|
|
|
|
|
|
|
return css_bar |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_image_b64(url): |
|
|
|
|
|
|
|
|
return barCSS |
|
|
|
|
|
|
|
|
|
|
|
def loadImageB64(url): |
|
|
resposne = requests.get(url) |
|
|
resposne = requests.get(url) |
|
|
return b64encode(resposne.content).decode("ascii") |
|
|
return b64encode(resposne.content).decode("ascii") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_svg(data): |
|
|
|
|
|
|
|
|
def makeSVG(data): |
|
|
|
|
|
|
|
|
height = 445 |
|
|
height = 445 |
|
|
num_bar = 75 |
|
|
|
|
|
title_text = "Now playing" |
|
|
|
|
|
|
|
|
num_bar = 85 |
|
|
content_bar = "".join(["<div class='bar'></div>" for i in range(num_bar)]) |
|
|
content_bar = "".join(["<div class='bar'></div>" for i in range(num_bar)]) |
|
|
css_bar = generate_css_bar(num_bar) |
|
|
|
|
|
|
|
|
css_bar = barGen(num_bar) |
|
|
|
|
|
|
|
|
if data == {}: |
|
|
if data == {}: |
|
|
# Get recently play |
|
|
|
|
|
title_text = "Last listening to" |
|
|
|
|
|
content_bar = "" |
|
|
content_bar = "" |
|
|
|
|
|
|
|
|
recent_plays = get_recently_play() |
|
|
recent_plays = get_recently_play() |
|
|
size_recent_play = len(recent_plays["items"]) |
|
|
size_recent_play = len(recent_plays["items"]) |
|
|
idx = random.randint(0, size_recent_play - 1) |
|
|
idx = random.randint(0, size_recent_play - 1) |
|
@ -115,9 +96,9 @@ def make_svg(data): |
|
|
else: |
|
|
else: |
|
|
item = data["item"] |
|
|
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"] |
|
|
url = item["external_urls"]["spotify"] |
|
|
|
|
|
|
|
|
rendered_data = { |
|
|
rendered_data = { |
|
@ -134,19 +115,17 @@ def make_svg(data): |
|
|
|
|
|
|
|
|
return render_template("spotify.html.j2", **rendered_data) |
|
|
return render_template("spotify.html.j2", **rendered_data) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/", defaults={"path": ""}) |
|
|
@app.route("/", defaults={"path": ""}) |
|
|
@app.route("/<path:path>") |
|
|
@app.route("/<path:path>") |
|
|
def catch_all(path): |
|
|
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 = Response(svg, mimetype="image/svg+xml") |
|
|
resp.headers["Cache-Control"] = "s-maxage=1" |
|
|
resp.headers["Cache-Control"] = "s-maxage=1" |
|
|
|
|
|
|
|
|
return resp |
|
|
return resp |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
if __name__ == "__main__": |
|
|
app.run(debug=True) |
|
|
app.run(debug=True) |