try catch breaking parts of lyrics plugin

This commit is contained in:
mungai-njoroge 2023-11-07 01:41:06 +03:00
parent 836bbe4dc5
commit de5b2a53b1
4 changed files with 43 additions and 25 deletions

View File

@ -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

View File

@ -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

View File

@ -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 = ""

View File

@ -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