Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: update xenforo tests to work on classes

NTFSvolumee1bc42355d02
diff --git a/cyberdrop_dl/aio.py b/cyberdrop_dl/aio.py
index da9706fff..cc3f3de9d 100644
--- a/cyberdrop_dl/aio.py
+++ b/cyberdrop_dl/aio.py
@@ -76,6 +76,7 @@ class EagerTaskGroup(asyncio.TaskGroup):
return await super().__aexit__(et, exc, tb)
finally:
self.done.set()
+ et = exc = tb = None # prevent ref cycles for GC
def create_lazy_task(
self,
diff --git a/cyberdrop_dl/crawlers/_forum.py b/cyberdrop_dl/crawlers/_forum.py
index ce96b7594..915dfd3fb 100644
--- a/cyberdrop_dl/crawlers/_forum.py
+++ b/cyberdrop_dl/crawlers/_forum.py
@@ -462,34 +462,40 @@ class HTMLMessageBoardCrawler(MessageBoardCrawler, is_abc=True):
if stats:
self.log.info(f"post #{post.id} {stats = }")
- def _external_links(self, post: ForumPostProtocol) -> Iterable[str]:
- selector = self.SELECTORS.posts.links
+ @classmethod
+ def _external_links(cls, post: ForumPostProtocol) -> Iterable[str]:
+ selector = cls.SELECTORS.posts.links
links = css.iselect(post.content, selector.element)
- valid_links = (link for link in links if not self.is_username_or_attachment(link))
+ valid_links = (link for link in links if not cls.is_username_or_attachment(link))
return iter_links(valid_links, selector.attribute)
- def _images(self, post: ForumPostProtocol) -> Iterable[str]:
- selector = self.SELECTORS.posts.a_tag_w_image if self.IGNORE_EMBEDED_IMAGES_SRC else self.SELECTORS.posts.images
+ @classmethod
+ def _images(cls, post: ForumPostProtocol) -> Iterable[str]:
+ selector = cls.SELECTORS.posts.a_tag_w_image if cls.IGNORE_EMBEDED_IMAGES_SRC else cls.SELECTORS.posts.images
images = css.iselect(post.content, selector.element)
return iter_links(images, selector.attribute)
- def _videos(self, post: ForumPostProtocol) -> Iterable[str]:
- selector = self.SELECTORS.posts.videos
+ @classmethod
+ def _videos(cls, post: ForumPostProtocol) -> Iterable[str]:
+ selector = cls.SELECTORS.posts.videos
videos = css.iselect(post.content, selector.element)
return iter_links(videos, selector.attribute)
- def _attachments(self, post: ForumPostProtocol) -> Iterable[str]:
- selector = self.SELECTORS.posts.attachments
+ @classmethod
+ def _attachments(cls, post: ForumPostProtocol) -> Iterable[str]:
+ selector = cls.SELECTORS.posts.attachments
attachments = css.iselect(post.article, selector.element)
return iter_links(attachments, selector.attribute)
- def _embeds(self, post: ForumPostProtocol) -> Iterable[str]:
- selector = self.SELECTORS.posts.embeds
+ @classmethod
+ def _embeds(cls, post: ForumPostProtocol) -> Iterable[str]:
+ selector = cls.SELECTORS.posts.embeds
embeds = css.iselect(post.content, selector.element)
return iter_links(embeds, selector.attribute)
- def _lazy_load_embeds(self, post: ForumPostProtocol) -> Iterable[str]:
- selector = self.SELECTORS.posts.lazy_load_embeds
+ @classmethod
+ def _lazy_load_embeds(cls, post: ForumPostProtocol) -> Iterable[str]:
+ selector = cls.SELECTORS.posts.lazy_load_embeds
for lazy_media in css.iselect(post.content, selector.element):
yield extr_text(css.attr(lazy_media, selector.attribute), "loadMedia(this, '", "')")
@@ -548,12 +554,13 @@ class HTMLMessageBoardCrawler(MessageBoardCrawler, is_abc=True):
await super().handle_internal_link(scrape_item, link)
- def is_username_or_attachment(self, link_obj: Tag) -> bool:
+ @classmethod
+ def is_username_or_attachment(cls, link_obj: Tag) -> bool:
if link_obj.select_one(".username"):
return True
try:
- if link_str := css.attr(link_obj, self.SELECTORS.posts.links.element):
- return self.is_attachment(link_str)
+ if link_str := css.attr(link_obj, cls.SELECTORS.posts.links.element):
+ return cls.is_attachment(link_str)
except Exception: # noqa: BLE001
pass
return False
diff --git a/tests/crawlers/test_crawlers.py b/tests/crawlers/test_crawlers.py
index bc311a0db..9ba085c18 100644
--- a/tests/crawlers/test_crawlers.py
+++ b/tests/crawlers/test_crawlers.py
@@ -109,7 +109,7 @@ async def test_crawler(running_manager: Manager, test_case: CrawlerTestCase) ->
None,
)
assert cls, f"{test_case.domain} is not a valid crawler domain. Test case is invalid"
- crawler = cls(running_manager)
+ crawler = scrape_mapper._factory[cls]
await crawler.__async_init__()
item = ScrapeItem.from_url(crawler.parse_url(test_case.url))
item.download_folder = running_manager.config.download_folder
diff --git a/tests/crawlers/test_xenforo.py b/tests/crawlers/test_xenforo.py
index bdbc6c934..95f404f39 100644
--- a/tests/crawlers/test_xenforo.py
+++ b/tests/crawlers/test_xenforo.py
@@ -9,6 +9,7 @@ from cyberdrop_dl.config.appdata import AppData, AppDirs
from cyberdrop_dl.crawlers import _forum
from cyberdrop_dl.crawlers import xenforo as crawlers
from cyberdrop_dl.crawlers.xenforo import xenforo
+from cyberdrop_dl.crawlers.xenforo.celebforum import CelebForumCrawler
from cyberdrop_dl.exceptions import ScrapeError
from cyberdrop_dl.manager import Manager
from cyberdrop_dl.url_objects import AbsoluteHttpURL, ScrapeItem
@@ -21,8 +22,7 @@ def _item(url: str) -> ScrapeItem:
manager = Manager(appdata=AppData.from_dirs(AppDirs.from_path(Path(__file__).parent / "xenforo_appdata")))
scrape_item = _item("https://xenforo.com/community")
-crawler_instances = {crawler: crawler(manager) for crawler in crawlers.XF_CRAWLERS}
-TEST_CRAWLER = crawler_instances[crawlers.CelebForumCrawler]
+TEST_CRAWLER = CelebForumCrawler
def _html(string: str) -> str:
@@ -39,7 +39,7 @@ def _post(
message_body: str = "",
message_attachments: str = "",
id: int = 12345, # noqa: A002
- crawler: xenforo.XenforoCrawler | None = None,
+ crawler: type[xenforo.XenforoCrawler] | None = None,
) -> _forum.ForumPost:
crawler = crawler or TEST_CRAWLER
html = _html(POST_TEMPLATE.format(id=id, message_body=message_body, message_attachments=message_attachments))
@@ -393,7 +393,7 @@ def test_lazy_load_embeds() -> None:
@pytest.mark.parametrize(
- ("cls", "post_content", "expected_result"),
+ ("crawler", "post_content", "expected_result"),
[
(
crawlers.AllPornComixCrawler,
@@ -450,8 +450,7 @@ def test_lazy_load_embeds() -> None:
),
],
)
-def test_post_images(cls: type[xenforo.XenforoCrawler], post_content: str, expected_result: list[str]) -> None:
- crawler = crawler_instances[cls]
+def test_post_images(crawler: type[xenforo.XenforoCrawler], post_content: str, expected_result: list[str]) -> None:
post = _post(post_content, crawler=crawler)
results = list(crawler._images(post))
count, expected_count = len(results), len(expected_result)
@@ -461,7 +460,7 @@ def test_post_images(cls: type[xenforo.XenforoCrawler], post_content: str, expec
def test_embeds_can_extract_google_drive_links() -> None:
# https://github.com/Cyberdrop-DL/cyberdrop-dl/issues/775
- crawler = crawler_instances[crawlers.SimpCityCrawler]
+ crawler = crawlers.SimpCityCrawler
content = """
<div itemprop="text">
<div class="bbWrapper">
@@ -541,7 +540,7 @@ def test_post_smg_extract_attachments() -> None:
</li>
</ul>
"""
- crawler = crawler_instances[crawlers.SocialMediaGirlsCrawler]
+ crawler = crawlers.SocialMediaGirlsCrawler
post = _post(message_attachments=attachments, crawler=crawler)
expected_result = [
"https://smgmedia2.socialmediagirls.com/forum/2022/04/33E3EDFF-B0ED-4AE3-8D5D-4D2BC6D7EFD4_3526918.jpeg",
@@ -562,7 +561,7 @@ def test_post_celebforum_should_use_href_for_images() -> None:
src="https://celebforum.to/data/attachments/321/321141-2d66067afbf5cd3d546479380c08929d.jpg" style=""
title="JC6HUqrju9" width="150" /></a>
"""
- crawler = crawler_instances[crawlers.CelebForumCrawler]
+ crawler = crawlers.CelebForumCrawler
post = _post(content, crawler=crawler)
expected_result = ["https://celebforum.to/attachments/jc6huqrju9-jpg.321620/"]
result = list(crawler._images(post))