Skip to content

Commit

Permalink
fix: minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NTFSvolume committed Nov 8, 2024
1 parent 3f46cb6 commit f3b588f
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 64 deletions.
2 changes: 1 addition & 1 deletion cyberdrop_dl/clients/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __init__(self, origin: ScrapeItem | URL | None = None) -> None:
class RestrictedFiletypeError(CDLBaseError):
def __init__(self, origin: ScrapeItem | URL | None = None) -> None:
"""This error will be thrown when has a filytpe not allowed by config."""
ui_message = "Insufficient Free Space"
ui_message = "Restricted Filetype"
super().__init__(ui_message, origin=origin)


Expand Down
6 changes: 3 additions & 3 deletions cyberdrop_dl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ def startup() -> Manager:

except InvalidYamlError as e:
print_to_console(e.message_rich)
exit(1)
sys.exit(1)

except KeyboardInterrupt:
print_to_console("Exiting...")
exit(0)
sys.exit(0)


async def runtime(manager: Manager) -> None:
Expand Down Expand Up @@ -225,7 +225,7 @@ def main() -> None:
print_to_console("Trying to Exit ...")
with contextlib.suppress(Exception):
asyncio.run(manager.close())
exit(1)
sys.exit(1)
loop.close()
sys.exit(0)

Expand Down
12 changes: 7 additions & 5 deletions cyberdrop_dl/managers/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,16 @@ def load_configs(self) -> None:
if self.settings.is_file():
self._verify_settings_config()
else:
from cyberdrop_dl.managers.path_manager import APP_STORAGE, DOWNLOAD_STORAGE
from cyberdrop_dl.utils import constants

self.settings_data = copy.deepcopy(settings)
self.settings_data["Files"]["input_file"] = APP_STORAGE / "Configs" / self.loaded_config / "URLs.txt"
self.settings_data["Files"]["download_folder"] = DOWNLOAD_STORAGE / "Cyberdrop-DL Downloads"
self.settings_data["Logs"]["log_folder"] = APP_STORAGE / "Configs" / self.loaded_config / "Logs"
self.settings_data["Files"]["input_file"] = (
constants.APP_STORAGE / "Configs" / self.loaded_config / "URLs.txt"
)
self.settings_data["Files"]["download_folder"] = constants.DOWNLOAD_STORAGE / "Cyberdrop-DL Downloads"
self.settings_data["Logs"]["log_folder"] = constants.APP_STORAGE / "Configs" / self.loaded_config / "Logs"
self.settings_data["Logs"]["webhook_url"] = ""
self.settings_data["Sorting"]["sort_folder"] = DOWNLOAD_STORAGE / "Cyberdrop-DL Sorted Downloads"
self.settings_data["Sorting"]["sort_folder"] = constants.DOWNLOAD_STORAGE / "Cyberdrop-DL Sorted Downloads"
self.settings_data["Sorting"]["scan_folder"] = None
self.write_updated_settings_config()

Expand Down
17 changes: 7 additions & 10 deletions cyberdrop_dl/managers/path_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
from pathlib import Path
from typing import TYPE_CHECKING

from cyberdrop_dl.utils.constants import DEBUG_VAR
from cyberdrop_dl.utils import constants

if TYPE_CHECKING:
from cyberdrop_dl.managers.manager import Manager
from cyberdrop_dl.utils.dataclasses.url_objects import MediaItem

APP_STORAGE = Path("./AppData")
DOWNLOAD_STORAGE = Path("./Downloads")

if DEBUG_VAR and Path.cwd().name == "cyberdrop_dl":
if constants.DEBUG_VAR and Path.cwd().name == "cyberdrop_dl":
"""This is for testing purposes only"""
APP_STORAGE = Path("../AppData")
DOWNLOAD_STORAGE = Path("../Downloads")
constants.APP_STORAGE = Path("../AppData")
constants.DOWNLOAD_STORAGE = Path("../Downloads")


class PathManager:
Expand Down Expand Up @@ -48,11 +46,10 @@ def __init__(self, manager: Manager) -> None:

def pre_startup(self) -> None:
if self.manager.args_manager.appdata_dir:
global APP_STORAGE
APP_STORAGE = Path(self.manager.args_manager.appdata_dir) / "AppData"
constants.APP_STORAGE = Path(self.manager.args_manager.appdata_dir) / "AppData"

self.cache_dir = APP_STORAGE / "Cache"
self.config_dir = APP_STORAGE / "Configs"
self.cache_dir = constants.APP_STORAGE / "Cache"
self.config_dir = constants.APP_STORAGE / "Configs"

self.cache_dir.mkdir(parents=True, exist_ok=True)
self.config_dir.mkdir(parents=True, exist_ok=True)
Expand Down
8 changes: 6 additions & 2 deletions cyberdrop_dl/scraper/crawlers/chevereto_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import calendar
import datetime
import re
from typing import TYPE_CHECKING, AsyncGenerator
from typing import TYPE_CHECKING, AsyncGenerator, ClassVar

from aiolimiter import AsyncLimiter
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -48,7 +48,11 @@ class CheveretoCrawler(Crawler):
"img.kiwi": URL("https://img.kiwi"),
}

FOLDER_DOMAINS = {"imagepond.net": "ImagePond", "jpg.church": "JPGChurch", "img.kiwi": "ImgKiwi"}
FOLDER_DOMAINS: ClassVar[dict[str, str]] = {
"imagepond.net": "ImagePond",
"jpg.church": "JPGChurch",
"img.kiwi": "ImgKiwi",
}

DOMAINS = PRIMARY_BASE_DOMAINS.keys() | JPG_CHURCH_DOMAINS

Expand Down
5 changes: 2 additions & 3 deletions cyberdrop_dl/scraper/crawlers/kemono_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@ async def handle_file(file_obj: dict):
files.extend(post["attachments"])

for file in files:
if scrape_item.children_limit:
if scrape_item.children >= scrape_item.children_limit:
raise MaxChildrenError(origin=scrape_item)
if scrape_item.children_limit and scrape_item.children >= scrape_item.children_limit:
raise MaxChildrenError(origin=scrape_item)
await handle_file(file)
scrape_item.children += 1

Expand Down
2 changes: 1 addition & 1 deletion cyberdrop_dl/scraper/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def regex_links(line: str) -> list:
return yarl_links

async def parse_input_file_groups(self) -> dict[str, URL]:
"""Split URLs from input file by their groups"""
"""Split URLs from input file by their groups."""
input_file = self.manager.path_manager.input_file
links = {"": []}
block_quote = False
Expand Down
8 changes: 5 additions & 3 deletions cyberdrop_dl/ui/progress/statistic_progress.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from http import HTTPStatus
from typing import NamedTuple, Union
from typing import NamedTuple

from rich.console import Group
from rich.panel import Panel
Expand Down Expand Up @@ -81,7 +83,7 @@ def update_total(self, total: int) -> None:
completed=task.completed,
)

def add_failure(self, failure_type: Union[str, int]) -> None:
def add_failure(self, failure_type: str | int) -> None:
"""Adds a failed file to the progress bar."""
self.failed_files += 1
if isinstance(failure_type, int):
Expand Down Expand Up @@ -159,7 +161,7 @@ def update_total(self, total: int) -> None:
completed=task.completed,
)

def add_failure(self, failure_type: Union[str, int]) -> None:
def add_failure(self, failure_type: str | int) -> None:
"""Adds a failed site to the progress bar."""
self.failed_files += 1
if isinstance(failure_type, int):
Expand Down
10 changes: 5 additions & 5 deletions cyberdrop_dl/utils/args/config_definitions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from cyberdrop_dl.managers.path_manager import APP_STORAGE, DOWNLOAD_STORAGE
from cyberdrop_dl.utils import constants

authentication_settings: dict = {
"Forums": {
Expand Down Expand Up @@ -71,11 +71,11 @@
"maximum_number_of_children": [],
},
"Files": {
"input_file": str(APP_STORAGE / "Configs" / "{config}" / "URLs.txt"),
"download_folder": str(DOWNLOAD_STORAGE),
"input_file": str(constants.APP_STORAGE / "Configs" / "{config}" / "URLs.txt"),
"download_folder": str(constants.DOWNLOAD_STORAGE),
},
"Logs": {
"log_folder": str(APP_STORAGE / "Configs" / "{config}" / "Logs"),
"log_folder": str(constants.APP_STORAGE / "Configs" / "{config}" / "Logs"),
"webhook_url": "",
"main_log_filename": "downloader.log",
"last_forum_post_filename": "Last_Scraped_Forum_Posts.csv",
Expand Down Expand Up @@ -116,7 +116,7 @@
},
"Sorting": {
"sort_downloads": False,
"sort_folder": str(DOWNLOAD_STORAGE / "Cyberdrop-DL Sorted Downloads"),
"sort_folder": str(constants.DOWNLOAD_STORAGE / "Cyberdrop-DL Sorted Downloads"),
"scan_folder": None,
"sort_cdl_only": True,
"sort_incremementer_format": " ({i})",
Expand Down
7 changes: 6 additions & 1 deletion cyberdrop_dl/utils/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from enum import IntEnum
from pathlib import Path

from rich.text import Text

Expand All @@ -21,7 +22,6 @@
"locals_max_length": 20,
}

BLOCKED_DOMAINS = ("facebook", "instagram", "fbcdn")

# regex
RAR_MULTIPART_PATTERN = re.compile(r"^part\d+")
Expand All @@ -35,13 +35,18 @@ class CustomHTTPStatus(IntEnum):
DDOS_GUARD = 429


BLOCKED_DOMAINS = ("facebook", "instagram", "fbcdn")

STYLE_TO_DIFF_FORMAT_MAP = {
"default": "{}",
"green": "+ {}",
"red": "- {}",
"yellow": "*** {}",
}

APP_STORAGE = Path("./AppData")
DOWNLOAD_STORAGE = Path("./Downloads")


# Pypi
PRELEASE_TAGS = {
Expand Down
2 changes: 1 addition & 1 deletion cyberdrop_dl/utils/dataclasses/supported_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class SupportedDomains:
"nudostar.com",
"xbunker.nu",
)
supported_forums_map = {
supported_forums_map: ClassVar[dict[str, str]] = {
"celebforum.to": "celebforum",
"f95zone.to": "f95zone",
"forums.socialmediagirls.com": "socialmediagirls",
Expand Down
48 changes: 25 additions & 23 deletions cyberdrop_dl/utils/transfer/first_time_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import platformdirs
import yaml

from cyberdrop_dl.managers.path_manager import APP_STORAGE, DOWNLOAD_STORAGE
from cyberdrop_dl.utils import constants
from cyberdrop_dl.utils.transfer.transfer_v4_db import transfer_v4_db

if TYPE_CHECKING:
Expand All @@ -21,41 +21,43 @@ def __init__(self, manager: Manager) -> None:

def startup(self) -> None:
"""Startup."""
OLD_APP_STORAGE: Path = Path(platformdirs.user_config_dir("Cyberdrop-DL"))
OLD_APP_STORAGE = Path(platformdirs.user_config_dir("Cyberdrop-DL"))
OLD_DOWNLOAD_STORAGE = Path(platformdirs.user_downloads_path()) / "Cyberdrop-DL Downloads"

if APP_STORAGE.exists():
cache_file = APP_STORAGE / "Cache" / "cache.yaml"
if constants.APP_STORAGE.exists():
cache_file = constants.APP_STORAGE / "Cache" / "cache.yaml"
if cache_file.is_file() and self.check_cache_for_moved(cache_file):
return

OLD_FILES = Path("./Old Files")
OLD_FILES.mkdir(parents=True, exist_ok=True)

if OLD_APP_STORAGE.exists():
if APP_STORAGE.exists():
if APP_STORAGE.with_name("AppData_OLD").exists():
APP_STORAGE.rename(APP_STORAGE.with_name("AppData_OLD2"))
APP_STORAGE.rename(APP_STORAGE.with_name("AppData_OLD"))
shutil.copytree(OLD_APP_STORAGE, APP_STORAGE, dirs_exist_ok=True)
if constants.APP_STORAGE.exists():
if constants.APP_STORAGE.with_name("AppData_OLD").exists():
constants.APP_STORAGE.rename(constants.APP_STORAGE.with_name("AppData_OLD2"))
constants.APP_STORAGE.rename(constants.APP_STORAGE.with_name("AppData_OLD"))
shutil.copytree(OLD_APP_STORAGE, constants.APP_STORAGE, dirs_exist_ok=True)
shutil.rmtree(OLD_APP_STORAGE)

if OLD_DOWNLOAD_STORAGE.exists():
shutil.copytree(OLD_DOWNLOAD_STORAGE, DOWNLOAD_STORAGE, dirs_exist_ok=True)
shutil.copytree(OLD_DOWNLOAD_STORAGE, constants.DOWNLOAD_STORAGE, dirs_exist_ok=True)
shutil.rmtree(OLD_DOWNLOAD_STORAGE)

if Path("./download_history.sqlite").is_file():
transfer_v4_db(Path("./download_history.sqlite"), APP_STORAGE / "Cache" / "cyberdrop.db")
transfer_v4_db(Path("./download_history.sqlite"), constants.APP_STORAGE / "Cache" / "cyberdrop.db")
Path("./download_history.sqlite").rename(OLD_FILES / "download_history1.sqlite")

if (APP_STORAGE / "download_history.sqlite").is_file():
transfer_v4_db(APP_STORAGE / "download_history.sqlite", APP_STORAGE / "Cache" / "cyberdrop.db")
(APP_STORAGE / "download_history.sqlite").rename(OLD_FILES / "download_history2.sqlite")
if (constants.APP_STORAGE / "download_history.sqlite").is_file():
transfer_v4_db(
constants.APP_STORAGE / "download_history.sqlite", constants.APP_STORAGE / "Cache" / "cyberdrop.db"
)
(constants.APP_STORAGE / "download_history.sqlite").rename(OLD_FILES / "download_history2.sqlite")

if Path("./config.yaml").is_file():
try:
self.transfer_v4_config(Path("./config.yaml"), "Imported V4")
self.update_default_config(APP_STORAGE / "Cache" / "cache.yaml", "Imported V4")
self.update_default_config(constants.APP_STORAGE / "Cache" / "cache.yaml", "Imported V4")
except OSError:
pass
Path("./config.yaml").rename(OLD_FILES / "config.yaml")
Expand All @@ -69,7 +71,7 @@ def startup(self) -> None:
if Path("./Unsupported_URLs.csv").is_file():
Path("./Unsupported_URLs.csv").rename(OLD_FILES / "Unsupported_URLs.csv")

self.update_cache(APP_STORAGE / "Cache" / "cache.yaml")
self.update_cache(constants.APP_STORAGE / "Cache" / "cache.yaml")

@staticmethod
def check_cache_for_moved(cache_file: Path) -> bool:
Expand Down Expand Up @@ -120,10 +122,10 @@ def transfer_v4_config(old_config_path: Path, new_config_name: str) -> None:
if Path("./URLs.txt").is_file():
new_user_data["Files"]["input_file"] = Path("./URLs.txt")
else:
new_user_data["Files"]["input_file"] = APP_STORAGE / "Configs" / new_config_name / "URLs.txt"
new_user_data["Files"]["download_folder"] = DOWNLOAD_STORAGE
new_user_data["Logs"]["log_folder"] = APP_STORAGE / "Configs" / new_config_name / "Logs"
new_user_data["Sorting"]["sort_folder"] = DOWNLOAD_STORAGE / "Cyberdrop-DL Sorted Downloads"
new_user_data["Files"]["input_file"] = constants.APP_STORAGE / "Configs" / new_config_name / "URLs.txt"
new_user_data["Files"]["download_folder"] = constants.DOWNLOAD_STORAGE
new_user_data["Logs"]["log_folder"] = constants.APP_STORAGE / "Configs" / new_config_name / "Logs"
new_user_data["Sorting"]["sort_folder"] = constants.DOWNLOAD_STORAGE / "Cyberdrop-DL Sorted Downloads"

with old_config_path.open() as yaml_file:
old_data = yaml.load(yaml_file.read(), Loader=yaml.FullLoader)
Expand Down Expand Up @@ -220,13 +222,13 @@ def transfer_v4_config(old_config_path: Path, new_config_name: str) -> None:
new_user_data["Sorting"]["sort_folder"] = str(new_user_data["Sorting"]["sort_folder"])

# Write config
new_config_path = APP_STORAGE / "Configs" / new_config_name / "settings.yaml"
new_config_path = constants.APP_STORAGE / "Configs" / new_config_name / "settings.yaml"
new_config_path.parent.mkdir(parents=True, exist_ok=True)
with new_config_path.open("w") as yaml_file:
yaml.dump(new_user_data, yaml_file)
new_auth_path = APP_STORAGE / "Configs" / "authentication.yaml"
new_auth_path = constants.APP_STORAGE / "Configs" / "authentication.yaml"
with new_auth_path.open("w") as yaml_file:
yaml.dump(new_auth_data, yaml_file)
new_global_settings_path = APP_STORAGE / "Configs" / "global_settings.yaml"
new_global_settings_path = constants.APP_STORAGE / "Configs" / "global_settings.yaml"
with new_global_settings_path.open("w") as yaml_file:
yaml.dump(new_global_settings_data, yaml_file)
10 changes: 5 additions & 5 deletions cyberdrop_dl/utils/transfer/transfer_v4_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def transfer_v4_config(manager: Manager, old_config_path: Path, new_config_name:
new_auth_data = manager.config_manager.authentication_data
new_user_data = copy.deepcopy(settings)

from cyberdrop_dl.managers.path_manager import APP_STORAGE, DOWNLOAD_STORAGE
from cyberdrop_dl.managers.path_manager import constants

new_user_data["Files"]["input_file"] = APP_STORAGE / "Configs" / new_config_name / "URLs.txt"
new_user_data["Files"]["download_folder"] = DOWNLOAD_STORAGE / "Cyberdrop-DL Downloads"
new_user_data["Logs"]["log_folder"] = APP_STORAGE / "Configs" / new_config_name / "Logs"
new_user_data["Sorting"]["sort_folder"] = DOWNLOAD_STORAGE / "Cyberdrop-DL Sorted Downloads"
new_user_data["Files"]["input_file"] = constants.APP_STORAGE / "Configs" / new_config_name / "URLs.txt"
new_user_data["Files"]["download_folder"] = constants.DOWNLOAD_STORAGE / "Cyberdrop-DL Downloads"
new_user_data["Logs"]["log_folder"] = constants.APP_STORAGE / "Configs" / new_config_name / "Logs"
new_user_data["Sorting"]["sort_folder"] = constants.DOWNLOAD_STORAGE / "Cyberdrop-DL Sorted Downloads"

new_global_data = manager.config_manager.global_settings_data
old_data = _load_yaml(old_config_path)
Expand Down
2 changes: 1 addition & 1 deletion cyberdrop_dl/utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def remove_file_id(manager: Manager, filename: str, ext: str) -> tuple[str, str]


def parse_bytes(size: int) -> tuple[int, str]:
"""Get human repr of bytes as a tuple of (VALUE , UNIT)"""
"""Get human repr of bytes as a tuple of (VALUE , UNIT)."""
for unit in ["B", "KB", "MB", "GB", "TB", "PB", "EB"]:
if size < 1024:
return size, unit
Expand Down

0 comments on commit f3b588f

Please sign in to comment.