nyxsite/site/app.py

76 lines
2.5 KiB
Python
Raw Normal View History

2025-02-23 22:42:00 -06:00
#!/usr/bin/env python
from flask import Flask, render_template, make_response, send_from_directory
2025-02-24 00:02:33 -06:00
import requests
import os
2025-02-24 00:02:33 -06:00
apiRoot = "https://api.listenbrainz.org"
user = "everypizza"
2025-02-23 22:42:00 -06:00
buttons = [
("assets/88x31s/benjae.nekoweb.org.gif", "https://benjae.nekoweb.org"),
("assets/88x31s/freetards.xyz.gif", "https://freetards.xyz"),
("assets/88x31s/fsky.io.webp", "https://fsky.io"),
("assets/88x31s/purplebored.pl.gif", "https://purplebored.pl"),
("assets/88x31s/squarebowl.club.gif", "https://squarebowl.club"),
("assets/88x31s/synth.download.svg", "https://synth.download"),
("assets/88x31s/telepath.im.png", "https://telepath.im"),
("assets/88x31s/voxel.fsky.io.webp", "https://voxel.fsky.io"),
]
2025-02-24 17:55:06 -06:00
nyxbuttons = [
("assets/88x31s/nyx.everypizza.im.webp", "https://nyx.everypizza.im"),
]
2025-02-23 22:42:00 -06:00
app = Flask(__name__)
2025-02-23 23:10:49 -06:00
countFile = "visitorCount.txt"
def get_visitor_count():
try:
with open(countFile, "r") as f:
return int(f.read())
except FileNotFoundError:
return 0
def increment_visitor_count():
count = get_visitor_count()
count += 1
with open(countFile, "w") as f:
f.write(str(count))
return count
2025-02-23 22:42:00 -06:00
@app.route('/')
2025-02-23 22:55:49 -06:00
def index():
2025-02-23 23:10:49 -06:00
increment_visitor_count()
2025-02-24 00:02:33 -06:00
data = requests.get(apiRoot + "/1/user/" + user + "/playing-now")
if not data.json()['payload']['listens']:
nowPlaying = False
nowPlayingString = "nothing is playing right now."
else:
track = data.json()['payload']['listens'][0]['track_metadata']['track_name']
artist = data.json()['payload']['listens'][0]['track_metadata']['artist_name']
album = data.json()['payload']['listens'][0]['track_metadata']['release_name']
nowPlayingString = f"now playing for <b>{user}</b>: <b>{track}</b> by <b>{artist}</b> from <b>{album}</b>"
2025-02-24 17:55:06 -06:00
return render_template('index.j2', np=nowPlayingString, visitor=get_visitor_count(), buttons=buttons, nyxbuttons=nyxbuttons)
2025-02-23 22:55:49 -06:00
@app.route('/assets/index.css')
def indexStyle():
css = render_template('assets/index.css')
response = make_response(css)
response.mimetype = "text/css"
return response
2025-02-23 22:42:00 -06:00
@app.route('/static/assets/88x31s/<path:filename>')
def serve_dir(filename):
directory = 'templates/assets/88x31s'
return send_from_directory(directory, filename)
@app.route('/todo')
def todo():
return render_template('todo.html')
2025-02-23 22:42:00 -06:00
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8080)