| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2021-2026 Mike Fährmann |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
|
| 9 |
"""Extractors for https://rule34.us/""" |
| 10 |
|
| 11 |
from .booru import BooruExtractor |
| 12 |
from .. import text |
| 13 |
import collections |
| 14 |
|
| 15 |
|
| 16 |
class Rule34usExtractor(BooruExtractor): |
| 17 |
category = "rule34us" |
| 18 |
root = "https://rule34.us" |
| 19 |
per_page = 42 |
| 20 |
|
| 21 |
def _init(self): |
| 22 |
self._find_tags = text.re( |
| 23 |
r'<li class="([^-"]+)-tag"[^>]*><a href="[^;"]+;q=([^"]+)').findall |
| 24 |
|
| 25 |
def _parse_post(self, post_id): |
| 26 |
url = f"{self.root}/index.php?r=posts/view&id={post_id}" |
| 27 |
page = self.request(url).text |
| 28 |
extr = text.extract_from(page) |
| 29 |
|
| 30 |
post = { |
| 31 |
"id" : post_id, |
| 32 |
"tags" : text.unescape(extr( |
| 33 |
'name="keywords" content="', '"').rstrip(", ")), |
| 34 |
"uploader": text.extract(extr('Added by: ', '</li>'), ">", "<")[0], |
| 35 |
"score" : text.extract(extr('Score: ', '> - <'), ">", "<")[0], |
| 36 |
"width" : extr('Size: ', 'w'), |
| 37 |
"height" : extr(' x ', 'h'), |
| 38 |
"file_url": extr('<source src="', '"') or extr('<img src="', '"'), |
| 39 |
} |
| 40 |
|
| 41 |
url = post["file_url"] |
| 42 |
if "//video-cdn1." in url: |
| 43 |
post["file_url"] = url.replace("//video-cdn1.", "//video.") |
| 44 |
post["_fallback"] = (url,) |
| 45 |
post["md5"] = url[url.rfind("/")+1:url.rfind(".")] |
| 46 |
|
| 47 |
tags = collections.defaultdict(list) |
| 48 |
for tag_type, tag_name in self._find_tags(page): |
| 49 |
tags[tag_type].append(text.unquote(tag_name)) |
| 50 |
for key, value in tags.items(): |
| 51 |
post["tags_" + key] = " ".join(value) |
| 52 |
|
| 53 |
return post |
| 54 |
|
| 55 |
|
| 56 |
class Rule34usTagExtractor(Rule34usExtractor): |
| 57 |
subcategory = "tag" |
| 58 |
directory_fmt = ("{category}", "{search_tags}") |
| 59 |
archive_fmt = "t_{search_tags}_{id}" |
| 60 |
pattern = r"(?:https?://)?rule34\.us/index\.php\?r=posts/index&q=([^&#]*)" |
| 61 |
example = "https://rule34.us/index.php?r=posts/index&q=TAG" |
| 62 |
|
| 63 |
def posts(self): |
| 64 |
self.kwdict["search_tags"] = tags = text.unquote( |
| 65 |
self.groups[0].replace("+", " ")) |
| 66 |
|
| 67 |
url = self.root + "/index.php" |
| 68 |
params = { |
| 69 |
"r" : "posts/index", |
| 70 |
"q" : tags, |
| 71 |
"page": self.page_start, |
| 72 |
} |
| 73 |
|
| 74 |
while True: |
| 75 |
page = self.request(url, params=params).text |
| 76 |
|
| 77 |
cnt = 0 |
| 78 |
for post_id in text.extract_iter(page, '><a id="', '"'): |
| 79 |
yield self._parse_post(post_id) |
| 80 |
cnt += 1 |
| 81 |
|
| 82 |
if cnt < self.per_page: |
| 83 |
break |
| 84 |
|
| 85 |
if "page" in params: |
| 86 |
del params["page"] |
| 87 |
params["q"] = f"{tags} id:<{post_id}" |
| 88 |
|
| 89 |
|
| 90 |
class Rule34usPostExtractor(Rule34usExtractor): |
| 91 |
subcategory = "post" |
| 92 |
archive_fmt = "{id}" |
| 93 |
pattern = r"(?:https?://)?rule34\.us/index\.php\?r=posts/view&id=(\d+)" |
| 94 |
example = "https://rule34.us/index.php?r=posts/view&id=12345" |
| 95 |
|
| 96 |
def posts(self): |
| 97 |
return (self._parse_post(self.groups[0]),) |