Skip to content

Bulk asynchronous downloader for multiple file hosts

Use default system dirs for app files (#520)

Arkadiy Illarionov0311b4a94c85
diff --git a/Pipfile b/Pipfile
index 1be6d3ad4..cde315084 100644
--- a/Pipfile
+++ b/Pipfile
@@ -12,6 +12,7 @@ asyncpraw = "==7.7.0"
beautifulsoup4 = "==4.12.2"
certifi = "==2023.5.7"
myjdapi = "==1.1.7"
+platformdirs = "==3.8.1"
PyYAML = "==6.0"
rich = "==13.4.2"
setuptools = "==68.0.0"
diff --git a/README.md b/README.md
index c45878199..4ee4f70ef 100644
--- a/README.md
+++ b/README.md
@@ -165,6 +165,7 @@ optional arguments:
--proxy HTTP/HTTPS proxy used for downloading, format [protocol]://[ip]:[port]
--remove-bunkr-identifier removes the bunkr added identifier from output filenames
--required-free-space required free space (in gigabytes) for the program to run
+--use-sys-dirs use default system directories for application files
--sort-downloads sorts downloaded files after downloads have finished
--sort-directory folder to download files to
diff --git a/cyberdrop_dl/base_functions/config_schema.py b/cyberdrop_dl/base_functions/config_schema.py
index ba832faee..35f9fa1a6 100644
--- a/cyberdrop_dl/base_functions/config_schema.py
+++ b/cyberdrop_dl/base_functions/config_schema.py
@@ -22,7 +22,6 @@ config_default: Dict = {
"xbunker_password": "",
},
"Files": {
- "variable_cache_file": "internal_variable_cache.yaml",
"db_file": "download_history.sqlite",
"errored_download_urls_file": "Errored_Download_URLs.csv",
"errored_scrape_urls_file": "Errored_Scrape_URLs.csv",
@@ -31,6 +30,7 @@ config_default: Dict = {
"output_folder": "Downloads",
"output_last_forum_post_file": "URLs_last_post.txt",
"unsupported_urls_file": "Unsupported_URLs.csv",
+ "variable_cache_file": "internal_variable_cache.yaml",
},
"Forum_Options": {
"output_last_forum_post": False,
diff --git a/cyberdrop_dl/main.py b/cyberdrop_dl/main.py
index a8688acd0..fae62f934 100644
--- a/cyberdrop_dl/main.py
+++ b/cyberdrop_dl/main.py
@@ -9,6 +9,7 @@ from typing import Dict, List, Tuple
import aiofiles
import aiorun
+from platformdirs import PlatformDirs
from yarl import URL
from cyberdrop_dl.base_functions.base_functions import (
@@ -33,7 +34,7 @@ from .base_functions.base_functions import MAX_NAME_LENGTHS
from .base_functions.data_classes import ForumItem, SkipData
-def parse_args() -> argparse.Namespace:
+def parse_args(app_dirs: PlatformDirs) -> argparse.Namespace:
"""Parses the command line arguments passed into the program"""
parser = argparse.ArgumentParser(description="Bulk downloader for multiple file hosts")
parser.add_argument("-V", "--version", action="version", version=f"%(prog)s {VERSION}")
@@ -47,13 +48,13 @@ def parse_args() -> argparse.Namespace:
path_opts.add_argument("-o", "--output-folder", type=Path, help="folder to download files to (default: %(default)s)", default=config_group["output_folder"])
path_opts.add_argument("--config-file", type=Path, help="config file to read arguments from (default: %(default)s)", default="config.yaml")
- path_opts.add_argument("--variable-cache-file", type=Path, help="internal variable cache file to read from and write to (default: %(default)s)", default=config_group["variable_cache_file"])
- path_opts.add_argument("--db-file", type=Path, help="history database file to write to (default: %(default)s)", default=config_group["db_file"])
+ path_opts.add_argument("--db-file", type=Path, help=f"history database file to write to (default: {app_dirs.user_data_dir}/%(default)s)", default=config_group["db_file"])
path_opts.add_argument("--errored-download-urls-file", type=Path, default=config_group["errored_download_urls_file"], help="csv file to write failed download information to (default: %(default)s)")
path_opts.add_argument("--errored-scrape-urls-file", type=Path, default=config_group["errored_scrape_urls_file"], help="csv file to write failed scrape information to (default: %(default)s)")
path_opts.add_argument("--log-file", type=Path, help="log file to write to (default: %(default)s)", default=config_group["log_file"])
path_opts.add_argument("--output-last-forum-post-file", type=Path, default=config_group["output_last_forum_post_file"], help="the text file to output last scraped post from a forum thread for re-feeding into CDL (default: %(default)s)")
path_opts.add_argument("--unsupported-urls-file", type=Path, default=config_group["unsupported_urls_file"], help="the csv file to output unsupported links into (default: %(default)s)")
+ path_opts.add_argument("--variable-cache-file", type=Path, help=f"internal variable cache file to read from and write to (default: {app_dirs.user_cache_dir}/%(default)s)", default=config_group["variable_cache_file"])
# Ignore
config_group = config_data["Ignore"]
@@ -96,6 +97,7 @@ def parse_args() -> argparse.Namespace:
runtime_opts.add_argument("--proxy", help="HTTP/HTTPS proxy used for downloading, format [protocol]://[ip]:[port]", default=config_group["proxy"])
runtime_opts.add_argument("--remove-bunkr-identifier", help="removes the bunkr added identifier from output filenames", action="store_true")
runtime_opts.add_argument("--required-free-space", type=int, default=config_group["required_free_space"], help="required free space (in gigabytes) for the program to run (default: %(default)s)")
+ runtime_opts.add_argument("--use-sys-dirs", help="use default system directories for application files", action="store_true")
# Sorting
config_group = config_data["Sorting"]
@@ -168,6 +170,14 @@ def parse_args() -> argparse.Namespace:
return parser.parse_args()
+def get_app_file_path(app_file: Path, app_dir: Path) -> Path:
+ """Adjust application file path according to OS settings"""
+ if app_file.exists() or app_file.is_absolute():
+ return app_file
+ app_dir.mkdir(parents=True, exist_ok=True)
+ return app_dir / app_file
+
+
async def file_management(args: Dict, links: List) -> Tuple[ErrorFileWriter, CacheManager]:
"""We handle file defaults here (resetting and creation)"""
input_file = args['Files']['input_file']
@@ -342,14 +352,29 @@ async def director(args: Dict, links: List) -> None:
def main(args=None):
+ app_dirs = PlatformDirs("Cyberdrop-DL")
if not args:
- args = parse_args()
+ args = parse_args(app_dirs)
atexit.register(lambda: print("\x1b[?25h"))
links = args.links
+ use_sys_dirs = args.use_sys_dirs
+
+ if use_sys_dirs:
+ args.config_file = get_app_file_path(args.config_file, app_dirs.user_config_path)
+
args = run_args(args.config_file, argparse.Namespace(**vars(args)).__dict__)
+ args["Files"]["db_file"] = get_app_file_path(args["Files"]["db_file"], app_dirs.user_data_path)
+ args["Files"]["variable_cache_file"] = get_app_file_path(args["Files"]["variable_cache_file"], app_dirs.user_cache_path)
+ if use_sys_dirs:
+ args["Files"]["errored_download_urls_file"] = get_app_file_path(args["Files"]["errored_download_urls_file"], app_dirs.user_log_path)
+ args["Files"]["errored_scrape_urls_file"] = get_app_file_path(args["Files"]["errored_scrape_urls_file"], app_dirs.user_log_path)
+ args["Files"]["log_file"] = get_app_file_path(args["Files"]["log_file"], app_dirs.user_log_path)
+ args["Files"]["output_last_forum_post_file"] = get_app_file_path(args["Files"]["output_last_forum_post_file"], app_dirs.user_state_path)
+ args["Files"]["unsupported_urls_file"] = get_app_file_path(args["Files"]["unsupported_urls_file"], app_dirs.user_log_path)
+
logging.basicConfig(
filename=args["Files"]["log_file"],
level=logging.DEBUG,
diff --git a/requirements.txt b/requirements.txt
index 18bf8bc20..1bd611ff3 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -6,6 +6,7 @@ asyncpraw==7.7.0
beautifulsoup4==4.12.2
certifi==2023.5.7
myjdapi==1.1.7
+platformdirs==3.8.1
PyYAML==6.0
rich==13.4.2
setuptools==68.0.0
diff --git a/setup.cfg b/setup.cfg
index 109fae0bf..914d09204 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -23,6 +23,7 @@ install_requires =
beautifulsoup4>=4.12.2
certifi>=2023.5.7
myjdapi>=1.1.7
+ platformdirs>=3.8.1
PyYAML>=6.0
rich>=13.4.2
setuptools>=68.0.0