|
|
|
@ -15,6 +15,7 @@ from src.rip import rip_song, rip_album, rip_artist, rip_playlist |
|
|
|
|
from src.types import GlobalAuthParams |
|
|
|
|
from src.url import AppleMusicURL, URLType, Song |
|
|
|
|
from src.utils import get_song_id_from_m3u8 |
|
|
|
|
from src.mitm import start_proxy |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NewInteractiveShell: |
|
|
|
@ -41,12 +42,7 @@ class NewInteractiveShell: |
|
|
|
|
default="alac") |
|
|
|
|
download_parser.add_argument("-f", "--force", default=False, action="store_true") |
|
|
|
|
download_parser.add_argument("--include-participate-songs", default=False, dest="include", action="store_true") |
|
|
|
|
download_from_file_parser = subparser.add_parser("download-from-file", aliases=["dlf"]) |
|
|
|
|
download_from_file_parser.add_argument("file", type=str) |
|
|
|
|
download_from_file_parser.add_argument("-f", "--force", default=False, action="store_true") |
|
|
|
|
download_from_file_parser.add_argument("-c", "--codec", |
|
|
|
|
choices=["alac", "ec3", "aac", "aac-binaural", "aac-downmix", "ac3"], |
|
|
|
|
default="alac") |
|
|
|
|
# download_from_file_parser = subparser.add_parser("download-from-file", aliases=["dlf"]) |
|
|
|
|
m3u8_parser = subparser.add_parser("m3u8") |
|
|
|
|
m3u8_parser.add_argument("url", type=str) |
|
|
|
|
m3u8_parser.add_argument("-c", "--codec", |
|
|
|
@ -54,6 +50,11 @@ class NewInteractiveShell: |
|
|
|
|
default="alac") |
|
|
|
|
m3u8_parser.add_argument("-f", "--force", default=False, action="store_true") |
|
|
|
|
subparser.add_parser("exit") |
|
|
|
|
mitm_parser = subparser.add_parser("mitm") |
|
|
|
|
mitm_parser.add_argument("-c", "--codec", |
|
|
|
|
choices=["alac", "ec3", "aac", "aac-binaural", "aac-downmix", "ac3"], |
|
|
|
|
default="alac") |
|
|
|
|
mitm_parser.add_argument("-f", "--force", default=False, action="store_true") |
|
|
|
|
|
|
|
|
|
logger.remove() |
|
|
|
|
logger.add(lambda msg: print_formatted_text(ANSI(msg), end=""), colorize=True, level="INFO") |
|
|
|
@ -78,7 +79,7 @@ class NewInteractiveShell: |
|
|
|
|
cmds = cmd.split(" ") |
|
|
|
|
try: |
|
|
|
|
args = self.parser.parse_args(cmds) |
|
|
|
|
except (argparse.ArgumentError, argparse.ArgumentTypeError, SystemExit): |
|
|
|
|
except argparse.ArgumentError: |
|
|
|
|
logger.warning(f"Unknown command: {cmd}") |
|
|
|
|
return |
|
|
|
|
match cmds[0]: |
|
|
|
@ -86,8 +87,8 @@ class NewInteractiveShell: |
|
|
|
|
await self.do_download(args.url, args.codec, args.force, args.include) |
|
|
|
|
case "m3u8": |
|
|
|
|
await self.do_m3u8(args.url, args.codec, args.force) |
|
|
|
|
case "download-from-file" | "dlf": |
|
|
|
|
await self.do_download_from_file(args.file, args.codec, args.force) |
|
|
|
|
case "mitm": |
|
|
|
|
await self.do_mitm(args.codec, args.force) |
|
|
|
|
case "exit": |
|
|
|
|
self.loop.stop() |
|
|
|
|
sys.exit() |
|
|
|
@ -133,13 +134,36 @@ class NewInteractiveShell: |
|
|
|
|
specified_m3u8=m3u8_url) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
async def do_download_from_file(self, file: str, codec: str, force_download: bool): |
|
|
|
|
with open(file, "r", encoding="utf-8") as f: |
|
|
|
|
urls = f.readlines() |
|
|
|
|
for url in urls: |
|
|
|
|
task = self.loop.create_task(self.do_download(raw_url=url, codec=codec, force_download=force_download)) |
|
|
|
|
self.tasks.append(task) |
|
|
|
|
task.add_done_callback(self.tasks.remove) |
|
|
|
|
async def do_mitm(self, codec: str, force_download: bool): |
|
|
|
|
available_device = await self._get_available_device(self.config.region.defaultStorefront) |
|
|
|
|
global_auth_param = GlobalAuthParams.from_auth_params_and_token(available_device.get_auth_params(), |
|
|
|
|
self.anonymous_access_token) |
|
|
|
|
m3u8_urls = set() |
|
|
|
|
tasks = set() |
|
|
|
|
|
|
|
|
|
async def upload(song_id: str, m3u8_url: str): |
|
|
|
|
song_info = await get_song_info(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) |
|
|
|
|
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(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)) |
|
|
|
|
|
|
|
|
|
async def _get_available_device(self, storefront: str): |
|
|
|
|
devices = self.storefront_device_mapping.get(storefront) |
|
|
|
|