From fd78445967d22b0f59dbb4da6d35dcb5faff9a91 Mon Sep 17 00:00:00 2001 From: WorldObservationLog Date: Mon, 6 May 2024 17:10:49 +0800 Subject: [PATCH] feat: asynchronous get_m3u8_from_api func and add config --- src/api.py | 4 ++++ src/config.py | 7 ++++++- src/rip.py | 16 ++++++---------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/api.py b/src/api.py index b709e7c..aa12001 100644 --- a/src/api.py +++ b/src/api.py @@ -26,6 +26,10 @@ def init_client_and_lock(proxy: str, parallel_num: int): lock = asyncio.Semaphore(parallel_num) +async def get_m3u8_from_api(endpoint: str, song_id: str) -> str: + return (await client.get(endpoint, params={"songid": song_id})).text + + @retry(retry=retry_if_exception_type((httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError)), stop=stop_after_attempt(5), before_sleep=before_sleep_log(logger, logging.WARN)) diff --git a/src/config.py b/src/config.py index bf73895..5e58330 100644 --- a/src/config.py +++ b/src/config.py @@ -15,6 +15,11 @@ class Device(BaseModel): suMethod: str +class M3U8Api(BaseModel): + enable: bool + endpoint: str + + class Download(BaseModel): proxy: str parallelNum: int @@ -28,7 +33,6 @@ class Download(BaseModel): coverFormat: str coverSize: str afterDownloaded: str - check: str class Metadata(BaseModel): @@ -43,6 +47,7 @@ class Mitm(BaseModel): class Config(BaseModel): region: Region devices: list[Device] + m3u8Api: M3U8Api download: Download metadata: Metadata mitm: Mitm diff --git a/src/rip.py b/src/rip.py index a4a80a6..7c67c0a 100644 --- a/src/rip.py +++ b/src/rip.py @@ -1,10 +1,9 @@ import asyncio import subprocess -import httpx from loguru import logger -from src.api import get_info_from_adam, get_song_lyrics, get_meta, download_song +from src.api import get_info_from_adam, get_song_lyrics, get_meta, download_song, get_m3u8_from_api from src.config import Config, Device from src.decrypt import decrypt from src.metadata import SongMetadata @@ -31,14 +30,11 @@ async def rip_song(song: Song, auth_params: GlobalAuthParams, codec: str, config lyrics = await get_song_lyrics(song.id, song.storefront, auth_params.accountAccessToken, auth_params.dsid, auth_params.accountToken, config.region.language) song_metadata.lyrics = lyrics - if "http" in config.download.check and codec == Codec.ALAC: - params = ( - ('songid', song.id), - ) - m3u8_url = httpx.get(config.download.check, params=params, verify=False).text + if config.m3u8Api.enable and codec == Codec.ALAC: + m3u8_url = await get_m3u8_from_api(config.m3u8Api.endpoint, song.id) if "m3u8" in m3u8_url: - song_data.attributes.extendedAssetUrls.enhancedHls = m3u8_url - logger.info("Found m3u8 from API") + specified_m3u8 = m3u8_url + logger.info("Use m3u8 from API") if specified_m3u8: song_uri, keys = await extract_media(specified_m3u8, codec, song_metadata, config.download.codecPriority, config.download.codecAlternative) @@ -52,7 +48,7 @@ async def rip_song(song: Song, auth_params: GlobalAuthParams, codec: str, config song = encapsulate(song_info, decrypted_song, config.download.atmosConventToM4a) if codec != Codec.EC3 or (codec == Codec.EC3 and config.download.atmosConventToM4a): song = write_metadata(song, song_metadata, config.metadata.embedMetadata, config.download.coverFormat) - if codec != Codec.AC3 or (codec == Codec.AC3 and config.download.atmosConventToM4a): + elif codec != Codec.AC3 or (codec == Codec.AC3 and config.download.atmosConventToM4a): song = write_metadata(song, song_metadata, config.metadata.embedMetadata, config.download.coverFormat) filename = save(song, codec, song_metadata, config.download) logger.info(f"Song {song_metadata.artist} - {song_metadata.title} saved!")