Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
Add ImgKiwi Crawler
Jules-WinnfieldX4cca0bd42caa
diff --git a/cyberdrop_dl/scraper/crawlers/imgkiwi_crawler.py b/cyberdrop_dl/scraper/crawlers/imgkiwi_crawler.pynew file mode 100644index 000000000..c58f2ef15--- /dev/null+++ b/cyberdrop_dl/scraper/crawlers/imgkiwi_crawler.py@@ -0,0 +1,97 @@+from __future__ import annotations++import calendar+import datetime+import re+from typing import TYPE_CHECKING++from aiolimiter import AsyncLimiter+from yarl import URL++from cyberdrop_dl.scraper.crawler import Crawler+from cyberdrop_dl.utils.dataclasses.url_objects import ScrapeItem+from cyberdrop_dl.utils.utilities import error_handling_wrapper, get_filename_and_ext++if TYPE_CHECKING:+ from cyberdrop_dl.managers.manager import Manager+++class ImgKiwiCrawler(Crawler):+ def __init__(self, manager: Manager):+ super().__init__(manager, "imgkiwi", "ImgKiwi")+ self.request_limiter = AsyncLimiter(10, 1)++ """~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""++ async def fetch(self, scrape_item: ScrapeItem) -> None:+ """Determines where to send the scrape item based on the url"""+ task_id = await self.scraping_progress.add_task(scrape_item.url)++ if self.check_direct_link(scrape_item.url):+ await self.handle_direct_link(scrape_item)+ elif "album" in scrape_item.url.parts:+ await self.album(scrape_item)+ else:+ await self.image(scrape_item)++ await self.scraping_progress.remove_task(task_id)++ @error_handling_wrapper+ async def album(self, scrape_item: ScrapeItem) -> None:+ """Scrapes an album"""+ async with self.request_limiter:+ soup = await self.client.get_BS4(self.domain, scrape_item.url)++ title = soup.select_one("a[data-text=album-name]").get_text()+ link_next = URL(soup.select_one("a[id=list-most-recent-link]").get("href"))++ while True:+ async with self.request_limiter:+ soup = await self.client.get_BS4(self.domain, link_next)+ links = soup.select("a[href*=image]")+ for link in links:+ link = URL(link.get('href'))+ new_scrape_item = ScrapeItem(link, parent_title=scrape_item.parent_title, part_of_album=True)+ await new_scrape_item.add_to_parent_title(title)+ await self.scraper_queue.put(new_scrape_item)++ link_next = soup.select_one('a[data-pagination=next]')+ if link_next is not None:+ link_next = link_next.get('href')+ if link_next is not None:+ link_next = URL(link_next)+ else:+ break+ else:+ break++ @error_handling_wrapper+ async def image(self, scrape_item: ScrapeItem) -> None:+ """Scrapes an image"""+ async with self.request_limiter:+ soup = await self.client.get_BS4(self.domain, scrape_item.url)++ link = URL(soup.select_one("div[id=image-viewer-container] img").get('src'))+ date = soup.select_one("p[class*=description-meta] span").get("title")+ date = await self.parse_datetime(date)+ scrape_item.possible_datetime = date++ filename, ext = await get_filename_and_ext(link.name)+ await self.handle_file(link, scrape_item, filename, ext)++ async def handle_direct_link(self, scrape_item: ScrapeItem) -> None:+ """Handles a direct link"""+ filename, ext = await get_filename_and_ext(scrape_item.url.name)+ await self.handle_file(scrape_item.url, scrape_item, filename, ext)++ """~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""++ async def parse_datetime(self, date: str) -> int:+ """Parses a datetime string into a unix timestamp"""+ date = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")+ return calendar.timegm(date.timetuple())++ async def check_direct_link(self, url: URL) -> bool:+ """Determines if the url is a direct link or not"""+ mapping_direct = [r'img.kiwi/images/']+ return any(re.search(domain, str(url)) for domain in mapping_direct)