From a9d095d79f5655d0d7b3cceb0319cf61cc2a78b3 Mon Sep 17 00:00:00 2001 From: cwilvx Date: Sun, 2 Mar 2025 21:50:34 +0300 Subject: [PATCH] try: use bjoern wsgi --- app/api/album.py | 4 --- app/api/lyrics.py | 11 +++--- app/api/stream.py | 23 ++----------- app/lib/lyrics.py | 82 +++++++++++++++++++++++++++++---------------- app/models/track.py | 10 +++--- manage.py | 19 ++++++----- pyproject.toml | 1 + uv.lock | 8 +++++ 8 files changed, 85 insertions(+), 73 deletions(-) diff --git a/app/api/album.py b/app/api/album.py index bfe7a475..3d770e1f 100644 --- a/app/api/album.py +++ b/app/api/album.py @@ -32,12 +32,10 @@ api = APIBlueprint("album", __name__, url_prefix="/album", abp_tags=[bp_tag]) class GetAlbumVersionsBody(BaseModel): og_album_title: str = Field( description="The original album title (album.og_title)", - example=Defaults.API_ALBUMNAME, ) albumhash: str = Field( description="The album hash of the album to exclude from the results.", - example=Defaults.API_ALBUMHASH, ) @@ -48,8 +46,6 @@ class GetMoreFromArtistsBody(AlbumLimitSchema): base_title: str = Field( description="The base title of the album to exclude from the results.", - example=Defaults.API_ALBUMNAME, - default=None, ) diff --git a/app/api/lyrics.py b/app/api/lyrics.py index 8d1cd261..52bdb0b3 100644 --- a/app/api/lyrics.py +++ b/app/api/lyrics.py @@ -15,10 +15,7 @@ api = APIBlueprint("lyrics", __name__, url_prefix="/lyrics", abp_tags=[bp_tag]) class SendLyricsBody(TrackHashSchema): - filepath: str = Field( - description="The path to the file", - example="/path/to/file.mp3", - ) + filepath: str = Field(description="The path to the file") @api.post("") @@ -30,13 +27,13 @@ def send_lyrics(body: SendLyricsBody): trackhash = body.trackhash is_synced = True - lyrics, copyright = get_lyrics(filepath) + lyrics, copyright = get_lyrics(filepath, trackhash) if not lyrics: lyrics, copyright = get_lyrics_from_duplicates(trackhash, filepath) if not lyrics: - lyrics, is_synced, copyright = get_lyrics_from_tags(filepath) + lyrics, is_synced, copyright = get_lyrics_from_tags(trackhash) # type: ignore if not lyrics: return {"error": "No lyrics found"} @@ -57,6 +54,6 @@ def check_lyrics(body: SendLyricsBody): if exists: return {"exists": exists}, 200 - exists = get_lyrics_from_tags(filepath, just_check=True) + exists = get_lyrics_from_tags(trackhash, just_check=True) return {"exists": exists}, 200 diff --git a/app/api/stream.py b/app/api/stream.py index baa06454..c5e759bd 100644 --- a/app/api/stream.py +++ b/app/api/stream.py @@ -39,9 +39,7 @@ class TransCodeStore: class SendTrackFileQuery(BaseModel): - filepath: str = Field( - description="The filepath to play (if available)", default=None - ) + filepath: str = Field(description="The filepath to play (if available)") quality: str = Field( "original", description="The quality of the audio file. Options: original, 1411, 1024, 512, 320, 256, 128, 96", @@ -85,14 +83,6 @@ def send_track_file_legacy(path: TrackHashSchema, query: SendTrackFileQuery): if track is not None: audio_type = guess_mime_type(filepath) - # return send_file( - # filepath, - # mimetype=audio_type, - # conditional=True, - # # environ=request.environ, - # as_attachment=True, - # max_age=None, - # ) return send_from_directory( Path(filepath).parent, Path(filepath).name, @@ -100,7 +90,6 @@ def send_track_file_legacy(path: TrackHashSchema, query: SendTrackFileQuery): conditional=True, as_attachment=True, ) - # return "" return msg, 404 @@ -319,14 +308,8 @@ def get_start_range(range_header: str): class GetAudioSilenceBody(BaseModel): - ending_file: str = Field( - description="The ending file's path", - example="/home/cwilvx/Music/Made in Kenya/Sol generation/Bensoul - Salama.mp3", - ) - starting_file: str = Field( - description="The beginning file's path", - example="/home/cwilvx/Music/Tidal/Albums/Bensoul - Qwarantunes/Bensoul - Peddi.m4a", - ) + ending_file: str = Field(description="The ending file's path") + starting_file: str = Field(description="The beginning file's path") @api.post("/silence") diff --git a/app/lib/lyrics.py b/app/lib/lyrics.py index 98a2a6a5..17f3cd91 100644 --- a/app/lib/lyrics.py +++ b/app/lib/lyrics.py @@ -1,5 +1,5 @@ from pathlib import Path -from tinytag import TinyTag +from typing import Iterable from app.store.tracks import TrackStore @@ -28,7 +28,7 @@ def convert_to_milliseconds(time: str): return int(milliseconds) -def format_synced_lyrics(lines: list[str]): +def format_synced_lyrics(lines: Iterable[str]): """ Formats synced lyrics into a list of dicts """ @@ -51,7 +51,7 @@ def format_synced_lyrics(lines: list[str]): return lyrics -def get_lyrics_from_lrc(filepath: str): +def get_lyrics_from_lrc(filepath: str | Path): with open(filepath, mode="r") as file: lines = (f.removesuffix("\n") for f in file.readlines()) return format_synced_lyrics(lines) @@ -79,7 +79,7 @@ def check_lyrics_file_rel_to_track(filepath: str): return False -def get_lyrics(track_path: str): +def get_lyrics(track_path: str, trackhash: str): """ Gets the lyrics for a track """ @@ -87,9 +87,18 @@ def get_lyrics(track_path: str): if lyrics_path: lyrics = get_lyrics_from_lrc(lyrics_path) - copyright = get_extras(track_path, ["copyright"]) + copyright = "" - return lyrics, copyright[0] + entry = TrackStore.trackhashmap.get(trackhash, None) + + if entry: + for track in entry.tracks: + copyright = track.copyright + + if copyright: + break + + return lyrics, copyright else: return None, "" @@ -98,10 +107,14 @@ def get_lyrics_from_duplicates(trackhash: str, filepath: str): """ Finds the lyrics from other duplicate tracks """ + entry = TrackStore.trackhashmap.get(trackhash, None) - for track in TrackStore.tracks: + if entry is None: + return None, "" + + for track in entry.tracks: if track.trackhash == trackhash and track.filepath != filepath: - lyrics, copyright = get_lyrics(track.filepath) + lyrics, copyright = get_lyrics(track.filepath, trackhash) if lyrics: return lyrics, copyright @@ -110,12 +123,20 @@ def get_lyrics_from_duplicates(trackhash: str, filepath: str): def check_lyrics_file(filepath: str, trackhash: str): + """ + Checks if the lyrics file exists for a track + """ lyrics_exists = check_lyrics_file_rel_to_track(filepath) if lyrics_exists: return True - for track in TrackStore.tracks: + entry = TrackStore.trackhashmap.get(trackhash, None) + + if entry is None: + return False + + for track in entry.tracks: if track.trackhash == trackhash and track.filepath != filepath: lyrics_exists = check_lyrics_file_rel_to_track(track.filepath) @@ -139,32 +160,35 @@ def test_is_synced(lyrics: list[str]): return False -def get_extras(filepath: str, keys: list[str]): - """ - Get extra tags from an audio file. - """ - try: - tags = TinyTag.get(filepath) - except Exception: - return [""] * len(keys) - - extras = tags.extra - - return [extras.get(key, "").strip() for key in keys] - - -def get_lyrics_from_tags(filepath: str, just_check: bool = False): +def get_lyrics_from_tags(trackhash: str, just_check: bool = False): """ Gets the lyrics from the tags of the track """ - lyrics, copyright = get_extras(filepath, ["lyrics", "copyright"]) - lyrics = lyrics.replace("engdesc", "") - exists = bool(lyrics.replace("\n", "").strip()) + entry = TrackStore.trackhashmap.get(trackhash, None) + + if entry is None: + return None, False, "" + + lyrics: str | None = None + copyright: str | None = None + synced = False + + for track in entry.tracks: + if lyrics and copyright: + break + + if not lyrics: + lyrics = track.extra.get("lyrics", None) + + if not copyright: + copyright = track.copyright if just_check: - return exists + return lyrics is not None - if not exists: + if lyrics: + lyrics = lyrics.replace("engdesc", "") + else: return None, False, "" lines = lyrics.split("\n") diff --git a/app/models/track.py b/app/models/track.py index 13263efd..0c407e17 100644 --- a/app/models/track.py +++ b/app/models/track.py @@ -88,11 +88,11 @@ class Track: self.explicit = int(explicit_tag[0]) == 1 self.image = self.albumhash + ".webp" + "?pathhash=" + self.pathhash - self.extra = { - "disc_total": self.extra.get("disc_total", 0), - "track_total": self.extra.get("track_total", 0), - "samplerate": self.extra.get("samplerate", -1), - } + # self.extra = { + # "disc_total": self.extra.get("disc_total", 0), + # "track_total": self.extra.get("track_total", 0), + # "samplerate": self.extra.get("samplerate", -1), + # } self.split_artists() self.map_with_config() diff --git a/manage.py b/manage.py index abfe6a74..4d0ca7ca 100644 --- a/manage.py +++ b/manage.py @@ -6,6 +6,7 @@ import logging import os import psutil import waitress +import bjoern import mimetypes import setproctitle @@ -237,11 +238,13 @@ if __name__ == "__main__": host = FLASKVARS.get_flask_host() port = FLASKVARS.get_flask_port() - waitress.serve( - app, - host=host, - port=port, - threads=100, - ipv6=True, - ipv4=True, - ) + # waitress.serve( + # app, + # host=host, + # port=port, + # threads=100, + # ipv6=True, + # ipv4=True, + # ) + # app.run(host=host, port=port, debug=False) + bjoern.run(app, host, port) diff --git a/pyproject.toml b/pyproject.toml index add46605..dea79281 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,7 @@ dependencies = [ "pillow>=11.1.0", "flask-openapi3==3.0.2", "rapidfuzz==3.11.0", + "bjoern>=3.2.2", ] [dependency-groups] diff --git a/uv.lock b/uv.lock index e6b4bd60..bfdc07f2 100644 --- a/uv.lock +++ b/uv.lock @@ -29,6 +29,12 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/78/b6/6307fbef88d9b5ee7421e68d78a9f162e0da4900bc5f5793f6d3d0e34fb8/annotated_types-0.7.0-py3-none-any.whl", hash = "sha256:1f02e8b43a8fbbc3f3e0d4f0f4bfc8131bcb4eebe8849b8e5c773f3a1c582a53", size = 13643 }, ] +[[package]] +name = "bjoern" +version = "3.2.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d5/a0/fba55eb58a502dabc0915137ff44eaacaba58a60196fd94dc8cd4e9fe67d/bjoern-3.2.2.tar.gz", hash = "sha256:16e5a02a9a17a7f5f8bea0d7c58650e78ab80ead6fe3e390037573d4355baf31", size = 45716 } + [[package]] name = "blinker" version = "1.9.0" @@ -1206,6 +1212,7 @@ name = "swingmusic" version = "2.0.0" source = { virtual = "." } dependencies = [ + { name = "bjoern" }, { name = "colorgram-py" }, { name = "ffmpeg-python" }, { name = "flask" }, @@ -1241,6 +1248,7 @@ dev = [ [package.metadata] requires-dist = [ + { name = "bjoern", specifier = ">=3.2.2" }, { name = "colorgram-py", specifier = ">=1.2.0" }, { name = "ffmpeg-python", specifier = ">=0.2.0" }, { name = "flask", specifier = ">=3.1.0" },