From bc98705375fa446fe03b0343700eb8c6da9e2dd7 Mon Sep 17 00:00:00 2001 From: WorldObservationLog Date: Wed, 22 May 2024 00:01:58 +0800 Subject: [PATCH] fix: raise exception when unable to get lyrics --- src/api.py | 8 ++++++-- src/rip.py | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/api.py b/src/api.py index 47e1f1c..a427595 100644 --- a/src/api.py +++ b/src/api.py @@ -1,6 +1,7 @@ import asyncio import logging from ssl import SSLError +from typing import Optional import httpx import regex @@ -167,7 +168,7 @@ async def get_song_info(song_id: str, token: str, storefront: str, lang: str): @retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), wait=wait_random_exponential(multiplier=1, max=60), stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) -async def get_song_lyrics(song_id: str, storefront: str, token: str, dsid: str, account_token: str, lang: str) -> str: +async def get_song_lyrics(song_id: str, storefront: str, token: str, dsid: str, account_token: str, lang: str) -> Optional[str]: async with request_lock: req = await client.get(f"https://amp-api.music.apple.com/v1/catalog/{storefront}/songs/{song_id}/lyrics", params={"l": lang}, @@ -175,7 +176,10 @@ async def get_song_lyrics(song_id: str, storefront: str, token: str, dsid: str, "X-Dsid": dsid}, cookies={f"mz_at_ssl-{dsid}": account_token}) result = SongLyrics.model_validate(req.json()) - return result.data[0].attributes.ttml + if result.data: + return result.data[0].attributes.ttml + else: + return None @alru_cache diff --git a/src/rip.py b/src/rip.py index be42581..f461ae0 100644 --- a/src/rip.py +++ b/src/rip.py @@ -48,7 +48,10 @@ async def rip_song(song: Song, auth_params: GlobalAuthParams, codec: str, config f"Use storefront {auth_params.storefront.upper()} to get lyrics") lyrics = await get_song_lyrics(song.id, auth_params.storefront, auth_params.accountAccessToken, auth_params.dsid, auth_params.accountToken, config.region.language) - song_metadata.lyrics = lyrics + if lyrics: + song_metadata.lyrics = lyrics + else: + logger.warning(f"Unable to get lyrics of song: {song_metadata.artist} - {song_metadata.title}") if config.m3u8Api.enable and codec == Codec.ALAC and not specified_m3u8: m3u8_url = await get_m3u8_from_api(config.m3u8Api.endpoint, song.id, config.m3u8Api.enable) if m3u8_url: