Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: remove cookies sites options from config (#85)

NTFSvolumef0c09d25b246
diff --git a/cyberdrop_dl/cli/__init__.py b/cyberdrop_dl/cli/__init__.py
index c0f1efd10..557318113 100644
--- a/cyberdrop_dl/cli/__init__.py
+++ b/cyberdrop_dl/cli/__init__.py
@@ -118,7 +118,7 @@ def parse_args(args: Sequence[str] | None = None) -> ParsedArgs:
def show_supported_sites() -> NoReturn:
from rich import print
- from cyberdrop_dl.utils.markdown import get_crawlers_info_as_rich_table
+ from cyberdrop_dl.supported_sites import get_crawlers_info_as_rich_table
table = get_crawlers_info_as_rich_table()
print(table)
diff --git a/cyberdrop_dl/config/config_model.py b/cyberdrop_dl/config/config_model.py
index 3d960767d..a03ffeedf 100755
--- a/cyberdrop_dl/config/config_model.py
+++ b/cyberdrop_dl/config/config_model.py
@@ -4,7 +4,7 @@ from datetime import date, datetime, timedelta
from logging import DEBUG
from pathlib import Path
-from pydantic import BaseModel, ByteSize, Field, NonNegativeInt, field_serializer, field_validator
+from pydantic import BaseModel, ByteSize, Field, NonNegativeInt, field_validator
from cyberdrop_dl import constants
from cyberdrop_dl.constants import DEFAULT_APP_STORAGE, DEFAULT_DOWNLOAD_STORAGE, Browser, Hashing
@@ -20,7 +20,6 @@ from cyberdrop_dl.models.types import (
PathOrNone,
)
from cyberdrop_dl.models.validators import falsy_as, to_timedelta
-from cyberdrop_dl.supported_domains import SUPPORTED_SITES_DOMAINS
from cyberdrop_dl.utils.strings import validate_format_string
from cyberdrop_dl.utils.utilities import purge_dir_tree
@@ -260,27 +259,6 @@ class Sorting(BaseModel):
class BrowserCookies(BaseModel):
auto_import: bool = False
browser: Browser | None = Browser.firefox
- sites: list[NonEmptyStr] = SUPPORTED_SITES_DOMAINS
-
- def model_post_init(self, *_) -> None:
- if self.auto_import and not self.browser:
- raise ValueError("You need to provide a browser for auto_import to work")
-
- @field_validator("sites", mode="before")
- @classmethod
- def handle_list(cls, values: list[str]) -> list[str]:
- values = falsy_as(values, [])
- if values == ALL_SUPPORTED_SITES:
- return SUPPORTED_SITES_DOMAINS
- if isinstance(values, list):
- return sorted(str(value).lower() for value in values)
- return values
-
- @field_serializer("sites", when_used="json-unless-none")
- def use_placeholder(self, values: list[str]) -> list[str]:
- if set(values) == set(SUPPORTED_SITES_DOMAINS):
- return ALL_SUPPORTED_SITES
- return values
class DupeCleanup(BaseModel):
diff --git a/cyberdrop_dl/supported_domains.py b/cyberdrop_dl/supported_domains.py
deleted file mode 100644
index 42989e022..000000000
--- a/cyberdrop_dl/supported_domains.py
+++ /dev/null
@@ -1,27 +0,0 @@
-from __future__ import annotations
-
-from typing import TYPE_CHECKING
-
-from cyberdrop_dl.crawlers import FORUM_CRAWLERS, WEBSITE_CRAWLERS
-
-if TYPE_CHECKING:
- from collections.abc import Iterable
-
- from cyberdrop_dl.crawlers import Crawler
-
-
-def get_supported_sites_from(crawlers: Iterable[type[Crawler]]) -> dict[str, str]:
- support_sites_dict = {}
- for crawler in crawlers:
- if crawler.IS_GENERIC:
- continue
- site = crawler.DOMAIN or crawler.PRIMARY_URL.host
- support_sites_dict[site] = crawler.PRIMARY_URL.host
-
- return {key: support_sites_dict[key] for key in sorted(support_sites_dict)}
-
-
-SUPPORTED_FORUMS = get_supported_sites_from(FORUM_CRAWLERS)
-SUPPORTED_WEBSITES = get_supported_sites_from(WEBSITE_CRAWLERS)
-SUPPORTED_SITES = SUPPORTED_FORUMS | SUPPORTED_WEBSITES
-SUPPORTED_SITES_DOMAINS = sorted(SUPPORTED_SITES.values())
diff --git a/cyberdrop_dl/supported_sites.py b/cyberdrop_dl/supported_sites.py
new file mode 100644
index 000000000..f680494d1
--- /dev/null
+++ b/cyberdrop_dl/supported_sites.py
@@ -0,0 +1,83 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from rich.table import Table
+from rich.text import Text
+
+if TYPE_CHECKING:
+ from collections.abc import Generator, Iterable
+
+ from cyberdrop_dl.crawlers.crawler import CrawlerInfo
+
+_COLUMNS = ("site", "primary URL", "supported domains", "supported paths")
+
+
+def get_crawlers_info_as_rich_table() -> Table:
+ table = Table(
+ title=Text.assemble("cyberdrop-dl supported sites", style="green"),
+ show_lines=True,
+ highlight=True,
+ )
+ for column in _COLUMNS[0:3]:
+ table.add_column(column, no_wrap=True)
+
+ for row_values in _gen_crawlers_info_rows():
+ table.add_row(*row_values[:3])
+
+ return table
+
+
+def _gen_crawlers_info_rows() -> Generator[tuple[str, ...]]:
+ from cyberdrop_dl.crawlers.crawler import Registry
+
+ Registry.import_all()
+
+ crawlers = Registry.generic | Registry.concrete
+ infos = (crawler.INFO for crawler in crawlers)
+ for info in sorted(infos, key=lambda x: x.site.casefold()):
+ yield _get_row_values(info)
+
+
+def get_crawlers_info_as_markdown_table() -> str:
+ from py_markdown_table.markdown_table import markdown_table
+
+ rows = list(_make_html_rows())
+ table = markdown_table(rows).set_params("markdown", padding_width=10, padding_weight="centerright", quote=False)
+ return table.get_markdown()
+
+
+def _make_html_rows() -> Generator[dict[str, str]]:
+ for row in _gen_crawlers_info_rows():
+ values = (value.replace("\n", "<br>") for value in row)
+ yield dict(zip(_COLUMNS, values, strict=True))
+
+
+def _get_row_values(crawler_info: CrawlerInfo) -> tuple[str, ...]:
+ supported_paths, notes = _get_supported_paths_and_notes(crawler_info)
+ if notes:
+ supported_paths = f"{supported_paths}\n\n**NOTES**\n{notes}"
+ supported_domains = "\n".join(crawler_info.supported_domains)
+ return crawler_info.site, str(crawler_info.primary_url).rstrip("/"), supported_domains, supported_paths
+
+
+def _get_supported_paths_and_notes(crawler_info: CrawlerInfo) -> tuple[str, str]:
+ supported_paths: list[str] = []
+ notes: list[str] = []
+
+ def join(paths: Iterable[str], *, quote_char: str = "`") -> str:
+ return "\n".join(f" - {quote_char}{p}{quote_char}" for p in paths)
+
+ for name, paths in crawler_info.supported_paths.items():
+ if isinstance(paths, str):
+ paths = (paths,)
+
+ if "direct link" in name.casefold() and paths == ("",):
+ supported_paths.append("Direct Links")
+
+ elif "*note*" in name.casefold():
+ notes.append(join(paths, quote_char=""))
+ else:
+ supported_paths.append(f"{name}: \n{join(paths)}")
+
+ return "\n".join(supported_paths), "\n".join(notes)
diff --git a/cyberdrop_dl/utils/markdown.py b/cyberdrop_dl/utils/markdown.py
deleted file mode 100644
index 7abb69603..000000000
--- a/cyberdrop_dl/utils/markdown.py
+++ /dev/null
@@ -1,96 +0,0 @@
-from __future__ import annotations
-
-from typing import TYPE_CHECKING, TypeAlias
-
-from rich.table import Table
-
-if TYPE_CHECKING:
- from collections.abc import Generator, Sequence
-
- from cyberdrop_dl.crawlers.crawler import Crawler, CrawlerInfo
-
-
-SUPPORTED_SITES_URL = "https://script-ware.gitbook.io/cyberdrop-dl/reference/supported-websites"
-MarkdownRowDict: TypeAlias = dict[str, str]
-
-
-def get_crawlers_info_as_rich_table() -> Table:
- caption = f"Visit {SUPPORTED_SITES_URL} for a details about supported paths"
- table = Table(title="Cyberdrop-DL Supported Sites", caption=caption)
- columns, rows = _get_crawlers_info_cols_and_rows()
-
- for column in columns[0:3]:
- table.add_column(column, no_wrap=True)
-
- for row_values in rows:
- table.add_row(*row_values[0:3])
-
- return table
-
-
-def get_crawlers_info_as_markdown_table() -> str:
- from py_markdown_table.markdown_table import markdown_table
-
- rows = _make_html_rows()
- table = markdown_table(rows).set_params("markdown", padding_width=10, padding_weight="centerright", quote=False)
- return table.get_markdown()
-
-
-def _make_html_rows() -> list[MarkdownRowDict]:
- columns, rows = _get_crawlers_info_cols_and_rows()
- html_rows: list[MarkdownRowDict] = []
- for row in rows:
- html_row_values = [r.replace("\n", "<br>") for r in row]
- html_row_dict = MarkdownRowDict(zip(columns, html_row_values, strict=True))
- html_rows.append(html_row_dict)
- return html_rows
-
-
-def _get_crawlers_info_cols_and_rows() -> tuple[list[str], Generator[tuple[str, ...]]]:
- from cyberdrop_dl.scraper.scrape_mapper import get_unique_crawlers
-
- def make_colunm(field_name: str) -> str:
- return field_name.replace("_", " ").title().replace("Url", "URL")
-
- crawlers = get_unique_crawlers()
- columns = [make_colunm(f) for f in crawlers[0].INFO._fields]
- return columns, _gen_crawlers_info_rows(crawlers)
-
-
-def _gen_crawlers_info_rows(crawlers: Sequence[Crawler]) -> Generator[tuple[str, ...]]:
- info_gen = (crawler.INFO for crawler in crawlers)
- for info in sorted(info_gen, key=lambda x: x.site.casefold()):
- yield _get_row_values(info)
-
-
-def _join_supported_paths(paths: tuple[str, ...] | list[str], quote_char: str = "`") -> str:
- joined = "\n".join([f" - {quote_char}{p}{quote_char}" for p in paths])
- return f"{joined}\n"
-
-
-def _get_supported_paths_and_notes(crawler_info: CrawlerInfo) -> tuple[str, str]:
- supported_paths: str = ""
- notes: str = ""
-
- for name, paths in crawler_info.supported_paths.items():
- if "direct link" in name.casefold() and (not paths or paths == ("",)):
- supported_paths += "Direct Links\n"
- continue
- if isinstance(paths, str):
- paths = [paths]
- if "*note*" in name.casefold():
- notes += _join_supported_paths(paths, "")
- continue
-
- supported_paths += f"{name}: \n{_join_supported_paths(paths)}"
-
- return supported_paths, notes
-
-
-def _get_row_values(crawler_info: CrawlerInfo) -> tuple[str, ...]:
- supported_paths, notes = _get_supported_paths_and_notes(crawler_info)
- if notes:
- supported_paths = f"{supported_paths}\n\n**NOTES**\n{notes}"
- supported_domains = "\n".join(crawler_info.supported_domains)
- row_values = crawler_info.site, str(crawler_info.primary_url).removesuffix("/"), supported_domains, supported_paths
- return row_values
diff --git a/scripts/tools/update_docs.py b/scripts/tools/update_docs.py
index e235aa4fd..162bc2a04 100644
--- a/scripts/tools/update_docs.py
+++ b/scripts/tools/update_docs.py
@@ -2,7 +2,7 @@ from pathlib import Path
from cyberdrop_dl import __version__
from cyberdrop_dl.cli import CustomHelpFormatter, make_parser
-from cyberdrop_dl.utils.markdown import get_crawlers_info_as_markdown_table
+from cyberdrop_dl.supported_sites import get_crawlers_info_as_markdown_table
REPO_ROOT = Path(__file__).parents[2]
CLI_ARGUMENTS_MD = REPO_ROOT / "docs/reference/cli-arguments.md"
diff --git a/tests/test_cli.py b/tests/test_cli.py
index ff6922554..d1f4e5b69 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -11,7 +11,7 @@ from cyberdrop_dl.main import run
"command, text",
[
("--help", "Bulk asynchronous downloader for multiple file hosts"),
- ("--show-supported-sites", "for a details about supported paths"),
+ ("--show-supported-sites", "cyberdrop-dl supported sites"),
],
)
def test_command_by_console_output(tmp_cwd: Path, capsys: pytest.CaptureFixture[str], command: str, text: str) -> None:
diff --git a/tests/test_supported_sites.py b/tests/test_supported_sites.py
new file mode 100644
index 000000000..e7102d4b7
--- /dev/null
+++ b/tests/test_supported_sites.py
@@ -0,0 +1,6 @@
+from cyberdrop_dl.supported_sites import get_crawlers_info_as_rich_table
+
+
+def test_rich_table() -> None:
+ table = get_crawlers_info_as_rich_table()
+ assert len(table.rows) >= 169