fix: raise exception when unable to get lyrics

with_status
WorldObservationLog 4 months ago
parent e493a5290b
commit bc98705375
  1. 8
      src/api.py
  2. 5
      src/rip.py

@ -1,6 +1,7 @@
import asyncio import asyncio
import logging import logging
from ssl import SSLError from ssl import SSLError
from typing import Optional
import httpx import httpx
import regex 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)), @retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)),
wait=wait_random_exponential(multiplier=1, max=60), wait=wait_random_exponential(multiplier=1, max=60),
stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) 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: async with request_lock:
req = await client.get(f"https://amp-api.music.apple.com/v1/catalog/{storefront}/songs/{song_id}/lyrics", req = await client.get(f"https://amp-api.music.apple.com/v1/catalog/{storefront}/songs/{song_id}/lyrics",
params={"l": lang}, params={"l": lang},
@ -175,7 +176,10 @@ async def get_song_lyrics(song_id: str, storefront: str, token: str, dsid: str,
"X-Dsid": dsid}, "X-Dsid": dsid},
cookies={f"mz_at_ssl-{dsid}": account_token}) cookies={f"mz_at_ssl-{dsid}": account_token})
result = SongLyrics.model_validate(req.json()) 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 @alru_cache

@ -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") f"Use storefront {auth_params.storefront.upper()} to get lyrics")
lyrics = await get_song_lyrics(song.id, auth_params.storefront, auth_params.accountAccessToken, lyrics = await get_song_lyrics(song.id, auth_params.storefront, auth_params.accountAccessToken,
auth_params.dsid, auth_params.accountToken, config.region.language) 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: 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) m3u8_url = await get_m3u8_from_api(config.m3u8Api.endpoint, song.id, config.m3u8Api.enable)
if m3u8_url: if m3u8_url:

Loading…
Cancel
Save