feat: asynchronous get_m3u8_from_api func and add config

pull/8/head
WorldObservationLog 6 months ago
parent 7e3751c0a4
commit fd78445967
  1. 4
      src/api.py
  2. 7
      src/config.py
  3. 16
      src/rip.py

@ -26,6 +26,10 @@ def init_client_and_lock(proxy: str, parallel_num: int):
lock = asyncio.Semaphore(parallel_num) 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)), @retry(retry=retry_if_exception_type((httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError)),
stop=stop_after_attempt(5), stop=stop_after_attempt(5),
before_sleep=before_sleep_log(logger, logging.WARN)) before_sleep=before_sleep_log(logger, logging.WARN))

@ -15,6 +15,11 @@ class Device(BaseModel):
suMethod: str suMethod: str
class M3U8Api(BaseModel):
enable: bool
endpoint: str
class Download(BaseModel): class Download(BaseModel):
proxy: str proxy: str
parallelNum: int parallelNum: int
@ -28,7 +33,6 @@ class Download(BaseModel):
coverFormat: str coverFormat: str
coverSize: str coverSize: str
afterDownloaded: str afterDownloaded: str
check: str
class Metadata(BaseModel): class Metadata(BaseModel):
@ -43,6 +47,7 @@ class Mitm(BaseModel):
class Config(BaseModel): class Config(BaseModel):
region: Region region: Region
devices: list[Device] devices: list[Device]
m3u8Api: M3U8Api
download: Download download: Download
metadata: Metadata metadata: Metadata
mitm: Mitm mitm: Mitm

@ -1,10 +1,9 @@
import asyncio import asyncio
import subprocess import subprocess
import httpx
from loguru import logger 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.config import Config, Device
from src.decrypt import decrypt from src.decrypt import decrypt
from src.metadata import SongMetadata 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, lyrics = await get_song_lyrics(song.id, song.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 song_metadata.lyrics = lyrics
if "http" in config.download.check and codec == Codec.ALAC: if config.m3u8Api.enable and codec == Codec.ALAC:
params = ( m3u8_url = await get_m3u8_from_api(config.m3u8Api.endpoint, song.id)
('songid', song.id),
)
m3u8_url = httpx.get(config.download.check, params=params, verify=False).text
if "m3u8" in m3u8_url: if "m3u8" in m3u8_url:
song_data.attributes.extendedAssetUrls.enhancedHls = m3u8_url specified_m3u8 = m3u8_url
logger.info("Found m3u8 from API") logger.info("Use m3u8 from API")
if specified_m3u8: if specified_m3u8:
song_uri, keys = await extract_media(specified_m3u8, codec, song_metadata, song_uri, keys = await extract_media(specified_m3u8, codec, song_metadata,
config.download.codecPriority, config.download.codecAlternative) 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) song = encapsulate(song_info, decrypted_song, config.download.atmosConventToM4a)
if codec != Codec.EC3 or (codec == Codec.EC3 and 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) 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) song = write_metadata(song, song_metadata, config.metadata.embedMetadata, config.download.coverFormat)
filename = save(song, codec, song_metadata, config.download) filename = save(song, codec, song_metadata, config.download)
logger.info(f"Song {song_metadata.artist} - {song_metadata.title} saved!") logger.info(f"Song {song_metadata.artist} - {song_metadata.title} saved!")

Loading…
Cancel
Save