Skip to content

Commit

Permalink
feat: add m3u8api upload
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldObservationLog committed May 6, 2024
1 parent fc43a72 commit 668b64c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
17 changes: 13 additions & 4 deletions src/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 668b64c

Please sign in to comment.