diff --git a/src/api.py b/src/api.py index df37607..2c814b0 100644 --- a/src/api.py +++ b/src/api.py @@ -9,6 +9,7 @@ from tenacity import retry, retry_if_exception_type, stop_after_attempt, before_sleep_log from src.models import * +from src.models.song_data import Datum client: httpx.AsyncClient lock: asyncio.Semaphore @@ -33,6 +34,19 @@ async def get_m3u8_from_api(endpoint: str, song_id: str) -> str: return resp +async def upload_m3u8_to_api(endpoint: str, m3u8_url: str, song_info: Datum): + await client.post(endpoint, json={ + "method": "add_m3u8", + "params": { + "songid": song_info.id, + "song_title": f"Disk {song_info.attributes.discNumber} Track {song_info.attributes.trackNumber} - {song_info.attributes.name}", + "albumid": song_info.relationships.albums.data[0].id, + "album_title": song_info.attributes.albumName, + "m3u8": m3u8_url, + } + }) + + @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/cmd.py b/src/cmd.py index 87e123a..817fc67 100644 --- a/src/cmd.py +++ b/src/cmd.py @@ -9,7 +9,7 @@ from prompt_toolkit.patch_stdout import patch_stdout from src.adb import Device -from src.api import get_token, init_client_and_lock +from src.api import get_token, init_client_and_lock, upload_m3u8_to_api, get_info_from_adam from src.config import Config from src.rip import rip_song, rip_album from src.types import GlobalAuthParams @@ -118,17 +118,26 @@ async def do_mitm(self, codec: str, force_download: bool): m3u8_urls = set() tasks = set() + async def upload(song_id: str, m3u8_url: str): + song_info = await get_info_from_adam(song_id, self.anonymous_access_token, + self.config.region.defaultStorefront, self.config.region.language) + await upload_m3u8_to_api(self.config.m3u8Api.endpoint, m3u8_url, song_info) + def callback(m3u8_url): if m3u8_url in m3u8_urls: return song_id = get_song_id_from_m3u8(m3u8_url) song = Song(id=song_id, storefront=self.config.region.defaultStorefront, url="", type=URLType.Song) - task = self.loop.create_task( + rip_task = self.loop.create_task( rip_song(song, global_auth_param, codec, self.config, available_device, force_save=force_download, specified_m3u8=m3u8_url) ) - tasks.update(task) - task.add_done_callback(tasks.remove) + tasks.update(rip_task) + rip_task.add_done_callback(tasks.remove) + if self.config.m3u8Api.enable: + upload_task = self.loop.create_task(upload(song_id, m3u8_url)) + tasks.update(upload_task) + upload_task.add_done_callback(tasks.remove) m3u8_urls.update(m3u8_url) self.loop.create_task(start_proxy(self.config.mitm.host, self.config.mitm.port, callback))