Init
This commit is contained in:
parent
a8f132e150
commit
50dfc82d0e
2 changed files with 192 additions and 2 deletions
|
@ -1,2 +0,0 @@
|
|||
# my-swingmusic-wrapped
|
||||
|
192
websitepart.py
Normal file
192
websitepart.py
Normal file
|
@ -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('''
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;700&display=swap" rel="stylesheet">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Halhadus' SwingMusic Wrapped</title>
|
||||
<link rel="icon" type="image/png" href="assets/favicon.png">
|
||||
<meta name="description" content="Halhadus' SwingMusic Wrapped">
|
||||
<meta name="author" content="Halhadus">
|
||||
<meta property="og:title" content="Halhadus' SwingMusic Wrapped">
|
||||
<meta property="og:description" content="Halhadus' SwingMusic Wrapped">
|
||||
<meta property="og:url" content="https://halhadus.rocks/swingmusicwrapped.html">
|
||||
<style>
|
||||
:root {
|
||||
--bg-color: #1f1f1f;
|
||||
--card-bg: #2a2a2a;
|
||||
--border-color: #333;
|
||||
--text-color: #ffffff;
|
||||
--font-family: 'JetBrains Mono', monospace;
|
||||
}
|
||||
body {
|
||||
background: var(--bg-color);
|
||||
color: var(--text-color);
|
||||
font-family: var(--font-family);
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
}
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.history-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-top: 2rem;
|
||||
}
|
||||
.history-table th, .history-table td {
|
||||
border: 1px solid var(--border-color);
|
||||
padding: 12px;
|
||||
text-align: left;
|
||||
}
|
||||
.history-table th {
|
||||
background: var(--card-bg);
|
||||
}
|
||||
.thumbnail {
|
||||
width: 80px;
|
||||
border-radius: 4px;
|
||||
aspect-ratio: 16/9;
|
||||
object-fit: cover;
|
||||
border: 2px solid var(--border-color);
|
||||
}
|
||||
.no-thumbnail {
|
||||
width: 80px;
|
||||
height: 45px;
|
||||
background: #333;
|
||||
border-radius: 4px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
.no-thumbnail span {
|
||||
color: #666;
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
a.play-button {
|
||||
background: #333;
|
||||
border: 1px solid #444;
|
||||
color: var(--text-color);
|
||||
padding: 8px 16px;
|
||||
border-radius: 5px;
|
||||
text-decoration: none;
|
||||
display: inline-block;
|
||||
}
|
||||
a.play-button:hover {
|
||||
background: #444;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>🎵 Halhadus' SwingMusic Wrapped</h1>
|
||||
<a href="/index.html" class="play-button">← Main Page</a>
|
||||
<a href="https://git.halhadus.rocks/Halhadus/my-swingmusic-wrapped" style="color: #ffffff; text-decoration: none;">📁 Source Codes</a>
|
||||
<table class="history-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Cover</th>
|
||||
<th>Song</th>
|
||||
<th>Duration(play time)</th>
|
||||
<th>Last Play Date</th>
|
||||
<th>Play</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for track in scrobbles %}
|
||||
<tr>
|
||||
<td>
|
||||
{% if is_youtube(track.videoid) %}
|
||||
<img src="https://img.youtube.com/vi/{{ track.videoid }}/hqdefault.jpg" class="thumbnail" alt="{{ track.title }} cover">
|
||||
{% else %}
|
||||
<div class="no-thumbnail">
|
||||
<span>N/A</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>{{ track.title }}</td>
|
||||
<td>{{ (track.duration // 60)|int }}:{{ "%02d" % (track.duration % 60) }}</td>
|
||||
<td>{{ datetime.fromtimestamp(track.timestamp).strftime('%Y-%m-%d %H:%M') }}</td>
|
||||
<td><a href="https://music.youtube.com/watch?v={{ track.videoid }}" class="play-button" target="_blank">▶ YT Music</a></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
''', scrobbles=scrobbles, datetime=datetime, is_youtube=is_youtube_id)
|
||||
|
||||
@app.route('/<path:path>')
|
||||
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"))
|
Loading…
Add table
Reference in a new issue