Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: switch to CSV for every log

NTFSvolumefd29e46d9d59
diff --git a/cyberdrop_dl/managers/live_manager.py b/cyberdrop_dl/managers/live_manager.py
index d5c7b94e3..90c91f443 100644
--- a/cyberdrop_dl/managers/live_manager.py
+++ b/cyberdrop_dl/managers/live_manager.py
@@ -1,4 +1,3 @@
-import traceback
from contextlib import asynccontextmanager
from rich.live import Live
diff --git a/cyberdrop_dl/managers/log_manager.py b/cyberdrop_dl/managers/log_manager.py
index 96d78d3df..04f92d072 100644
--- a/cyberdrop_dl/managers/log_manager.py
+++ b/cyberdrop_dl/managers/log_manager.py
@@ -1,14 +1,16 @@
from typing import TYPE_CHECKING, Optional
import aiofiles
+import csv
if TYPE_CHECKING:
from pathlib import Path
from yarl import URL
-
from cyberdrop_dl.managers.manager import Manager
+CSV_DELIMITER = ','
+
class LogManager:
def __init__(self, manager: 'Manager'):
self.manager = manager
@@ -20,70 +22,72 @@ class LogManager:
def startup(self) -> None:
"""Startup process for the file manager"""
- self.main_log.unlink(missing_ok=True)
- self.main_log.touch(exist_ok=True)
- self.last_post_log.unlink(missing_ok=True)
- self.last_post_log.touch(exist_ok=True)
- self.unsupported_urls_log.unlink(missing_ok=True)
- self.unsupported_urls_log.touch(exist_ok=True)
- self.download_error_log.unlink(missing_ok=True)
- self.download_error_log.touch(exist_ok=True)
- self.scrape_error_log.unlink(missing_ok=True)
- self.scrape_error_log.touch(exist_ok=True)
+ pass
+
+ async def write_to_csv (self, file: Path, **kwargs):
+ "Write to the specified csv file. kwargs are columns for the CSV "
+
+ write_headers = not file.is_file()
+
+ async with aiofiles.open(file, 'a', encoding="utf8") as csv_file:
+ writer = csv.DictWriter(csv_file, fieldnames=kwargs.keys(), delimiter=CSV_DELIMITER, quoting=csv.QUOTE_MINIMAL)
+ if write_headers:
+ writer.writeheader()
+ writer.writerows(**kwargs)
async def write_last_post_log(self, url: 'URL') -> None:
"""Writes to the last post log"""
- async with aiofiles.open(self.last_post_log, 'a') as f:
- await f.write(f"{url}\n")
+ await self.write_to_csv(self.last_post_log, url=url)
- async def write_unsupported_urls_log(self, url: 'URL', parent_url: Optional['URL'] = None ) -> None:
+ async def write_unsupported_urls_log(self, url: 'URL', origin: Optional['URL'] = None ) -> None:
"""Writes to the unsupported urls log"""
- async with aiofiles.open(self.unsupported_urls_log, 'a') as f:
- await f.write(f"{url} ; {parent_url}\n")
+ await self.write_to_csv(self.unsupported_urls_log, url=url, origin=origin)
- async def write_download_error_log(self, url: 'URL', error_message: str) -> None:
+ async def write_download_error_log(self, url: 'URL', error_message: str, origin: Optional['URL'] = None ) -> None:
"""Writes to the download error log"""
- async with aiofiles.open(self.download_error_log, 'a') as f:
- await f.write(f"{url},{error_message}\n")
+ await self.write_to_csv(self.download_error_log, url=url, error=error_message, origin=origin)
- async def write_scrape_error_log(self, url: 'URL', error_message: str) -> None:
+ async def write_scrape_error_log(self, url: 'URL', error_message: str, origin: Optional['URL'] = None) -> None:
"""Writes to the scrape error log"""
- async with aiofiles.open(self.scrape_error_log, 'a') as f:
- await f.write(f"{url},{error_message}\n")
+ await self.write_to_csv(self.scrape_error_log, url=url, error=error_message, origin=origin)
async def update_last_forum_post(self) -> None:
"""Updates the last forum post"""
+
input_file = self.manager.path_manager.input_file
- # we need to touch the file just in case, purge_tree deletes it
- if not input_file.exists():
+ if not input_file.is_file() or not self.last_post_log.is_file():
return
- base_urls = []
+
+ current_urls, current_base_urls, new_urls, new_base_urls = [], [], [], []
async with aiofiles.open(input_file, 'r') as f:
- current_urls = await f.readlines()
-
- for url in current_urls:
- if "http" not in url:
- continue
- if "post-" in url:
- url = url.rsplit("/", 1)[0]
- if not url.endswith("\n"):
- url += "\n"
- base_urls.append(url)
-
- last_post_file = self.last_post_log
- if not last_post_file.exists():
- return
-
- async with aiofiles.open(last_post_file, 'r') as f:
- new_urls = await f.readlines()
-
- for url in new_urls:
- url_temp = url.rsplit("/", 1)[0] + "\n"
- if url_temp in base_urls:
- base_urls.remove(url_temp)
- base_urls.append(url)
- # we need to touch the file just in case, purge_tree deletes it
- input_file.touch(exist_ok=True)
- async with aiofiles.open(input_file, 'w') as f:
- await f.writelines(base_urls)
+ async for line in f:
+ url = base_url = line.strip()
+
+ if "https" in url and "post-" in url:
+ base_url = url.rsplit("/", 1)[0]
+
+ # only keep 1 url of the same thread
+ if base_url not in current_base_urls:
+ current_urls.append(url)
+ current_base_urls.append(base_url)
+
+ async with aiofiles.open(self.last_post_log, 'r') as f:
+ async for line in f:
+ url = base_url = line.strip()
+
+ if "https" in url and "post-" in url:
+ base_url = url.rsplit("/", 1)[0]
+
+ # only keep 1 url of the same thread
+ if base_url not in current_base_urls:
+ new_urls.append(url)
+ new_base_urls.append(base_url)
+
+ for url, base in zip(new_urls, new_base_urls):
+ if base in current_base_urls:
+ index = current_base_urls.index(base)
+ current_urls[index] = url
+
+ async with aiofiles.open(input_file, 'w', newline = '\n') as f:
+ await f.writelines(current_urls)
diff --git a/cyberdrop_dl/utils/args/config_definitions.py b/cyberdrop_dl/utils/args/config_definitions.py
index 069ba379c..db137c7ce 100644
--- a/cyberdrop_dl/utils/args/config_definitions.py
+++ b/cyberdrop_dl/utils/args/config_definitions.py
@@ -76,8 +76,8 @@ settings: Dict = {
"log_folder": str(APP_STORAGE / "Configs" / "{config}" / "Logs"),
"webhook_url": "",
"main_log_filename": "downloader.log",
- "last_forum_post_filename": "Last_Scraped_Forum_Posts.txt",
- "unsupported_urls_filename": "Unsupported_URLs.txt",
+ "last_forum_post_filename": "Last_Scraped_Forum_Posts.csv",
+ "unsupported_urls_filename": "Unsupported_URLs.csv",
"download_error_urls_filename": "Download_Error_URLs.csv",
"scrape_error_urls_filename": "Scrape_Error_URLs.csv",
"rotate_logs": False
diff --git a/docs/reference/configuration-options/settings.md b/docs/reference/configuration-options/settings.md
index fae8de701..98a24e2cf 100644
--- a/docs/reference/configuration-options/settings.md
+++ b/docs/reference/configuration-options/settings.md
@@ -170,10 +170,10 @@ Files that will be rotated:
| option | default_filename |
|------------------------------|-------------------------------|
| download_error_urls_filename | Download_Error_URLs.csv |
-| last_forum_post_filename | Last_Scraped_Forum_Posts.txt |
+| last_forum_post_filename | Last_Scraped_Forum_Posts.csv |
| main_log_filename | downloader.log |
| scrape_error_urls_filename | Scrape_Error_URLs.csv |
-| unsupported_urls_filename | Unsupported_URLs.txt |
+| unsupported_urls_filename | Unsupported_URLs.csv |
</details>