Skip to content

Commit

Permalink
Recently playing mod
Browse files Browse the repository at this point in the history
  • Loading branch information
novatorem committed Aug 23, 2020
1 parent ad63ff5 commit d653529
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 47 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
<td width="50%">

Software Developer currently working on cool projects at [Cast](https://blacktrax.cast-soft.com/showcase/). Everything here is available under the [Unlicense](https://choosealicense.com/licenses/unlicense/)!
<br><p align="center">
[![Website](https://img.shields.io/badge/website-dev-2a8?style=flat-square&logo=safari&logoColor=white)](https://novac.dev)
[![E-Mail](https://img.shields.io/badge/email-reveal-369?style=flat-square&logo=gmail&logoColor=white)](https://mailhide.io/e/5ck1H)<br>
[![Spotify](https://img.shields.io/badge/spotify-omni-1DB954?style=flat-square&logo=spotify&logoColor=white)](https://open.spotify.com/user/omnitenebris)
[![Visits](https://badges.pufler.dev/visits/novatorem/novatorem?logo=GitHub&label=github%20visits&color=blue&logoColor=white&style=flat-square)](https://github.com/novatorem)
</p>
<br><p align="center">
[![Website](https://img.shields.io/badge/website-dev-2a8?style=flat-square&logo=safari&logoColor=white)](https://novac.dev)
[![E-Mail](https://img.shields.io/badge/email-reveal-369?style=flat-square&logo=gmail&logoColor=white)](https://mailhide.io/e/5ck1H)<br>
[![Spotify](https://img.shields.io/badge/spotify-omni-1DB954?style=flat-square&logo=spotify&logoColor=white)](https://open.spotify.com/user/omnitenebris)
[![Visits](https://badges.pufler.dev/visits/novatorem/novatorem?logo=GitHub&label=github%20visits&color=blue&logoColor=white&style=flat-square)](https://github.com/novatorem)
</p>
</td>
</table>

[//]: <> (The `&nbsp;` is to have Aphelion take up more space)
</table>
47 changes: 21 additions & 26 deletions api/spotify-playing.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
from flask import Flask, Response, jsonify, render_template
from base64 import b64encode
import os
import json
import random
import requests

from base64 import b64encode
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
from flask import Flask, Response, jsonify, render_template

import requests
import json
import os
import random
load_dotenv(find_dotenv())

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
# Scopes:
# 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"


app = Flask(__name__)


def getAuth():
return b64encode(f"{SPOTIFY_CLIENT_ID}:{SPOTIFY_SECRET_ID}".encode()).decode("ascii")


def refreshToken():
data = {
"grant_type": "refresh_token",
Expand All @@ -48,11 +47,8 @@ def recentlyPlayed():
return response.json()

def nowPlaying():

token = refreshToken()

headers = {"Authorization": f"Bearer {token}"}

response = requests.get(SPOTIFY_URL_NOW_PLAYING, headers=headers)

if response.status_code == 204:
Expand Down Expand Up @@ -82,32 +78,31 @@ def makeSVG(data):
barCSS = barGen(barCount)

if data == {}:
content_bar = ""
recent_plays = recentlyPlayed()
size_recent_play = len(recent_plays["items"])
idx = random.randint(0, size_recent_play - 1)
item = recent_plays["items"][idx]["track"]
#contentBar = ""
recentlyPlayed = recentlyPlayed()
recentLength = len(recentlyPlayed["items"])
itemIndex = random.randint(0, recentLength - 1)
item = recentlyPlayed["items"][itemIndex]["track"]
else:
item = data["item"]

img = loadImageB64(item["album"]["images"][1]["url"])
image = loadImageB64(item["album"]["images"][1]["url"])
artistName = item["artists"][0]["name"].replace("&", "&amp;")
songName = item["name"].replace("&", "&amp;")

dataDict = {
"content_bar": contentBar,
"css_bar": barCSS,
"artist_name": artistName,
"song_name": songName,
"img": img,
"contentBar": contentBar,
"barCSS": barCSS,
"artistName": artistName,
"songName": songName,
"image": image
}

return render_template("spotify.html.j2", **dataDict)

@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def catch_all(path):

data = nowPlaying()
svg = makeSVG(data)

Expand Down
8 changes: 4 additions & 4 deletions api/templates/preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@
}

.cover {
position: absolute;
border-radius: 5px;
height: 100px;
width: 100px;
z-index: -1;
}

#bars {
Expand Down Expand Up @@ -99,16 +101,14 @@
<div class="main">
<a class="art" href="{}" target="_BLANK">
<center>
<img src="https://source.unsplash.com/random/500x500" class="cover" />
<img src="https://source.unsplash.com/random/500x500" class="cover"/>
</center>
</a>

<div class="text">
<div class="song">Song</div>
<div class="artist">Arist</div>
<div id="bars">
{{content_bar|safe}}
</div>
<div id="bars">Bars</div>
</div>
</div>

Expand Down
17 changes: 9 additions & 8 deletions api/templates/spotify.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@
color: #666;
text-align: center;
margin-top: 3px;
white-space:nowrap;
overflow:hidden;
text-overflow:ellipsis;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.artist {
font-size: 20px;
color: #b3b3b3;
text-align: center;
margin-bottom: 5px;
margin-top: 5px;
}
.logo {
Expand Down Expand Up @@ -97,19 +98,19 @@
{{css_bar|safe}}
</style>

<div class="main">
<a class="art" href="{}" target="_BLANK">
<center>
<img src="data:image/png;base64, {{img}}" class="cover" />
<img src="data:image/png;base64, {{image}}" class="cover" />
</center>
</a>

<div class="text">
<div class="song">{{song_name}}</div>
<div class="artist">{{artist_name}}</div>
<div class="song">{{songName}}</div>
<div class="artist">{{artistName}}</div>
<div id="bars">
{{content_bar|safe}}
{{contentBar|safe}}
</div>
</div>
</div>
Expand Down

0 comments on commit d653529

Please sign in to comment.