From 143dce7214571298efcd9c18772d9f08e9c0b767 Mon Sep 17 00:00:00 2001 From: Ryu18 Date: Sun, 7 Nov 2021 23:03:46 +0100 Subject: [PATCH] fixed #6 --- twspace_dl/__main__.py | 83 +++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/twspace_dl/__main__.py b/twspace_dl/__main__.py index dae6082..9837324 100644 --- a/twspace_dl/__main__.py +++ b/twspace_dl/__main__.py @@ -5,6 +5,7 @@ import os import re import shutil +import shlex import subprocess import sys from collections import defaultdict @@ -200,10 +201,8 @@ def playlist_text(self) -> str: def write_playlist(self, save_dir: str = "./") -> None: """Write the modified playlist for external use""" - filename = self.filename - with open( - os.path.join(save_dir, f"{filename}.m3u8"), "w", encoding="utf-8" - ) as stream_io: + filename = os.path.join(save_dir, shlex.quote(self.filename) + ".m3u8") + with open(filename, "w", encoding="utf-8") as stream_io: stream_io.write(self.playlist_text) logging.info(f"{filename}.m3u8 written to disk") @@ -228,50 +227,50 @@ def download(self) -> None: "-c", "copy", "-metadata", - f"title='{format_info['title']}'", + f"title={format_info['title']}", "-metadata", - f"author='{format_info['creator_name']}'", + f"author={format_info['creator_name']}", "-metadata", - f"episode_id='{self.id}'", - os.path.join("tmp", f"{self.filename}.m4a"), + f"episode_id={self.id}", ] - cmd_old = ( - [cmd_base[0]] - + [ - "-protocol_whitelist", - "file,https,tls,tcp", - ] - + cmd_base[1:6] - + [ - os.path.join("tmp", self.filename + ".m3u8"), - ] - + cmd_base[6:] - ) + cmd_base = [shlex.quote(element) for element in cmd_base] - if state == "Running": - cmd_new = ( - cmd_base[:6] - + [self.dyn_url] - + cmd_base[6:-1] - + [os.path.join("tmp", f"{self.filename}_new.m4a")] - ) + filename_m3u8 = os.path.join("tmp", shlex.quote(self.filename) + ".m3u8") + filename_old = os.path.join("tmp", (self.filename) + ".m4a") + cmd_old = cmd_base.copy() + cmd_old.insert(1, "-protocol_whitelist") + cmd_old.insert(2, "file,https,tls,tcp") + cmd_old.insert(8, filename_m3u8) + cmd_old.append(filename_old) - cmd_final = ( - cmd_base[:6] - + [ - ( - "concat:" - + os.path.join("tmp", f"{self.filename}.m4a") - + "|" - + os.path.join("tmp", f"{self.filename}_new.m4a") - ) - ] - + cmd_base[6:-1] - + [f"{self.filename}.m4a"] - ) + if state == "Running": + filename_new = os.path.join("tmp", (self.filename) + "_new.m4a") + cmd_new = cmd_base.copy() + cmd_new.insert(6, (self.dyn_url)) + cmd_new.append(filename_new) + + concat_fn = os.path.join("tmp", "list.txt") + with open(concat_fn, "w", encoding="utf-8") as list_io: + list_io.write( + "file " + + f"'{os.path.join(os.getcwd(), filename_old)}'" + + "\n" + + "file " + + f"'{os.path.join(os.getcwd(), filename_new)}'" + ) - with ThreadPoolExecutor(max_workers=self.threads) as executor: - executor.map(subprocess.run, (cmd_new, cmd_old), timeout=60) + cmd_final = cmd_base.copy() + cmd_final.insert(1, "-f") + cmd_final.insert(2, "concat") + cmd_final.insert(3, "-safe") + cmd_final.insert(4, "0") + cmd_final.insert(10, concat_fn) + cmd_final.append(shlex.quote(self.filename) + ".m4a") + + # with ThreadPoolExecutor(max_workers=self.threads) as executor: + # executor.map(subprocess.run, (cmd_new, cmd_old), timeout=60) + subprocess.run(cmd_new, check=True) + subprocess.run(cmd_old, check=True) subprocess.run(cmd_final, check=True) else: subprocess.run(cmd_old, check=True)