Skip to content

Commit

Permalink
refactor: references
Browse files Browse the repository at this point in the history
  • Loading branch information
NTFSvolume committed Nov 7, 2024
1 parent bd1f501 commit b572503
Show file tree
Hide file tree
Showing 51 changed files with 267 additions and 362 deletions.
3 changes: 2 additions & 1 deletion cyberdrop_dl/clients/download_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from aiohttp import ClientSession

from cyberdrop_dl.clients.errors import DownloadError, InvalidContentTypeError
from cyberdrop_dl.utils.utilities import DEBUG_VAR, FILE_FORMATS, log
from cyberdrop_dl.utils.constants import DEBUG_VAR, FILE_FORMATS
from cyberdrop_dl.utils.logger import log

if TYPE_CHECKING:
from typing import Any, Callable, Coroutine
Expand Down
2 changes: 1 addition & 1 deletion cyberdrop_dl/clients/hash_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from send2trash import send2trash

from cyberdrop_dl.ui.prompts.continue_prompt import enter_to_continue
from cyberdrop_dl.utils.utilities import log
from cyberdrop_dl.utils.logger import log

if TYPE_CHECKING:
from yarl import URL
Expand Down
3 changes: 2 additions & 1 deletion cyberdrop_dl/clients/scraper_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from yarl import URL

from cyberdrop_dl.clients.errors import DDOSGuardError, InvalidContentTypeError
from cyberdrop_dl.utils.utilities import DEBUG_VAR, log
from cyberdrop_dl.utils.constants import DEBUG_VAR
from cyberdrop_dl.utils.logger import log

if TYPE_CHECKING:
from multidict import CIMultiDictProxy
Expand Down
3 changes: 2 additions & 1 deletion cyberdrop_dl/downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from cyberdrop_dl.clients.download_client import is_4xx_client_error
from cyberdrop_dl.clients.errors import CDLBaseError, DownloadError, InsufficientFreeSpaceError, RestrictedFiletypeError
from cyberdrop_dl.managers.real_debrid.errors import RealDebridError
from cyberdrop_dl.utils.utilities import CustomHTTPStatus, log
from cyberdrop_dl.utils.constants import CustomHTTPStatus
from cyberdrop_dl.utils.logger import log

if TYPE_CHECKING:
from cyberdrop_dl.clients.download_client import DownloadClient
Expand Down
133 changes: 59 additions & 74 deletions cyberdrop_dl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,52 @@
import logging
import os
import sys
import time
from pathlib import Path
from time import perf_counter

from rich.console import Console
from rich.logging import RichHandler

from cyberdrop_dl.clients.errors import InvalidYamlError
from cyberdrop_dl.managers.console_manager import print_
from cyberdrop_dl.managers.manager import Manager
from cyberdrop_dl.scraper.scraper import ScrapeMapper
from cyberdrop_dl.ui.ui import program_ui
from cyberdrop_dl.utils.logger import (
log,
log_spacer,
log_with_color,
print_to_console,
)
from cyberdrop_dl.utils.sorting import Sorter
from cyberdrop_dl.utils.utilities import (
DEFAULT_CONSOLE_WIDTH,
check_latest_pypi,
check_partials_and_empty_folders,
log,
log_spacer,
log_with_color,
send_webhook_message,
sent_apprise_notifications,
)

RICH_HANDLER_CONFIG = {"show_time": True, "rich_tracebacks": True, "tracebacks_show_locals": False}

RICH_HANDLER_DEBUG_CONFIG = {
"show_time": True,
"rich_tracebacks": True,
"tracebacks_show_locals": True,
"locals_max_string": DEFAULT_CONSOLE_WIDTH,
"tracebacks_extra_lines": 2,
"locals_max_length": 20,
}

start_time = 0


def startup() -> Manager:
"""Starts the program and returns the manager.
This will also run the UI for the program
After this function returns, the manager will be ready to use and scraping / downloading can begin.
"""
try:
manager = Manager()
manager.startup()

if not manager.args_manager.immediate_download:
program_ui(manager)

return manager
manager = Manager()
manager.startup()

except InvalidYamlError as e:
print_(e.message_rich)
sys.exit(1)
if not manager.args_manager.immediate_download:
program_ui(manager)

except KeyboardInterrupt:
print_("\nExiting...")
sys.exit(0)
return manager


async def runtime(manager: Manager) -> None:
"""Main runtime loop for the program, this will run until all scraping and downloading is complete."""
if manager.args_manager.sort_all_configs:
return

scrape_mapper = ScrapeMapper(manager)

async with asyncio.TaskGroup() as task_group:
Expand All @@ -79,7 +62,7 @@ async def post_runtime(manager: Manager) -> None:
"""Actions to complete after main runtime, and before ui shutdown."""
log_spacer(20)
log_with_color(
f"Running Post-Download Processes For Config: {manager.config_manager.loaded_config}...\n",
f"Running Post-Download Processes For Config: {manager.config_manager.loaded_config}",
"green",
20,
)
Expand All @@ -94,6 +77,7 @@ async def post_runtime(manager: Manager) -> None:
):
sorter = Sorter(manager)
await sorter.sort()

await check_partials_and_empty_folders(manager)

if manager.config_manager.settings_data["Runtime_Options"]["update_last_forum_post"]:
Expand All @@ -104,24 +88,24 @@ def setup_debug_logger(manager: Manager) -> Path | None:
logger_debug = logging.getLogger("cyberdrop_dl_debug")
debug_log_file_path = None
running_in_IDE = os.getenv("PYCHARM_HOSTED") or os.getenv("TERM_PROGRAM") == "vscode"
import cyberdrop_dl.utils.utilities
import cyberdrop_dl.utils.constants as contants

if running_in_IDE or manager.config_manager.settings_data["Runtime_Options"]["log_level"] == -1:
manager.config_manager.settings_data["Runtime_Options"]["log_level"] = 10
cyberdrop_dl.utils.utilities.DEBUG_VAR = True
contants.DEBUG_VAR = True

if running_in_IDE or manager.config_manager.settings_data["Runtime_Options"]["console_log_level"] == -1:
cyberdrop_dl.utils.utilities.CONSOLE_DEBUG_VAR = True
contants.CONSOLE_DEBUG_VAR = True

if cyberdrop_dl.utils.utilities.DEBUG_VAR:
if contants.DEBUG_VAR:
logger_debug.setLevel(manager.config_manager.settings_data["Runtime_Options"]["log_level"])
debug_log_file_path = Path(__file__).parent / "cyberdrop_dl_debug.log"
if running_in_IDE:
debug_log_file_path = Path(__file__).parents[1] / "cyberdrop_dl_debug.log"

rich_file_handler_debug = RichHandler(
**RICH_HANDLER_DEBUG_CONFIG,
console=Console(file=debug_log_file_path.open("w", encoding="utf8"), width=DEFAULT_CONSOLE_WIDTH),
**contants.RICH_HANDLER_DEBUG_CONFIG,
console=Console(file=debug_log_file_path.open("w", encoding="utf8"), width=contants.DEFAULT_CONSOLE_WIDTH),
level=manager.config_manager.settings_data["Runtime_Options"]["log_level"],
)

Expand All @@ -133,9 +117,8 @@ def setup_debug_logger(manager: Manager) -> Path | None:
return debug_log_file_path.resolve() if debug_log_file_path else None


async def switch_config_and_reset_logger(manager: Manager, config_name: str) -> None:
import cyberdrop_dl.managers.console_manager
import cyberdrop_dl.utils.utilities
def setup_logger(manager: Manager, config_name: str) -> None:
import cyberdrop_dl.utils.constants as contants

logger = logging.getLogger("cyberdrop_dl")
if manager.args_manager.all_configs:
Expand All @@ -150,39 +133,37 @@ async def switch_config_and_reset_logger(manager: Manager, config_name: str) ->

logger.setLevel(manager.config_manager.settings_data["Runtime_Options"]["log_level"])

if cyberdrop_dl.utils.utilities.DEBUG_VAR:
if contants.DEBUG_VAR:
manager.config_manager.settings_data["Runtime_Options"]["log_level"] = 10

rich_file_handler = RichHandler(
**RICH_HANDLER_CONFIG,
console=Console(file=manager.path_manager.main_log.open("w", encoding="utf8"), width=DEFAULT_CONSOLE_WIDTH),
**contants.RICH_HANDLER_CONFIG,
console=Console(
file=manager.path_manager.main_log.open("w", encoding="utf8"),
width=contants.DEFAULT_CONSOLE_WIDTH,
),
level=manager.config_manager.settings_data["Runtime_Options"]["log_level"],
)

logger.addHandler(rich_file_handler)

cyberdrop_dl.managers.console_manager.LEVEL = manager.config_manager.settings_data["Runtime_Options"][
"console_log_level"
]
contants.CONSOLE_LEVEL = manager.config_manager.settings_data["Runtime_Options"]["console_log_level"]


async def director(manager: Manager) -> None:
"""Runs the program and handles the UI."""
global start_time
manager.path_manager.startup()
manager.log_manager.startup()
debug_log_file_path = setup_debug_logger(manager)
using_multiple_configs = False

configs_to_run = [manager.config_manager.loaded_config]
if manager.args_manager.all_configs:
configs_to_run = manager.config_manager.get_configs()
using_multiple_configs = True
configs_to_run.sort()

for current_config in configs_to_run:
if using_multiple_configs:
await switch_config_and_reset_logger(manager, current_config)
start_time = manager.start_time
while configs_to_run:
current_config = configs_to_run[0]
setup_logger(manager, current_config)
configs_to_run.pop(0)

log(f"Using Debug Log: {debug_log_file_path if debug_log_file_path else None}", 10)
Expand All @@ -191,17 +172,13 @@ async def director(manager: Manager) -> None:

log_spacer(20)
log("Starting CDL...\n", 20)
if not manager.args_manager.sort_all_configs:
try:
async with manager.live_manager.get_main_live(stop=True):
await runtime(manager)
await post_runtime(manager)
except Exception:
log("\nAn error occurred, please report this to the developer:", 50, exc_info=True)
sys.exit(1)

async with manager.live_manager.get_main_live(stop=True):
await runtime(manager)
await post_runtime(manager)

log_spacer(20)
await manager.progress_manager.print_stats(start_time)
manager.progress_manager.print_stats(start_time)

if not configs_to_run:
log_spacer(20)
Expand All @@ -213,28 +190,36 @@ async def director(manager: Manager) -> None:
log_with_color("Finished downloading. Enjoy :)", "green", 20, show_in_stats=False)

await send_webhook_message(manager)
await sent_apprise_notifications(manager)
start_time = time.perf_counter()
sent_apprise_notifications(manager)
start_time = perf_counter()


def main() -> None:
global start_time
start_time = time.perf_counter()
manager = startup()

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
exit_code = 1
with contextlib.suppress(RuntimeError):
try:
manager = startup()
asyncio.run(director(manager))

exit_code = 0
except InvalidYamlError as e:
print_to_console(e.message_rich)
except KeyboardInterrupt:
print_("\nTrying to Exit...")
with contextlib.suppress(Exception):
print_to_console("\nTrying to Exit...")
asyncio.run(manager.close())
sys.exit(1)
except Exception:
log_with_color(
"An error occurred, please report this to the developer:",
"bold red",
50,
show_in_stats=False,
exc_info=True,
)

loop.close()
sys.exit(0)
sys.exit(exit_code)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion cyberdrop_dl/managers/args_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def startup(self) -> None:
del self.parsed_args["retry_maintenance"]
del self.parsed_args["input_file"]
del self.parsed_args["output_folder"]
del self.parsed_args["appdata_folder"]
del self.parsed_args["appdata_dir"]
del self.parsed_args["config_file"]
del self.parsed_args["log_folder"]
del self.parsed_args["proxy"]
Expand Down
2 changes: 1 addition & 1 deletion cyberdrop_dl/managers/client_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from cyberdrop_dl.clients.errors import DDOSGuardError, DownloadError, ScrapeError
from cyberdrop_dl.clients.scraper_client import ScraperClient
from cyberdrop_dl.managers.leaky import LeakyBucket
from cyberdrop_dl.utils.utilities import CustomHTTPStatus
from cyberdrop_dl.utils.constants import CustomHTTPStatus

if TYPE_CHECKING:
from yarl import URL
Expand Down
Loading

0 comments on commit b572503

Please sign in to comment.