1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 08:56:17 +00:00

Apply changes

This commit is contained in:
vithyze 2023-03-05 12:06:51 +00:00
parent 8e2adf8fc8
commit b662162ca7
3 changed files with 33 additions and 27 deletions

View File

@ -40,6 +40,9 @@ log_level = WARNING
; Default: El La Le Las Les Los The ; Default: El La Le Las Les Los The
index_ignored_prefixes = 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] [daemon]
; Socket file the daemon will listen on for incoming management commands ; Socket file the daemon will listen on for incoming management commands
; Default: /tmp/supysonic/supysonic.sock ; Default: /tmp/supysonic/supysonic.sock

View File

@ -443,37 +443,39 @@ def lyrics():
return lyrics_response_for_track(track, 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 = {} lyrics = {}
try:
lyrics = json.loads( if current_app.config["WEBAPP"]["online_lyrics"]:
zlib.decompress(current_app.cache.get_value(cache_key)).decode("utf-8") # Create a stable, unique, filesystem-compatible identifier for the artist+title
) unique = hashlib.md5(
except (CacheMiss, zlib.error, TypeError, ValueError): json.dumps([x.lower() for x in (artist, title)]).encode("utf-8")
).hexdigest()
cache_key = f"lyrics-{unique}"
try: try:
r = requests.get( lyrics = json.loads(
"http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect", zlib.decompress(current_app.cache.get_value(cache_key)).decode("utf-8")
params={"artist": artist, "song": title},
timeout=5,
) )
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/"} ns = {"cl": "http://api.chartlyrics.com/"}
lyrics = { lyrics = {
"artist": root.find("cl:LyricArtist", namespaces=ns).text, "artist": root.find("cl:LyricArtist", namespaces=ns).text,
"title": root.find("cl:LyricSong", namespaces=ns).text, "title": root.find("cl:LyricSong", namespaces=ns).text,
"value": root.find("cl:Lyric", namespaces=ns).text, "value": root.find("cl:Lyric", namespaces=ns).text,
} }
current_app.cache.set( current_app.cache.set(
cache_key, zlib.compress(json.dumps(lyrics).encode("utf-8"), 9) cache_key, zlib.compress(json.dumps(lyrics).encode("utf-8"), 9)
) )
except requests.exceptions.RequestException as e: # pragma: nocover except requests.exceptions.RequestException as e: # pragma: nocover
logger.warning("Error while requesting the ChartLyrics API: " + str(e)) logger.warning("Error while requesting the ChartLyrics API: " + str(e))
return request.formatter("lyrics", lyrics) return request.formatter("lyrics", lyrics)

View File

@ -37,6 +37,7 @@ class DefaultConfig:
"mount_webui": True, "mount_webui": True,
"mount_api": True, "mount_api": True,
"index_ignored_prefixes": "El La Le Las Les Los The", "index_ignored_prefixes": "El La Le Las Les Los The",
"online_lyrics": False,
} }
DAEMON = { DAEMON = {
"socket": r"\\.\pipe\supysonic" "socket": r"\\.\pipe\supysonic"