Legibility improvements

This commit is contained in:
novatorem
2020-09-01 19:13:53 -04:00
parent 9ccf8c2ce1
commit ab4288bfd0
2 changed files with 46 additions and 39 deletions

View File

@@ -9,21 +9,27 @@ from flask import Flask, Response, jsonify, render_template
load_dotenv(find_dotenv()) load_dotenv(find_dotenv())
# Spotify: # Spotify scopes:
# user-read-currently-playing # user-read-currently-playing
# user-read-recently-played # user-read-recently-played
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")
SPOTIFY_URL_REFRESH_TOKEN = "https://accounts.spotify.com/api/token" REFRESH_TOKEN_URL = "https://accounts.spotify.com/api/token"
SPOTIFY_URL_NOW_PLAYING = "https://api.spotify.com/v1/me/player/currently-playing" NOW_PLAYING_URL = "https://api.spotify.com/v1/me/player/currently-playing"
SPOTIFY_URL_RECENTLY_PLAY = "https://api.spotify.com/v1/me/player/recently-played?limit=10" RECENTLY_PLAYING_URL = (
"https://api.spotify.com/v1/me/player/recently-played?limit=10"
)
app = Flask(__name__) app = Flask(__name__)
def getAuth(): 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(): def refreshToken():
data = { data = {
@@ -33,53 +39,57 @@ def refreshToken():
headers = {"Authorization": "Basic {}".format(getAuth())} 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"] return response.json()["access_token"]
def recentlyPlayed(): def recentlyPlayed():
token = refreshToken() token = refreshToken()
headers = {"Authorization": f"Bearer {token}"} 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: if response.status_code == 204:
return {} return {}
return response.json() return response.json()
def nowPlaying(): def nowPlaying():
token = refreshToken() token = refreshToken()
headers = {"Authorization": f"Bearer {token}"} 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: if response.status_code == 204:
return {} return {}
return response.json() return response.json()
def barGen(barCount): def barGen(barCount):
barCSS = "" barCSS = ""
left = 1 left = 1
for i in range(1, barCount + 1): for i in range(1, barCount + 1):
anim = random.randint(1000, 1350) anim = random.randint(1000, 1350)
barCSS += ".bar:nth-child({}) {{ left: {}px; animation-duration: {}ms; }}".format( barCSS += (
i, left, anim ".bar:nth-child({}) {{ left: {}px; animation-duration: {}ms; }}".format(
i, left, anim
)
) )
left += 4 left += 4
return barCSS return barCSS
def loadImageB64(url): def loadImageB64(url):
resposne = requests.get(url) resposne = requests.get(url)
return b64encode(resposne.content).decode("ascii") return b64encode(resposne.content).decode("ascii")
def makeSVG(data): def makeSVG(data):
barCount = 84 barCount = 84
contentBar = "".join(["<div class='bar'></div>" for i in range(barCount)]) contentBar = "".join(["<div class='bar'></div>" for i in range(barCount)])
barCSS = barGen(barCount) barCSS = barGen(barCount)
if data == {} or data["item"] == 'None': if data == {} or data["item"] == "None":
#contentBar = "" #Shows/Hides the EQ bar if no song is currently playing # contentBar = "" #Shows/Hides the EQ bar if no song is currently playing
currentStatus = "Last seen playing:" currentStatus = "Was playing:"
recentPlays = recentlyPlayed() recentPlays = recentlyPlayed()
recentPlaysLength = len(recentPlays["items"]) recentPlaysLength = len(recentPlays["items"])
itemIndex = random.randint(0, recentPlaysLength - 1) itemIndex = random.randint(0, recentPlaysLength - 1)
@@ -87,7 +97,6 @@ def makeSVG(data):
else: else:
item = data["item"] item = data["item"]
currentStatus = "Vibing to:" currentStatus = "Vibing to:"
image = loadImageB64(item["album"]["images"][1]["url"]) image = loadImageB64(item["album"]["images"][1]["url"])
artistName = item["artists"][0]["name"].replace("&", "&amp;") artistName = item["artists"][0]["name"].replace("&", "&amp;")
songName = item["name"].replace("&", "&amp;") songName = item["name"].replace("&", "&amp;")
@@ -98,11 +107,12 @@ def makeSVG(data):
"artistName": artistName, "artistName": artistName,
"songName": songName, "songName": songName,
"image": image, "image": image,
"status": currentStatus "status": currentStatus,
} }
return render_template("spotify.html.j2", **dataDict) return render_template("spotify.html.j2", **dataDict)
@app.route("/", defaults={"path": ""}) @app.route("/", defaults={"path": ""})
@app.route("/<path:path>") @app.route("/<path:path>")
def catch_all(path): def catch_all(path):
@@ -114,5 +124,6 @@ def catch_all(path):
return resp return resp
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True) app.run(debug=True)

View File

@@ -2,19 +2,15 @@
<foreignObject width="480" height="133"> <foreignObject width="480" height="133">
<div xmlns="http://www.w3.org/1999/xhtml" class="container"> <div xmlns="http://www.w3.org/1999/xhtml" class="container">
<style> <style>
div {
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji;
}
.main { .main {
/*margin-top: 40px;*/ /*margin-top: 40px;*/
display: flex; display: flex;
} }
.currentStatus { .currentStatus {
position: static;
float: left; float: left;
font-size: 24px; font-size: 24px;
position: static;
margin-top: -5px; margin-top: -5px;
margin-left: 10px; margin-left: 10px;
} }
@@ -25,8 +21,8 @@
} }
.art { .art {
float: left;
width: 27%; width: 27%;
float: left;
margin-left: -5px; margin-left: -5px;
} }
@@ -35,55 +31,55 @@
} }
.song { .song {
font-size: 24px;
color: #666; color: #666;
text-align: center;
margin-top: 3px;
white-space:nowrap;
overflow:hidden; overflow:hidden;
margin-top: 3px;
font-size: 24px;
text-align: center;
white-space:nowrap;
text-overflow:ellipsis; text-overflow:ellipsis;
} }
.artist { .artist {
font-size: 20px;
color: #b3b3b3; color: #b3b3b3;
font-size: 20px;
margin-top: 4px;
text-align: center; text-align: center;
margin-bottom: 5px; margin-bottom: 5px;
margin-top: 4px;
} }
.cover { .cover {
border-radius: 5px;
height: 100px;
width: 100px; width: 100px;
height: 100px;
border-radius: 5px;
} }
#bars { #bars {
width: 40px;
height: 30px; height: 30px;
bottom: 23px; bottom: 23px;
margin: -20px 0 0 0px;
position: absolute; position: absolute;
width: 40px; margin: -20px 0 0 0px;
} }
.bar { .bar {
background: #1DB954cc; width: 3px;
bottom: 1px; bottom: 1px;
height: 3px; height: 3px;
position: absolute; position: absolute;
width: 3px; background: #1DB954cc;
animation: sound 0ms -800ms linear infinite alternate; animation: sound 0ms -800ms linear infinite alternate;
} }
@keyframes sound { @keyframes sound {
0% { 0% {
opacity: .35;
height: 3px; height: 3px;
opacity: .35;
} }
100% { 100% {
opacity: 1;
height: 15px; height: 15px;
opacity: 0.95;
} }
} }
@@ -93,7 +89,7 @@
<!-- <div class="currentStatus">{{status}}</div> --> <!-- <div class="currentStatus">{{status}}</div> -->
<div class="main"> <div class="main">
<a class="art" href="{}" target="_BLANK"> <a class="art" href="{}" target="_blank">
<center> <center>
<img src="data:image/png;base64, {{image}}" class="cover" /> <img src="data:image/png;base64, {{image}}" class="cover" />
</center> </center>

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB