Skip to content

Bulk asynchronous downloader for multiple file hosts

add simpcity crawler

Jules-WinnfieldXa643016e62c5
diff --git a/cyberdrop_dl/scraper/crawlers/simpcity_crawler.py b/cyberdrop_dl/scraper/crawlers/simpcity_crawler.py
new file mode 100644
index 000000000..89e4df15e
--- /dev/null
+++ b/cyberdrop_dl/scraper/crawlers/simpcity_crawler.py
@@ -0,0 +1,258 @@
+from __future__ import annotations
+
+import re
+from typing import TYPE_CHECKING
+
+from aiolimiter import AsyncLimiter
+from bs4 import Tag
+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 get_filename_and_ext, error_handling_wrapper, log
+
+if TYPE_CHECKING:
+ from cyberdrop_dl.managers.manager import Manager
+
+
+class SimpCityCrawler(Crawler):
+ def __init__(self, manager: Manager):
+ super().__init__(manager, "simpcity", "SimpCity")
+ self.logged_in = False
+ 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 not self.logged_in:
+ login_url = URL("https://simpcity.su/login")
+ session_cookie = self.manager.config_manager.authentication_data['Forums']['simpcity_xf_user_cookie']
+ username = self.manager.config_manager.authentication_data['Forums']['simpcity_username']
+ password = self.manager.config_manager.authentication_data['Forums']['simpcity_password']
+ wait_time = 5
+
+ await self.forum_login(login_url, session_cookie, username, password, wait_time)
+
+ await self.forum(scrape_item)
+
+ await self.scraping_progress.remove_task(task_id)
+
+ @error_handling_wrapper
+ async def forum(self, scrape_item: ScrapeItem) -> None:
+ """Scrapes an album"""
+ continue_scraping = True
+ if "post-" in str(scrape_item.url):
+ url_parts = str(scrape_item.url).rsplit("post-", 1)
+ thread_url = URL(url_parts[0].rstrip("#"))
+ post_number = int(url_parts[-1].strip("/")) if len(url_parts) == 2 else 0
+ else:
+ thread_url = scrape_item.url
+ post_number = 0
+ current_post_number = 0
+
+ while True:
+ async with self.request_limiter:
+ soup = await self.client.get_BS4(self.domain, thread_url)
+
+ title_block = soup.select_one("h1[class=p-title-value]")
+ for elem in title_block.find_all("a"):
+ elem.decompose()
+ title = title_block.text.replace("\n", "").strip() + f" ({thread_url.host})"
+
+ new_scrape_item = ScrapeItem(thread_url, scrape_item.parent_title)
+ await new_scrape_item.add_to_parent_title(title)
+
+ posts = soup.select("div[class*=message-main]")
+ for post in posts:
+ current_post_number = int(post.select_one("li[class=u-concealed] a").get("href").split('/')[-1].split('post-')[-1])
+ if post_number > current_post_number:
+ continue
+
+ for elem in post.find_all("blockquote"):
+ elem.decompose()
+ post_content = post.select_one("div[class=bbWrapper]")
+ await self.post(new_scrape_item, post_content, current_post_number)
+
+ if self.manager.config_manager.settings_data['Download_Options']['scrape_single_forum_post']:
+ continue_scraping = False
+ break
+
+ next_page = soup.select_one("a[class=pageNav-jump--next]")
+ if next_page and continue_scraping:
+ thread_url = next_page.get("href")
+ if thread_url:
+ if thread_url.startswith("/"):
+ thread_url = "https://simpcity.su" + thread_url
+ thread_url = URL(thread_url)
+ continue
+ else:
+ break
+ post_string = f"post-{current_post_number}"
+ if "page-" in scrape_item.url.raw_name or "post-" in scrape_item.url.raw_name:
+ last_post_url = scrape_item.url.parent / post_string
+ else:
+ last_post_url = scrape_item.url / post_string
+ await self.manager.file_manager.write_last_post_log(last_post_url)
+
+ @error_handling_wrapper
+ async def post(self, scrape_item: ScrapeItem, post_content: Tag, post_number: int) -> None:
+ """Scrapes a post"""
+ if self.manager.config_manager.settings_data['Download_Options']['separate_posts']:
+ scrape_item = ScrapeItem(scrape_item.url, scrape_item.parent_title)
+ await scrape_item.add_to_parent_title("post-" + str(post_number))
+
+ await self.links(scrape_item, post_content)
+ await self.images(scrape_item, post_content)
+ await self.videos(scrape_item, post_content)
+ await self.embeds(scrape_item, post_content)
+ await self.attachments(scrape_item, post_content)
+
+ @error_handling_wrapper
+ async def links(self, scrape_item: ScrapeItem, post_content: Tag) -> None:
+ """Scrapes links from a post"""
+ links = post_content.select("a")
+ for link_obj in links:
+ test_for_img = link_obj.select_one("img")
+ if test_for_img is not None:
+ continue
+
+ link = link_obj.get("href")
+ if not link:
+ continue
+
+ link = link.replace(".th.", ".").replace(".md.", ".")
+
+ if link.endswith("/"):
+ link = link[:-1]
+
+ if link.startswith("//"):
+ link = "https:" + link
+ elif link.startswith("/"):
+ link = "https://simpcity.su" + link
+ link = URL(link)
+
+ if "simpcity" not in link.host:
+ new_scrape_item = ScrapeItem(link, scrape_item.parent_title)
+ await self.handle_external_links(new_scrape_item)
+ elif "attachments" in link.parts:
+ await self.handle_internal_links(link, scrape_item)
+ else:
+ await log(f"Unknown link type: {link}")
+ continue
+
+ @error_handling_wrapper
+ async def images(self, scrape_item: ScrapeItem, post_content: Tag) -> None:
+ """Scrapes images from a post"""
+ images = post_content.select("img[class*=bbImage]")
+ for image in images:
+ link = image.get("src")
+ if not link:
+ continue
+
+ link = link.replace(".th.", ".").replace(".md.", ".")
+ if link.endswith("/"):
+ link = link[:-1]
+
+ if link.startswith("//"):
+ link = "https:" + link
+ elif link.startswith("/"):
+ link = "https://simpcity.su" + link
+ link = URL(link)
+
+ if "simpcity" not in link.host:
+ new_scrape_item = ScrapeItem(link, scrape_item.parent_title)
+ await self.handle_external_links(new_scrape_item)
+ elif "attachments" in link.parts:
+ await self.handle_internal_links(link, scrape_item)
+ else:
+ await log(f"Unknown image type: {link}")
+ continue
+
+ @error_handling_wrapper
+ async def videos(self, scrape_item: ScrapeItem, post_content: Tag) -> None:
+ """Scrapes videos from a post"""
+ videos = post_content.select("video source")
+ videos.extend(post_content.select("iframe[class=saint-iframe]"))
+
+ for video in videos:
+ link = video.get("src")
+ if not link:
+ continue
+
+ if link.endswith("/"):
+ link = link[:-1]
+
+ if link.startswith("//"):
+ link = "https:" + link
+
+ link = URL(link)
+ new_scrape_item = ScrapeItem(link, scrape_item.parent_title)
+ await self.handle_external_links(new_scrape_item)
+
+ @error_handling_wrapper
+ async def embeds(self, scrape_item: ScrapeItem, post_content: Tag) -> None:
+ """Scrapes embeds from a post"""
+ embeds = post_content.select("span[data-s9e-mediaembed-iframe]")
+ for embed in embeds:
+ data = embed.get("data-s9e-mediaembed-iframe")
+ if not data:
+ continue
+
+ data = data.replace("\/\/", "https://www.")
+ data = data.replace("\\", "")
+
+ embed = re.search(r"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)", data)
+ if not embed:
+ embed = re.search(r"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\/[-a-zA-Z0-9@:%._\+~#=]*\/[-a-zA-Z0-9@:?&%._\+~#=]*", data)
+
+ if embed:
+ link = embed.group(0).replace("www.", "")
+ if link.endswith("/"):
+ link = link[:-1]
+ link = URL(link)
+ new_scrape_item = ScrapeItem(link, scrape_item.parent_title)
+ await self.handle_external_links(new_scrape_item)
+
+ @error_handling_wrapper
+ async def attachments(self, scrape_item: ScrapeItem, post_content: Tag) -> None:
+ """Scrapes attachments from a post"""
+ attachment_block = post_content.select_one("section[class=message-attachments]")
+ if not attachment_block:
+ return
+
+ attachments = attachment_block.select("a")
+ for attachment in attachments:
+ link = attachment.get("href")
+ if not link:
+ continue
+
+ if link.endswith("/"):
+ link = link[:-1]
+
+ if link.startswith("//"):
+ link = "https:" + link
+ elif link.startswith("/"):
+ link = "https://simpcity.su" + link
+ link = URL(link)
+
+ if "simpcity" not in link.host:
+ new_scrape_item = ScrapeItem(link, scrape_item.parent_title)
+ await self.handle_external_links(new_scrape_item)
+ elif "attachments" in link.parts:
+ await self.handle_internal_links(link, scrape_item)
+ else:
+ await log(f"Unknown image type: {link}")
+ continue
+
+ """~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
+
+ @error_handling_wrapper
+ async def handle_internal_links(self, link: URL, scrape_item: ScrapeItem) -> None:
+ """Handles internal links"""
+ filename, ext = await get_filename_and_ext(link.name, True)
+ new_scrape_item = ScrapeItem(link, scrape_item.parent_title, True)
+ await new_scrape_item.add_to_parent_title("Attachments")
+ await self.handle_file(link, new_scrape_item, filename, ext)
diff --git a/cyberdrop_dl/scraper/scraper.py b/cyberdrop_dl/scraper/scraper.py
index 5a14e3c91..3632400d7 100644
--- a/cyberdrop_dl/scraper/scraper.py
+++ b/cyberdrop_dl/scraper/scraper.py
@@ -19,7 +19,9 @@ if TYPE_CHECKING:
class ScrapeMapper:
"""This class maps links to their respective handlers, or JDownloader if they are unsupported"""
def __init__(self, manager: Manager):
- self.mapping = {"bunkr": self.bunkr, "coomer": self.coomer, "cyberdrop": self.cyberdrop,
+ # "cyberdrop": self.cyberdrop
+
+ self.mapping = {"bunkr": self.bunkr, "coomer": self.coomer,
"cyberfile": self.cyberfile, "e-hentai": self.ehentai, "erome": self.erome,
"fapello": self.fapello, "gofile": self.gofile, "imgbox": self.imgbox,
"imgur": self.imgur, "img.kiwi": self.imgwiki, "jpg.church": self.jpgchurch,
@@ -27,7 +29,7 @@ class ScrapeMapper:
"jpg.pet": self.jpgchurch, "jpeg.pet": self.jpgchurch, "jpg1.su": self.jpgchurch,
"jpg2.su": self.jpgchurch, "jpg3.su": self.jpgchurch, "kemono": self.kemono,
"pimpandhost": self.pimpandhost, "pixeldrain": self.pixeldrain, "postimg": self.postimg,
- "reddit": self.reddit, "redgifs": self.redgifs, "saint": self.saint}
+ "reddit": self.reddit, "redgifs": self.redgifs, "saint": self.saint, "simpcity": self.simpcity}
self.sharex_domains = ["img.kiwi", "jpg.church", "jpg.homes", "jpg.fish", "jpg.fishing", "jpg.pet",
"jpeg.pet", "jpg1.su", "jpg2.su", "jpg3.su"]
self.existing_crawlers = {}
@@ -138,6 +140,11 @@ class ScrapeMapper:
from cyberdrop_dl.scraper.crawlers.saint_crawler import SaintCrawler
self.existing_crawlers['saint'] = SaintCrawler(self.manager)
+ async def simpcity(self) -> None:
+ """Creates a SimpCity Crawler instance"""
+ from cyberdrop_dl.scraper.crawlers.simpcity_crawler import SimpCityCrawler
+ self.existing_crawlers['simpcity'] = SimpCityCrawler(self.manager)
+
"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
async def regex_links(self, line: str) -> List: