Skip to content

Bulk asynchronous downloader for multiple file hosts

Purge empty directories

Jules-WinnfieldX335402fcd81e
diff --git a/cyberdrop_dl/__init__.py b/cyberdrop_dl/__init__.py
index 8f57b15e9..d00ccbef0 100644
--- a/cyberdrop_dl/__init__.py
+++ b/cyberdrop_dl/__init__.py
@@ -1 +1 @@
-__version__ = "2.13.4"
+__version__ = "2.13.5"
diff --git a/cyberdrop_dl/main.py b/cyberdrop_dl/main.py
index 2d32842a3..e1b24d1d7 100644
--- a/cyberdrop_dl/main.py
+++ b/cyberdrop_dl/main.py
@@ -68,6 +68,9 @@ async def download_all(args: argparse.Namespace):
all_files = [str(f) for f in pathlib.Path(args.output_folder).glob("**/*") if f.is_file()]
combined = '\t'.join(all_files)
+ log('Purging empty directories')
+ deleted = purge_dir(args.output_folder)
+
log('Finished downloading. Enjoy :)')
if '.part' in combined:
log('There are still partial downloads in your folders, please re-run the program.')
diff --git a/cyberdrop_dl/utils/base_functions.py b/cyberdrop_dl/utils/base_functions.py
index eac8adcb2..608e8004b 100644
--- a/cyberdrop_dl/utils/base_functions.py
+++ b/cyberdrop_dl/utils/base_functions.py
@@ -106,6 +106,23 @@ def make_title_safe(title: str):
return title
+def purge_dir(dir, in_place=True):
+
+ deleted = []
+ dir_tree = list(os.walk(dir, topdown=False))
+
+ for tree_element in dir_tree:
+ sub_dir = tree_element[0]
+ is_empty = not len(os.listdir(sub_dir))
+ if is_empty:
+ deleted.append(sub_dir)
+
+ if in_place:
+ list(map(os.rmdir, deleted))
+
+ return deleted
+
+
def regex_links(urls) -> list:
all_links = [x.group().replace(".md.", ".") for x in
re.finditer(r"(?:http.*?)(?=('|$|\n|\r\n|\r|\s|\"|\[/URL]))", urls)]