diff --git a/app/api/lyrics.py b/app/api/lyrics.py index 2a00be9..7e86ed4 100644 --- a/app/api/lyrics.py +++ b/app/api/lyrics.py @@ -33,7 +33,7 @@ def send_lyrics(): lyrics, is_synced, copyright = get_lyrics_from_tags(filepath) if not lyrics: - return {"error": "No lyrics found"}, 404 + return {"error": "No lyrics found"} return {"lyrics": lyrics, "synced": is_synced, "copyright": copyright}, 200 diff --git a/app/api/plugins/lyrics.py b/app/api/plugins/lyrics.py index e1a2df6..c53b7ba 100644 --- a/app/api/plugins/lyrics.py +++ b/app/api/plugins/lyrics.py @@ -1,4 +1,5 @@ from flask import Blueprint, request + from app.plugins.lyrics import Lyrics from app.utils.hashing import create_hash @@ -19,7 +20,7 @@ def search_lyrics(): data = finder.search_lyrics_by_title_and_artist(title, artist) if not data: - return {"downloaded": False, "all": []}, 404 + return {"downloaded": False} perfect_match = data[0] @@ -31,9 +32,11 @@ def search_lyrics(): i_album ) == create_hash(album): perfect_match = track - break + + else: + track["saved"] = False track_id = perfect_match["track_id"] - downloaded = finder.download_lyrics_to_path_by_id(track_id, filepath) + downloaded = finder.download_lyrics(track_id, filepath) - return {"downloaded": downloaded, "all": data}, 200 + return {"downloaded": downloaded}, 200 diff --git a/app/configs.py b/app/configs.py index f085562..a76bdeb 100644 --- a/app/configs.py +++ b/app/configs.py @@ -1,4 +1,4 @@ -LASTFM_API_KEY = "" -POSTHOG_API_KEY = "" +LASTFM_API_KEY = '' +POSTHOG_API_KEY = '' PLUGIN_LYRICS_AUTHORITY = "" -PLUGIN_LYRICS_ROOT_URL = "" +PLUGIN_LYRICS_ROOT_URL = "" \ No newline at end of file diff --git a/app/plugins/lyrics.py b/app/plugins/lyrics.py index aaafa35..b055cda 100644 --- a/app/plugins/lyrics.py +++ b/app/plugins/lyrics.py @@ -78,7 +78,10 @@ class LyricsProvider(LRCProvider): except requests.exceptions.ConnectionError: return None - return response + if response is not None and response.ok: + return response + + return None def _get_token(self): # Check if token is cached and not expired @@ -99,12 +102,17 @@ class LyricsProvider(LRCProvider): return # Token not cached or expired, fetch a new token - d = self._get("token.get", [("user_language", "en")]).json() - if d["message"]["header"]["status_code"] == 401: + res = self._get("token.get", [("user_language", "en")]) + + if res is None: + return + + res = res.json() + if res["message"]["header"]["status_code"] == 401: time.sleep(10) return self._get_token() - new_token = d["message"]["body"]["user_token"] + new_token = res["message"]["body"]["user_token"] expiration_time = current_time + 600 # 10 minutes expiration # Cache the new token @@ -120,19 +128,19 @@ class LyricsProvider(LRCProvider): "track.subtitle.get", [("track_id", track_id), ("subtitle_format", "lrc")] ) - if not res.ok: + try: + res = res.json() + body = res["message"]["body"] + except AttributeError: return None - res = res.json() - body = res["message"]["body"] - if not body: return None return body["subtitle"]["subtitle_body"] def get_lrc(self, title: str, artist: str) -> Optional[str]: - r = self._get( + res = self._get( "track.search", [ ("q_track_artist", f"{title} {artist}"), @@ -146,7 +154,11 @@ class LyricsProvider(LRCProvider): ], ) - body = r.json()["message"]["body"] + try: + body = res.json()["message"]["body"] + except AttributeError: + return [] + tracks = body["track_list"] return [ @@ -183,14 +195,17 @@ class Lyrics(Plugin): return self.provider.get_lrc(title, artist) @plugin_method - def download_lyrics_to_path_by_id(self, trackid: str, path: str): + def download_lyrics(self, trackid: str, path: str): lrc = self.provider.get_lrc_by_id(trackid) + is_valid = lrc is not None and lrc.replace("\n", "").strip() != "" - path = Path(path).with_suffix(".lrc") - - if not lrc or lrc.replace("\n", "").strip() == "": + if not is_valid: return False - with open(path, "w", encoding="utf-8") as f: - f.write(lrc) - return True + if is_valid: + path = Path(path).with_suffix(".lrc") + with open(path, "w", encoding="utf-8") as f: + f.write(lrc) + return True + + return False