From b662162ca76874131e2ada469534eb69a834e4f6 Mon Sep 17 00:00:00 2001 From: vithyze <127023076+vithyze@users.noreply.github.com> Date: Sun, 5 Mar 2023 12:06:51 +0000 Subject: [PATCH] Apply changes --- config.sample | 3 +++ supysonic/api/media.py | 56 ++++++++++++++++++++++-------------------- supysonic/config.py | 1 + 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/config.sample b/config.sample index 897cdd7..e509598 100644 --- a/config.sample +++ b/config.sample @@ -40,6 +40,9 @@ log_level = WARNING ; Default: El La Le Las Les Los The index_ignored_prefixes = El La Le Las Les Los The +; Enable the ChartLyrics API. Default: off +online_lyrics = off + [daemon] ; Socket file the daemon will listen on for incoming management commands ; Default: /tmp/supysonic/supysonic.sock diff --git a/supysonic/api/media.py b/supysonic/api/media.py index a5d3f0f..fcce37e 100644 --- a/supysonic/api/media.py +++ b/supysonic/api/media.py @@ -443,37 +443,39 @@ def lyrics(): return lyrics_response_for_track(track, lyrics) - # Create a stable, unique, filesystem-compatible identifier for the artist+title - unique = hashlib.md5( - json.dumps([x.lower() for x in (artist, title)]).encode("utf-8") - ).hexdigest() - cache_key = f"lyrics-{unique}" - lyrics = {} - try: - lyrics = json.loads( - zlib.decompress(current_app.cache.get_value(cache_key)).decode("utf-8") - ) - except (CacheMiss, zlib.error, TypeError, ValueError): + + if current_app.config["WEBAPP"]["online_lyrics"]: + # Create a stable, unique, filesystem-compatible identifier for the artist+title + unique = hashlib.md5( + json.dumps([x.lower() for x in (artist, title)]).encode("utf-8") + ).hexdigest() + cache_key = f"lyrics-{unique}" + try: - r = requests.get( - "http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect", - params={"artist": artist, "song": title}, - timeout=5, + lyrics = json.loads( + zlib.decompress(current_app.cache.get_value(cache_key)).decode("utf-8") ) - root = ElementTree.fromstring(r.content) + except (CacheMiss, zlib.error, TypeError, ValueError): + try: + r = requests.get( + "http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect", + params={"artist": artist, "song": title}, + timeout=5, + ) + root = ElementTree.fromstring(r.content) - ns = {"cl": "http://api.chartlyrics.com/"} - lyrics = { - "artist": root.find("cl:LyricArtist", namespaces=ns).text, - "title": root.find("cl:LyricSong", namespaces=ns).text, - "value": root.find("cl:Lyric", namespaces=ns).text, - } + ns = {"cl": "http://api.chartlyrics.com/"} + lyrics = { + "artist": root.find("cl:LyricArtist", namespaces=ns).text, + "title": root.find("cl:LyricSong", namespaces=ns).text, + "value": root.find("cl:Lyric", namespaces=ns).text, + } - current_app.cache.set( - cache_key, zlib.compress(json.dumps(lyrics).encode("utf-8"), 9) - ) - except requests.exceptions.RequestException as e: # pragma: nocover - logger.warning("Error while requesting the ChartLyrics API: " + str(e)) + current_app.cache.set( + cache_key, zlib.compress(json.dumps(lyrics).encode("utf-8"), 9) + ) + except requests.exceptions.RequestException as e: # pragma: nocover + logger.warning("Error while requesting the ChartLyrics API: " + str(e)) return request.formatter("lyrics", lyrics) diff --git a/supysonic/config.py b/supysonic/config.py index 3d913b2..0f21683 100644 --- a/supysonic/config.py +++ b/supysonic/config.py @@ -37,6 +37,7 @@ class DefaultConfig: "mount_webui": True, "mount_api": True, "index_ignored_prefixes": "El La Le Las Les Los The", + "online_lyrics": False, } DAEMON = { "socket": r"\\.\pipe\supysonic"