From 50dfc82d0e4695b1665d0d9617ef1e49c3984ac1 Mon Sep 17 00:00:00 2001 From: Halhadus Date: Tue, 22 Apr 2025 00:30:55 +0300 Subject: [PATCH] Init --- README.md | 2 - websitepart.py | 192 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+), 2 deletions(-) delete mode 100644 README.md create mode 100644 websitepart.py diff --git a/README.md b/README.md deleted file mode 100644 index 527c0b5..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# my-swingmusic-wrapped - diff --git a/websitepart.py b/websitepart.py new file mode 100644 index 0000000..27481cf --- /dev/null +++ b/websitepart.py @@ -0,0 +1,192 @@ +import sqlite3 +from flask import Flask, render_template_string, redirect +from datetime import datetime +import os +import json +import requests +import re + +app = Flask(__name__) + +def is_youtube_id(videoid): + return re.match(r'^[a-zA-Z0-9_-]{11}$', videoid) is not None + +def get_db_values(url="https://halhadus.rocks/assets/localwrapped/music.db"): + if not os.path.exists("/var/www/swingmusic/.swingmusic/music.db"): + try: + download = requests.get(url) + with open("/var/www/swingmusic/.swingmusic/music.db", "wb") as f: + f.write(download.content) + except: + print("Could not download the database.") + exit() + +def extract_filename(filepath): + return os.path.basename(filepath) + +def get_scrobbles(): + get_db_values() + + swing_conn = sqlite3.connect('/var/www/swingmusic/.swingmusic/swingmusic.db') + swing_cursor = swing_conn.cursor() + swing_cursor.execute('SELECT timestamp, duration, extra FROM scrobble ORDER BY timestamp DESC') + scrobbles = swing_cursor.fetchall() + swing_conn.close() + + music_conn = sqlite3.connect('/var/www/swingmusic/.swingmusic/music.db') + music_cursor = music_conn.cursor() + music_cursor.execute('SELECT listmusicname, listvideoid, filelocation FROM music') + music_data = {extract_filename(row[2]): (row[0], row[1]) for row in music_cursor.fetchall()} + music_conn.close() + + enriched_data = [] + for ts, duration, extra_json in scrobbles: + try: + extra = json.loads(extra_json.replace("'", '"')) + filepath = extra.get("filepath", "") + filename = extract_filename(filepath) + if filename in music_data: + title, videoid = music_data[filename] + enriched_data.append({ + 'title': title, + 'videoid': videoid, + 'duration': duration, + 'timestamp': ts + }) + except json.JSONDecodeError: + continue + return enriched_data + +@app.route('/swingmusicwrapped.html') +def swing_history(): + scrobbles = get_scrobbles() + return render_template_string(''' + + + + + + + Halhadus' SwingMusic Wrapped + + + + + + + + + +
+

🎵 Halhadus' SwingMusic Wrapped

+ ← Main Page + 📁 Source Codes + + + + + + + + + + + + {% for track in scrobbles %} + + + + + + + + {% endfor %} + +
CoverSongDuration(play time)Last Play DatePlay
+ {% if is_youtube(track.videoid) %} + {{ track.title }} cover + {% else %} +
+ N/A +
+ {% endif %} +
{{ track.title }}{{ (track.duration // 60)|int }}:{{ "%02d" % (track.duration % 60) }}{{ datetime.fromtimestamp(track.timestamp).strftime('%Y-%m-%d %H:%M') }}▶ YT Music
+
+ + + ''', scrobbles=scrobbles, datetime=datetime, is_youtube=is_youtube_id) + +@app.route('/') +def catch_all(path): + return redirect(f'https://halhadus.rocks/{path}') + +@app.route('/') +def index(): + return redirect('https://halhadus.rocks') + +if __name__ == '__main__': + app.run(host='127.0.0.1', port=os.environ.get("PORT"))