Added support for other providers

This commit is contained in:
Halhadus 2025-02-15 21:22:45 +03:00
parent 1f7eeb53a7
commit f44f06dd1d

View file

@ -1,8 +1,12 @@
import os import os
import sqlite3 import sqlite3
import datetime import datetime
# For now
import flask import flask
import re
def is_youtube_id(video_id):
"""Check if the video_id is a valid YouTube ID"""
return re.match(r'^[a-zA-Z0-9_-]{11}$', str(video_id)) is not None
def read_music_database(): def read_music_database():
conn = sqlite3.connect("assets/localwrapped/music.db") conn = sqlite3.connect("assets/localwrapped/music.db")
@ -61,7 +65,6 @@ def generatehtmlcode(year: int = datetime.datetime.now().year):
<meta property="og:title" content="Halhadus' Local Wrapped"> <meta property="og:title" content="Halhadus' Local Wrapped">
<meta property="og:description" content="Halhadus' Local Wrapped"> <meta property="og:description" content="Halhadus' Local Wrapped">
<meta property="og:url" content="https://halhadus.rocks/localwrapped.html"> <meta property="og:url" content="https://halhadus.rocks/localwrapped.html">
<link rel="icon" type="image/png" href="assets/favicon.png">
<style> <style>
:root { :root {
--bg-color: #1f1f1f; --bg-color: #1f1f1f;
@ -177,13 +180,20 @@ def generatehtmlcode(year: int = datetime.datetime.now().year):
<h2>🎶 Most Played Tracks</h2> <h2>🎶 Most Played Tracks</h2>
{% for music in top_tracks %} {% for music in top_tracks %}
<div class="music-card"> <div class="music-card">
{% if is_youtube_id(music.video_id) %}
<img src="https://img.youtube.com/vi/{{ music.video_id }}/hqdefault.jpg" <img src="https://img.youtube.com/vi/{{ music.video_id }}/hqdefault.jpg"
class="thumbnail" class="thumbnail"
alt="{{ music.name }} cover"> alt="{{ music.name }} cover">
{% else %}
<div class="thumbnail" style="background: #333; display: flex; align-items: center; justify-content: center;">
<span style="color: #666;">No thumbnail available</span>
</div>
{% endif %}
<div> <div>
<h3>{{ music.name }}</h3> <h3>{{ music.name }}</h3>
<p> Played {{ music.count }} times</p> <p> Played {{ music.count }} times</p>
<div> <div>
{% if is_youtube_id(music.video_id) %}
<a href="https://www.youtube.com/watch?v={{ music.video_id }}" <a href="https://www.youtube.com/watch?v={{ music.video_id }}"
class="play-button" class="play-button"
target="_blank"> target="_blank">
@ -194,6 +204,13 @@ def generatehtmlcode(year: int = datetime.datetime.now().year):
target="_blank"> target="_blank">
YT Music YT Music
</a> </a>
{% else %}
<a href="{{ music.video_id if music.video_id.startswith('http') else '//' + music.video_id }}"
class="play-button"
target="_blank">
Play Source
</a>
{% endif %}
</div> </div>
</div> </div>
</div> </div>
@ -202,7 +219,7 @@ def generatehtmlcode(year: int = datetime.datetime.now().year):
</body> </body>
</html>''' </html>'''
# Verileri hazırla # Prepare data
music_data = {row[0]: row for row in read_music_database()} music_data = {row[0]: row for row in read_music_database()}
count_data = read_count_database(selected_year) count_data = read_count_database(selected_year)
@ -223,4 +240,12 @@ def generatehtmlcode(year: int = datetime.datetime.now().year):
db_path = f'assets/localwrapped/count-{selected_year}.db' if selected_year != current_year else 'assets/localwrapped/count.db' db_path = f'assets/localwrapped/count-{selected_year}.db' if selected_year != current_year else 'assets/localwrapped/count.db'
last_updated = datetime.datetime.utcfromtimestamp(os.path.getmtime(db_path)).strftime('%Y-%m-%d %H:%M UTC') last_updated = datetime.datetime.utcfromtimestamp(os.path.getmtime(db_path)).strftime('%Y-%m-%d %H:%M UTC')
return flask.render_template_string(html, top_tracks=top_tracks, total_time=total_time, last_updated=last_updated, available_years=available_years, selected_year=selected_year) return flask.render_template_string(
html,
top_tracks=top_tracks,
total_time=total_time,
last_updated=last_updated,
available_years=available_years,
selected_year=selected_year,
is_youtube_id=is_youtube_id
)