Fixed code
This commit is contained in:
parent
50dfc82d0e
commit
1cb7409a28
1 changed files with 43 additions and 21 deletions
|
@ -5,6 +5,7 @@ import os
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
import re
|
import re
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
app = Flask(__name__)
|
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_data = {extract_filename(row[2]): (row[0], row[1]) for row in music_cursor.fetchall()}
|
||||||
music_conn.close()
|
music_conn.close()
|
||||||
|
|
||||||
|
total_durations = defaultdict(int)
|
||||||
enriched_data = []
|
enriched_data = []
|
||||||
for ts, duration, extra_json in scrobbles:
|
for ts, duration, extra_json in scrobbles:
|
||||||
try:
|
try:
|
||||||
|
@ -47,6 +49,7 @@ def get_scrobbles():
|
||||||
filename = extract_filename(filepath)
|
filename = extract_filename(filepath)
|
||||||
if filename in music_data:
|
if filename in music_data:
|
||||||
title, videoid = music_data[filename]
|
title, videoid = music_data[filename]
|
||||||
|
total_durations[(title, videoid)] += duration
|
||||||
enriched_data.append({
|
enriched_data.append({
|
||||||
'title': title,
|
'title': title,
|
||||||
'videoid': videoid,
|
'videoid': videoid,
|
||||||
|
@ -55,11 +58,22 @@ def get_scrobbles():
|
||||||
})
|
})
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
continue
|
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')
|
@app.route('/swingmusicwrapped.html')
|
||||||
def swing_history():
|
def swing_history():
|
||||||
scrobbles = get_scrobbles()
|
tracks = get_scrobbles()
|
||||||
return render_template_string('''
|
return render_template_string('''
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
|
@ -93,6 +107,24 @@ def swing_history():
|
||||||
max-width: 1200px;
|
max-width: 1200px;
|
||||||
margin: 0 auto;
|
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 {
|
.history-table {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
border-collapse: collapse;
|
border-collapse: collapse;
|
||||||
|
@ -126,37 +158,27 @@ def swing_history():
|
||||||
color: #666;
|
color: #666;
|
||||||
font-size: 0.8rem;
|
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>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1>🎵 Halhadus' SwingMusic Wrapped</h1>
|
<h1>🎵 Halhadus' SwingMusic Wrapped</h1>
|
||||||
|
<div class="header-buttons">
|
||||||
<a href="/index.html" class="play-button">← Main Page</a>
|
<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>
|
<a href="https://git.halhadus.rocks/Halhadus/my-swingmusic-wrapped" class="play-button">📁 Source Code</a>
|
||||||
|
</div>
|
||||||
<table class="history-table">
|
<table class="history-table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Cover</th>
|
<th>Cover</th>
|
||||||
<th>Song</th>
|
<th>Song</th>
|
||||||
<th>Duration(play time)</th>
|
<th>Total Play Time</th>
|
||||||
<th>Last Play Date</th>
|
<th>Last Play Date</th>
|
||||||
<th>Play</th>
|
<th>Play</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for track in scrobbles %}
|
{% for track in tracks %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
{% if is_youtube(track.videoid) %}
|
{% if is_youtube(track.videoid) %}
|
||||||
|
@ -168,8 +190,8 @@ def swing_history():
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>{{ track.title }}</td>
|
<td>{{ track.title }}</td>
|
||||||
<td>{{ (track.duration // 60)|int }}:{{ "%02d" % (track.duration % 60) }}</td>
|
<td>{{ (track.total_duration // 3600)|int }}h {{ ((track.total_duration % 3600) // 60)|int }}m</td>
|
||||||
<td>{{ datetime.fromtimestamp(track.timestamp).strftime('%Y-%m-%d %H:%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>
|
<td><a href="https://music.youtube.com/watch?v={{ track.videoid }}" class="play-button" target="_blank">▶ YT Music</a></td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
@ -178,7 +200,7 @@ def swing_history():
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
''', scrobbles=scrobbles, datetime=datetime, is_youtube=is_youtube_id)
|
''', tracks=tracks, datetime=datetime, is_youtube=is_youtube_id)
|
||||||
|
|
||||||
@app.route('/<path:path>')
|
@app.route('/<path:path>')
|
||||||
def catch_all(path):
|
def catch_all(path):
|
||||||
|
|
Loading…
Add table
Reference in a new issue