From 0ff466649affe9276d8de0760bcbe4273c564462 Mon Sep 17 00:00:00 2001 From: Halhadus Date: Sat, 15 Feb 2025 15:09:53 +0300 Subject: [PATCH] Added support for other providers --- websitepart.py | 418 +++++++++++++++++++++++++------------------------ 1 file changed, 217 insertions(+), 201 deletions(-) diff --git a/websitepart.py b/websitepart.py index 7dbb8d8..4f760de 100644 --- a/websitepart.py +++ b/websitepart.py @@ -7,23 +7,39 @@ import re app = flask.Flask(__name__) app.config['LYRICS_DIR'] = 'lyrics' - code = "" history = [] +def is_youtube_url(url): + yt_patterns = [ + r'youtube\.com/watch\?v=', + r'music\.youtube\.com/watch\?v=', + r'youtu\.be/' + ] + return any(re.search(pattern, url) for pattern in yt_patterns) + +def extract_yt_id(url): + patterns = [ + r'v=([a-zA-Z0-9_-]{11})', + r'youtu\.be/([a-zA-Z0-9_-]{11})', + r'/([a-zA-Z0-9_-]{11})$' + ] + for pattern in patterns: + match = re.search(pattern, url) + if match: + return match.group(1) + return None + @app.route('/musicstatus.html', methods=['GET', 'POST']) def musicstatus(): global code, history - - # POST Request Handling + if flask.request.method == 'POST': data = flask.request.get_json() - # Validate verification code if code != data.get('verificationcode'): return "Verification failed", 403 - - # Create new entry + entry = ( data['music'], data['time'], @@ -31,211 +47,214 @@ def musicstatus(): data['videoid'] ) - # Handle history reset if data.get('resetstatus', '').lower() == "true": history.clear() - + history.append(entry) return "Entry added successfully" - # GET Request Handling - elif flask.request.method == 'GET': - current_playing = history[-1] if history else None - lyrics_content = [] - - # Load lyrics if available - if current_playing: - lrc_path = os.path.join(app.config['LYRICS_DIR'], f"{current_playing[0]}.lrc") - if os.path.exists(lrc_path): - with open(lrc_path, 'r') as f: - lyrics = [line.split("]")[-1].strip() for line in f if "]" in line] - lyrics_content = [line for line in lyrics if line] - - return flask.render_template_string(''' - - - - - - - Halhadus' Music Status - - - - - - - + + +
+

🎵 Halhadus' Music Status

+ ← Main Page + Source Code → - .history-table { - width: 100%; - border-collapse: collapse; - margin-top: 2rem; - overflow-x: auto; - } - - .history-table th, - .history-table td { - border: 1px solid var(--border-color); - padding: 0.8rem; - min-width: 120px; - } - - .lyrics-container { - background: #333; - border-radius: 8px; - padding: 1rem; - margin-top: 1rem; - text-align: center; - } - - .lyrics-line { - margin: 0.5rem 0; - color: #ccc; - } - - @media (min-width: 768px) { - .music-card { - grid-template-columns: 200px 1fr; - gap: 2rem; - padding: 2rem; - } - .play-button { - padding: 0.8rem 1.5rem; - } - } - - - -
-

🎵 Halhadus' Music Status

- ← Main Page - Source Code → - - {% if current_playing %} -
- {{ current_playing[0] }} cover -
-

{{ current_playing[0] }}

-

🕒 Last Updated: {{ format_timestamp(current_playing[1]) }}

- - {% if lyrics_content %} -
-

Lyrics

- {% for line in lyrics_content %} -

{{ line }}

- {% endfor %} -
- {% endif %} -
-
+ {% if current_playing %} +
+ {% if is_youtube(current_playing[3]) %} + {{ current_playing[0] }} cover {% else %} -
-

No music currently playing

+
+ No thumbnail available
{% endif %} - -

Play History

- - - - - - - - - - - {% for entry in history|reverse %} - - - - - - + +
+

{{ current_playing[0] }}

+

🕒 Last Updated: {{ format_timestamp(current_playing[1]) }}

+
+ {% if is_youtube(current_playing[3]) %} + + ▶ YouTube + + + ▶ YT Music + + {% else %} + + ▶ Play Source + + {% endif %} +
+ {% if lyrics_content %} +
+

Lyrics

+ {% for line in lyrics_content %} +

{{ line }}

{% endfor %} -
-
CoverTitleTimePlay
- {{ entry[0] }} thumbnail - {{ entry[0] }}{{ format_timestamp(entry[1]) }} - - ▶ - -
+
+ {% endif %} +
- - - ''', current_playing=current_playing, - history=history, - format_timestamp=format_timestamp, - lyrics_content=lyrics_content) + {% else %} +
+

No music currently playing

+
+ {% endif %} -def format_timestamp(timestamp): - return datetime.datetime.fromtimestamp( - int(timestamp), - datetime.timezone.utc - ).strftime("%Y-%m-%d %H:%M UTC") +

Play History

+ + + + + + + + + + + {% for entry in history|reverse %} + + + + + + + {% endfor %} + +
CoverTitleTimePlay
+ {% if is_youtube(entry[3]) %} + {{ entry[0] }} thumbnail + {% else %} +
+ N/A +
+ {% endif %} +
{{ entry[0] }}{{ format_timestamp(entry[1]) }} + + ▶ + +
+
+ + + ''', current_playing=current_playing, + history=history, + format_timestamp=lambda ts: datetime.datetime.fromtimestamp(int(ts), datetime.timezone.utc).strftime("%Y-%m-%d %H:%M UTC"), + is_youtube=is_youtube_url, + extract_yt_id=extract_yt_id, + lyrics_content=lyrics_content) @app.route('/verifykey', methods=['POST']) def verifycert(): @@ -249,16 +268,13 @@ def verifycert(): @app.route('/') def catch_all(path): - if path == None: - return flask.redirect('https://halhadus.rocks') - if not path == 'musicstatus.html' or not path == 'verifykey': - return flask.redirect(f'https://halhadus.rocks/{path}') - + return flask.redirect(f'https://halhadus.rocks/{path}') + @app.route('/') def index(): return flask.redirect('https://halhadus.rocks') if __name__ == '__main__': if not os.path.exists(app.config['LYRICS_DIR']): - os.makedirs(app.config['LYRICS_DIR']) - app.run(host='0.0.0.0', port=int(os.environ.get('PORT'))) + os.makedirs(app.config['LYRICS_DIR'], exist_ok=True) + app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))