This commit is contained in:
Alfie King 2024-09-05 00:18:39 +01:00
commit 6c347ca179
8 changed files with 196 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.venv

18
Dockerfile Normal file
View File

@ -0,0 +1,18 @@
# Set the base image
FROM python:alpine3.19
# Copy the files to the container
COPY src /app
COPY requirements.txt /app
# Set the working directory
WORKDIR /app
# Install dependencies
RUN pip install -r requirements.txt
# Expose the port
EXPOSE 3425
# Run the application
CMD ["python", "main.py"]

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2024 acetheking987
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
flask
requests

34
src/main.py Normal file
View File

@ -0,0 +1,34 @@
from flask import Flask, render_template, request, redirect
import requests
banned_userAgent_match = ['Discordbot']
app = Flask(__name__, template_folder='templates', static_folder='static')
@app.route('/')
def index():
data = requests.get('https://alfieking.dev/api/nowplaying').json()
if data == {}:
return render_template('index.html', title="Idle", data="Idle", image="https://placehold.co/600x400", progress="progress: 0%")
redirect_allowed = True
for i in banned_userAgent_match:
if i in request.user_agent.string:
redirect_allowed = False
break
if request.args.get('redirect') and redirect_allowed:
return redirect(data['item']['external_urls']['spotify'])
progress = data['progress_ms'] / data["item"]['duration_ms'] * 100
data = data['item']
return render_template(
'index.html',
title=data['name'],
data=f"{data['artists'][0]['name']} - {data['album']['name']}",
image=data['album']['images'][0]['url'],
progress=f"progress: {progress}%"
)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3425)

View File

@ -0,0 +1,67 @@
html {
--background-color: #121212;
--text-color: #fff;
--primary-color: #5cdd8b;
}
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background:none transparent;
}
#spotify {
height: 100%;
aspect-ratio: 1/1;
border-radius: 20px;
background-image: unset;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
text-decoration: none;
}
#spotify-grad {
height: 100%;
width: 100%;
background: linear-gradient(0deg, rgba(0, 0, 0, 0.76) 0%, rgba(0,0,0,0) 100%);
border-radius: 20px;
display: flex;
flex-direction: column;
justify-content: flex-end;
padding: 0.5rem;
box-sizing: border-box;
}
#spotify h1 {
font-size: 1.2rem;
font-weight: 500;
color: var(--text-color);
}
#spotify #spotify-grad h2 {
font-size: 0.7rem;
font-weight: 300;
color: var(--text-color);
}
#spotify div#spotify-progress {
width: 100%;
height: 7px;
background-color: var(--background-color);
border-radius: 5px;
margin-top: 0.2rem;
}
#spotify div#spotify-progress div {
width: 0%;
height: 100%;
background-color: var(--primary-color);
border-radius: 5px;
transition: all 3s linear;
}

36
src/static/js/spotify.js Normal file
View File

@ -0,0 +1,36 @@
var spotifyProgress = 0;
var spotifyProgressMax = 100;
function spotify() {
fetch('https://alfieking.dev/api/nowplaying').then(response => response.json()).then(data => {
if (data.error) {
document.getElementById("spotify").style.display = "none";
} else if (data.idle == true) {
document.getElementById("spotify").style.display = "none";
} else {
document.getElementById("spotify-title").innerHTML = data.item.name;
document.getElementById("title").innerHTML = data.item.name;
document.getElementById("spotify-info").innerHTML = data.item.artists[0].name + " - " + data.item.album.name;
document.getElementById("spotify").style.backgroundImage = "url('" + data.item.album.images[0].url + "')";
spotifyProgress = data.progress_ms
spotifyProgressMax = data.item.duration_ms
document.getElementById("spotify").href = data.item.external_urls.spotify;
document.getElementById("spotify").style.display = "flex";
}
}).catch(error => {
document.getElementById("spotify").style.display = "none";
});
setTimeout(spotify, 3000);
}
var dt = 1;
function spotifyBarProgress() {
start = new Date();
document.getElementById("spotify-progress-bar").style.width = spotifyProgress / spotifyProgressMax * 100 + "%";
spotifyProgress += dt;
setTimeout(spotifyBarProgress, 1000);
end = new Date();
dt = end - start;
}
spotify();
spotifyBarProgress();

29
src/templates/index.html Normal file
View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title id="title">{{title}}</title>
<link rel="stylesheet" href="static/css/spotify.css">
<meta name="theme-color" content="#5cdd8b">
<meta property="og:site_name" content="{{progress}}">
<meta property="og:url" content="https://alfieking.dev/spotify_embed">
<meta property="og:title" content="{{title}}">
<meta property="og:description" content="{{data}}">
<meta property="og:type" content="website">
<link rel="icon" href={{image}}>
<meta name="og:image" itemprop="image" content="{{image}}">
</head>
<body>
<a id="spotify">
<div id="spotify-grad">
<h1 id="spotify-title"></h1>
<h2 id="spotify-info"></h2>
<div id="spotify-progress">
<div id="spotify-progress-bar"></div>
</div>
</div>
</a>
</body>
<script src="static/js/spotify.js"></script>
</html>