Skip to content

Commit

Permalink
Fix --keep-files (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryu1845 committed Sep 28, 2022
1 parent 65fbe55 commit 7fad013
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
5 changes: 3 additions & 2 deletions twspace_dl/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import logging
import os
import shutil
import sys
from types import TracebackType
from typing import Iterable, Type
Expand Down Expand Up @@ -124,8 +125,8 @@ def space(args: argparse.Namespace) -> int:
except KeyboardInterrupt:
logging.info("Download Interrupted by user")
finally:
if not args.keep_files and os.path.exists(twspace_dl.tempdir.name):
twspace_dl.tempdir.cleanup()
if not args.keep_files and os.path.exists(twspace_dl.tempdir):
shutil.rmtree(twspace_dl.tempdir)
return EXIT_CODE_SUCCESS


Expand Down
12 changes: 6 additions & 6 deletions twspace_dl/twspace_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, space: Twspace, format_str: str) -> None:
self.space = space
self.format_str = format_str or DEFAULT_FNAME_FORMAT
self.session = requests.Session()
self._tempdir = tempfile.TemporaryDirectory(dir=".")
self._tempdir = tempfile.mkdtemp(dir=".")

@cached_property
def filename(self) -> str:
Expand Down Expand Up @@ -104,7 +104,7 @@ def download(self) -> None:
if not shutil.which("ffmpeg"):
raise FileNotFoundError("ffmpeg not installed")
space = self.space
self.write_playlist(save_dir=self._tempdir.name)
self.write_playlist(save_dir=self._tempdir)
state = space["state"]

cmd_base = [
Expand All @@ -125,8 +125,8 @@ def download(self) -> None:
]

filename = os.path.basename(self.filename)
filename_m3u8 = os.path.join(self._tempdir.name, filename + ".m3u8")
filename_old = os.path.join(self._tempdir.name, filename + ".m4a")
filename_m3u8 = os.path.join(self._tempdir, filename + ".m3u8")
filename_old = os.path.join(self._tempdir, filename + ".m4a")
cmd_old = cmd_base.copy()
cmd_old.insert(1, "-protocol_whitelist")
cmd_old.insert(2, "file,https,tls,tcp")
Expand All @@ -135,12 +135,12 @@ def download(self) -> None:
logging.debug("Command for the old part: %s", " ".join(cmd_old))

if state == "Running":
filename_new = os.path.join(self._tempdir.name, filename + "_new.m4a")
filename_new = os.path.join(self._tempdir, 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(self._tempdir.name, "list.txt")
concat_fn = os.path.join(self._tempdir, "list.txt")
with open(concat_fn, "w", encoding="utf-8") as list_io:
list_io.write(
"file "
Expand Down

0 comments on commit 7fad013

Please sign in to comment.