Skip to content

Commit

Permalink
feat: asynchronous get_m3u8_from_api func and add config
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldObservationLog committed May 6, 2024
1 parent 7e3751c commit fd78445
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class Device(BaseModel):
suMethod: str


class M3U8Api(BaseModel):
enable: bool
endpoint: str


class Download(BaseModel):
proxy: str
parallelNum: int
Expand All @@ -28,7 +33,6 @@ class Download(BaseModel):
coverFormat: str
coverSize: str
afterDownloaded: str
check: str


class Metadata(BaseModel):
Expand All @@ -43,6 +47,7 @@ class Mitm(BaseModel):
class Config(BaseModel):
region: Region
devices: list[Device]
m3u8Api: M3U8Api
download: Download
metadata: Metadata
mitm: Mitm
Expand Down
16 changes: 6 additions & 10 deletions src/rip.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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!")
Expand Down

0 comments on commit fd78445

Please sign in to comment.