Skip to content

Bulk asynchronous downloader for multiple file hosts

Async functions should not call open or synchronous os methods (#468)

Arkadiy Illarionov4cc6d7f40e2e
diff --git a/cyberdrop_dl/base_functions/base_functions.py b/cyberdrop_dl/base_functions/base_functions.py
index 8876dcda2..067030709 100644
--- a/cyberdrop_dl/base_functions/base_functions.py
+++ b/cyberdrop_dl/base_functions/base_functions.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import asyncio
import logging
import os
import re
@@ -44,7 +45,8 @@ MAX_FILENAME_LENGTH = 95
async def clear() -> None:
"""Clears the terminal screen"""
- os.system('cls' if os.name == 'nt' else 'clear')
+ clear_screen_proc = await asyncio.create_subprocess_shell('cls' if os.name == 'nt' else 'clear')
+ await clear_screen_proc.wait()
def log(text: str, quiet: bool = False, style: str = "") -> None:
diff --git a/cyberdrop_dl/main.py b/cyberdrop_dl/main.py
index dcae83f93..254902d32 100644
--- a/cyberdrop_dl/main.py
+++ b/cyberdrop_dl/main.py
@@ -6,9 +6,10 @@ import re
from pathlib import Path
from typing import Dict, List
+import aiofiles
from yarl import URL
-from cyberdrop_dl.base_functions.base_functions import clear, log, purge_dir, ErrorFileWriter
+from cyberdrop_dl.base_functions.base_functions import ErrorFileWriter, clear, log, purge_dir
from cyberdrop_dl.base_functions.config_manager import document_args, run_args
from cyberdrop_dl.base_functions.config_schema import config_default
from cyberdrop_dl.base_functions.sorting_functions import Sorter
@@ -202,8 +203,8 @@ async def consolidate_links(args: Dict, links: List) -> List:
"""We consolidate links from command line and from URLs.txt into a singular list"""
links = list(map(URL, links))
if args["Files"]["input_file"].is_file():
- with open(args["Files"]["input_file"], "r", encoding="utf8") as f:
- links += await regex_links([line.rstrip() for line in f])
+ async with aiofiles.open(args["Files"]["input_file"], "r", encoding="utf8") as f:
+ links += await regex_links([line.rstrip() async for line in f])
links = list(filter(None, links))
if not links:
@@ -258,11 +259,10 @@ async def director(args: Dict, links: List) -> None:
await asyncio.sleep(5)
await clear()
- if args['Progress_Options']['hide_new_progress']:
- if not await Forums.is_empty():
+ if not await Forums.is_empty():
+ if args['Progress_Options']['hide_new_progress']:
await old_download_forums(args, Forums, SQL_Helper, client)
- else:
- if not await Forums.is_empty():
+ else:
download_director = DownloadDirector(args, Forums, SQL_Helper, client, error_writer)
await download_director.start()