# -*- coding: utf-8 -*- # Copyright 2018-2023 Mike Fährmann # # 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://rule34.paheal.net/""" from .common import Extractor, Message from .. import text class PahealExtractor(Extractor): """Base class for paheal extractors""" basecategory = "shimmie2" category = "paheal" filename_fmt = "{category}_{id}_{md5}.{extension}" archive_fmt = "{id}" root = "https://rule34.paheal.net" def items(self): self.cookies.set( "ui-tnc-agreed", "true", domain="rule34.paheal.net") data = self.get_metadata() for post in self.get_posts(): url = post["file_url"] for key in ("id", "width", "height"): post[key] = text.parse_int(post[key]) post["tags"] = text.unquote(post["tags"]) post.update(data) yield Message.Directory, post yield Message.Url, url, post def get_metadata(self): """Return general metadata""" return {} def get_posts(self): """Return an iterable containing data of all relevant posts""" def _extract_post(self, post_id): url = "{}/post/view/{}".format(self.root, post_id) extr = text.extract_from(self.request(url).text) post = { "id" : post_id, "tags" : extr(": ", "<"), "md5" : extr("/_thumbs/", "/"), "file_url": (extr("id='main_image' src='", "'") or extr("Source Link<", ""), "href='", "'")), } dimensions, size, ext = extr("Info", "<").split(" // ") post["size"] = text.parse_bytes(size[:-1]) post["width"], _, height = dimensions.partition("x") post["height"], _, duration = height.partition(", ") post["duration"] = text.parse_float(duration[:-1]) post["filename"] = "{} - {}".format(post_id, post["tags"]) post["extension"] = ext return post class PahealTagExtractor(PahealExtractor): """Extractor for images from rule34.paheal.net by search-tags""" subcategory = "tag" directory_fmt = ("{category}", "{search_tags}") pattern = (r"(?:https?://)?(?:rule34|rule63|cosplay)\.paheal\.net" r"/post/list/([^/?#]+)") example = "https://rule34.paheal.net/post/list/TAG/1" per_page = 70 def __init__(self, match): PahealExtractor.__init__(self, match) self.tags = text.unquote(match.group(1)) def _init(self): if self.config("metadata"): self._extract_data = self._extract_data_ex def get_metadata(self): return {"search_tags": self.tags} def get_posts(self): pnum = 1 while True: url = "{}/post/list/{}/{}".format(self.root, self.tags, pnum) page = self.request(url).text pos = page.find("id='image-list'") for post in text.extract_iter( page, "