Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: simplify css iget API (#9)

NTFSvolumea74a14933521
diff --git a/cyberdrop_dl/crawlers/discourse/_discourse.py b/cyberdrop_dl/crawlers/discourse/_discourse.py
index b318f0c5e..7bf42c5a4 100644
--- a/cyberdrop_dl/crawlers/discourse/_discourse.py
+++ b/cyberdrop_dl/crawlers/discourse/_discourse.py
@@ -135,8 +135,8 @@ class DiscourseCrawler(MessageBoardCrawler, is_generic=True):
def extract_links(self, post: AvailablePost) -> Iterable[AbsoluteHttpURL]:
def iter_links() -> Iterable[AbsoluteHttpURL]:
soup = BeautifulSoup(post.content_html, "html.parser")
- images = css.iget(soup, *css.images)
- links = css.iget(soup, *css.links)
+ images = css.iselect(soup, *css.images)
+ links = css.iselect(soup, *css.links)
external_links = (ref.url for ref in post.link_counts)
for link_str in unique(itertools.chain(external_links, images, links)):
try:
diff --git a/cyberdrop_dl/crawlers/wordpress/_wordpress.py b/cyberdrop_dl/crawlers/wordpress/_wordpress.py
index 083156e81..dc8ee5120 100644
--- a/cyberdrop_dl/crawlers/wordpress/_wordpress.py
+++ b/cyberdrop_dl/crawlers/wordpress/_wordpress.py
@@ -336,8 +336,8 @@ def _get_original_quality_link(link: str) -> str:
def _iter_links(html: HTML, use_regex: bool) -> Iterable[str]:
soup = BeautifulSoup(html, "html.parser")
- images = css.iget(soup, *css.images)
- iframes = css.iget(soup, *css.iframes)
+ images = css.iselect(soup, *css.images)
+ iframes = css.iselect(soup, *css.iframes)
if use_regex:
regex = (match.group() for match in re.finditer(_HTTP_URL_REGEX, html))
return itertools.chain(images, iframes, regex)
diff --git a/cyberdrop_dl/utils/css.py b/cyberdrop_dl/utils/css.py
index 636acbfa8..1a00ca991 100644
--- a/cyberdrop_dl/utils/css.py
+++ b/cyberdrop_dl/utils/css.py
@@ -105,9 +105,24 @@ def select_one_get_attr_or_none(tag: Tag, selector: str, attribute: str) -> str
return get_attr_or_none(inner_tag, attribute)
-def iselect(tag: Tag, selector: str) -> Generator[Tag]:
+@overload
+def iselect(tag: Tag, selector: str) -> Generator[Tag]: ...
+
+
+@overload
+def iselect(tag: Tag, selector: str, attribute: str) -> Generator[str]: ...
+
+
+def iselect(tag: Tag, selector: str, attribute: str | None = None) -> Generator[Tag] | Generator[str]:
"""Same as `tag.select(selector)`, but it returns a generator instead of a list."""
- yield from bs4.css.CSS(tag).iselect(selector)
+ tags = bs4.css.CSS(tag).iselect(selector)
+ if not attribute:
+ yield from tags
+
+ else:
+ for inner_tag in tags:
+ if attr := get_attr_or_none(inner_tag, attribute):
+ yield attr
def _parse_srcset(srcset: str) -> str:
@@ -115,12 +130,6 @@ def _parse_srcset(srcset: str) -> str:
return [src.split(" ")[0] for src in srcset.split(", ")][-1]
-def iget(tag: Tag, selector: str, attribute: str) -> Generator[str]:
- for inner_tag in iselect(tag, selector):
- if link := get_attr_or_none(inner_tag, attribute):
- yield link
-
-
def decompose(tag: Tag, selector: str) -> None:
for inner_tag in tag.select(selector):
inner_tag.decompose()