Fixed code

This commit is contained in:
Halhadus 2025-04-22 01:03:03 +03:00
parent 50dfc82d0e
commit 1cb7409a28

View file

@ -5,6 +5,7 @@ import os
import json
import requests
import re
from collections import defaultdict
app = Flask(__name__)
@ -39,6 +40,7 @@ def get_scrobbles():
music_data = {extract_filename(row[2]): (row[0], row[1]) for row in music_cursor.fetchall()}
music_conn.close()
total_durations = defaultdict(int)
enriched_data = []
for ts, duration, extra_json in scrobbles:
try:
@ -47,6 +49,7 @@ def get_scrobbles():
filename = extract_filename(filepath)
if filename in music_data:
title, videoid = music_data[filename]
total_durations[(title, videoid)] += duration
enriched_data.append({
'title': title,
'videoid': videoid,
@ -55,11 +58,22 @@ def get_scrobbles():
})
except json.JSONDecodeError:
continue
return enriched_data
unique_tracks = {}
for track in enriched_data:
key = (track['title'], track['videoid'])
if key not in unique_tracks:
unique_tracks[key] = {
'title': track['title'],
'videoid': track['videoid'],
'total_duration': total_durations[key],
'last_played': track['timestamp']
}
return sorted(unique_tracks.values(), key=lambda x: x['last_played'], reverse=True)
@app.route('/swingmusicwrapped.html')
def swing_history():
scrobbles = get_scrobbles()
tracks = get_scrobbles()
return render_template_string('''
<!DOCTYPE html>
<html>
@ -93,6 +107,24 @@ def swing_history():
max-width: 1200px;
margin: 0 auto;
}
.header-buttons {
display: flex;
gap: 1rem;
margin-bottom: 2rem;
}
.play-button {
background: var(--card-bg);
border: 1px solid var(--border-color);
color: var(--text-color);
padding: 0.8rem 1.5rem;
border-radius: 5px;
text-decoration: none;
display: inline-flex;
align-items: center;
}
.play-button:hover {
background: #444;
}
.history-table {
width: 100%;
border-collapse: collapse;
@ -126,37 +158,27 @@ def swing_history():
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>
<div class="header-buttons">
<a href="/index.html" class="play-button"> Main Page</a>
<a href="https://git.halhadus.rocks/Halhadus/my-swingmusic-wrapped" class="play-button">📁 Source Code</a>
</div>
<table class="history-table">
<thead>
<tr>
<th>Cover</th>
<th>Song</th>
<th>Duration(play time)</th>
<th>Total Play Time</th>
<th>Last Play Date</th>
<th>Play</th>
</tr>
</thead>
<tbody>
{% for track in scrobbles %}
{% for track in tracks %}
<tr>
<td>
{% if is_youtube(track.videoid) %}
@ -168,8 +190,8 @@ def swing_history():
{% 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>{{ (track.total_duration // 3600)|int }}h {{ ((track.total_duration % 3600) // 60)|int }}m</td>
<td>{{ datetime.fromtimestamp(track.last_played).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 %}
@ -178,7 +200,7 @@ def swing_history():
</div>
</body>
</html>
''', scrobbles=scrobbles, datetime=datetime, is_youtube=is_youtube_id)
''', tracks=tracks, datetime=datetime, is_youtube=is_youtube_id)
@app.route('/<path:path>')
def catch_all(path):