Skip to content

Commit

Permalink
fix sterilize (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryu1845 committed Dec 16, 2021
1 parent eda4212 commit 2f4f9f8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 5 additions & 2 deletions twspace_dl/FormatInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def set_info(self, metadata: dict) -> None:
root = defaultdict(str, metadata["data"]["audioSpace"]["metadata"])
creator_info = root["creator_results"]["result"]["legacy"] # type: ignore
self["id"] = root["rest_id"]
self["url"] = "https://twitter.com/spaces/" + self["id"]
self["url"] = "https://twitter.com/i/spaces/" + self["id"]
self["title"] = root["title"]
self["creator_name"] = creator_info["name"] # type: ignore
self["creator_screen_name"] = creator_info["screen_name"] # type: ignore
Expand Down Expand Up @@ -100,4 +100,7 @@ def sterilize_fn(value: str) -> str:
return value + extension

def format(self, format_str: str) -> str:
return format_str % self
actual_format_str = os.path.basename(format_str)
abs_dir = os.path.dirname(format_str)
basename = self.sterilize_fn(actual_format_str % self)
return os.path.join(abs_dir, basename)
8 changes: 4 additions & 4 deletions twspace_dl/TwspaceDL.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def playlist_text(self) -> str:

def write_playlist(self, save_dir: str = "./") -> None:
"""Write the modified playlist for external use"""
filename = FormatInfo.sterilize_fn(os.path.basename(self.filename)) + ".m3u8"
filename = os.path.basename(self.filename) + ".m3u8"
path = os.path.join(save_dir, filename)
with open(path, "w", encoding="utf-8") as stream_io:
stream_io.write(self.playlist_text)
Expand Down Expand Up @@ -245,7 +245,7 @@ def download(self) -> None:
f"episode_id={self.id}",
]

filename = FormatInfo.sterilize_fn(os.path.basename(self.filename))
filename = os.path.basename(self.filename)
filename_m3u8 = os.path.join(tempdir, filename + ".m3u8")
filename_old = os.path.join(tempdir, filename + ".m4a")
cmd_old = cmd_base.copy()
Expand Down Expand Up @@ -276,7 +276,7 @@ def download(self) -> None:
cmd_final.insert(3, "-safe")
cmd_final.insert(4, "0")
cmd_final.insert(10, concat_fn)
cmd_final.append(FormatInfo.sterilize_fn(self.filename) + ".m4a")
cmd_final.append(self.filename + ".m4a")

try:
subprocess.run(cmd_new, check=True)
Expand All @@ -289,6 +289,6 @@ def download(self) -> None:
subprocess.run(cmd_old, check=True)
except subprocess.CalledProcessError as err:
raise RuntimeError(" ".join(err.cmd)) from err
shutil.move(filename_old, FormatInfo.sterilize_fn(self.filename) + ".m4a")
shutil.move(filename_old, self.filename + ".m4a")

logging.info("Finished downloading")

0 comments on commit 2f4f9f8

Please sign in to comment.