# -*- coding: utf-8 -*- # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. """Extractors for https://cosplayrule34.com/""" from .common import Extractor, Message from .. import text, util BASE_PATTERN = r"(?:https?://)?(?:www\.)?cosplayrule34\.com(?:/[a-z]{2})?" class Cosplayrule34Extractor(Extractor): """Base class for cosplayrule34 extractors""" category = "cosplayrule34" root = "https://cosplayrule34.com" directory_fmt = ("{category}", "{title}") filename_fmt = "{id}_{num}.{extension}" archive_fmt = "{id}_{num}" request_interval = (0.5, 1.5) def items(self): data = {"_extractor": Cosplayrule34PostExtractor} for post_url in self.posts(): yield Message.Queue, post_url, data def posts(self): _find_post_urls = text.re( r"""onclick="location\.href='(/post/\d+)'""").findall _find_next = text.re( r'class="d-none next"[^>]*href="([^"]+)').search url = self.root + self.groups[0] while True: page = self.request(url, notfound=self.subcategory).text post_url = None for post_url in util.unique(_find_post_urls(page)): yield self.root + post_url if post_url is None: break match = _find_next(page) if match is None: break url = self.root + text.unescape(match[1]) class Cosplayrule34PostExtractor(Cosplayrule34Extractor): """Extractor for individual cosplayrule34 posts""" subcategory = "post" pattern = BASE_PATTERN + r"/post/(\d+)" example = "https://cosplayrule34.com/post/12345" def items(self): data, urls = self._extract_post(self.groups[0]) data["count"] = len(urls) yield Message.Directory, "", data for data["num"], url in enumerate(urls, 1): yield Message.Url, url, text.nameext_from_url(url, data) def _extract_post(self, post_id): url = f"{self.root}/post/{post_id}" page = self.request(url, notfound=self.subcategory).text if title := text.extr(page, 'const title = "', '";'): title = util.json_loads(f'"{title}"') else: title = text.unescape(text.extr(page, '