From 6c347ca179f90aedcdc137500a138dfbeea4aaaa Mon Sep 17 00:00:00 2001 From: Alfie King Date: Thu, 5 Sep 2024 00:18:39 +0100 Subject: [PATCH] main --- .gitignore | 1 + Dockerfile | 18 ++++++++++ LICENSE | 9 +++++ requirements.txt | 2 ++ src/main.py | 34 +++++++++++++++++++ src/static/css/spotify.css | 67 ++++++++++++++++++++++++++++++++++++++ src/static/js/spotify.js | 36 ++++++++++++++++++++ src/templates/index.html | 29 +++++++++++++++++ 8 files changed, 196 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 requirements.txt create mode 100644 src/main.py create mode 100644 src/static/css/spotify.css create mode 100644 src/static/js/spotify.js create mode 100644 src/templates/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b694934 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.venv \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..86dfb65 --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c3abcb0 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5eaf725 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask +requests \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..c9ca418 --- /dev/null +++ b/src/main.py @@ -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) \ No newline at end of file diff --git a/src/static/css/spotify.css b/src/static/css/spotify.css new file mode 100644 index 0000000..2e4130e --- /dev/null +++ b/src/static/css/spotify.css @@ -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; +} \ No newline at end of file diff --git a/src/static/js/spotify.js b/src/static/js/spotify.js new file mode 100644 index 0000000..eea5fb5 --- /dev/null +++ b/src/static/js/spotify.js @@ -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(); \ No newline at end of file diff --git a/src/templates/index.html b/src/templates/index.html new file mode 100644 index 0000000..b16dab0 --- /dev/null +++ b/src/templates/index.html @@ -0,0 +1,29 @@ + + + + + + {{title}} + + + + + + + + + + + + +
+

+

+
+
+
+
+
+ + + \ No newline at end of file