| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License version 2 as |
| 5 |
# published by the Free Software Foundation. |
| 6 |
|
| 7 |
"""Extractors for https://cosplayrule34.com/""" |
| 8 |
|
| 9 |
from .common import Extractor, Message |
| 10 |
from .. import text, util |
| 11 |
|
| 12 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?cosplayrule34\.com(?:/[a-z]{2})?" |
| 13 |
|
| 14 |
|
| 15 |
class Cosplayrule34Extractor(Extractor): |
| 16 |
"""Base class for cosplayrule34 extractors""" |
| 17 |
category = "cosplayrule34" |
| 18 |
root = "https://cosplayrule34.com" |
| 19 |
directory_fmt = ("{category}", "{title}") |
| 20 |
filename_fmt = "{id}_{num}.{extension}" |
| 21 |
archive_fmt = "{id}_{num}" |
| 22 |
request_interval = (0.5, 1.5) |
| 23 |
|
| 24 |
def items(self): |
| 25 |
data = {"_extractor": Cosplayrule34PostExtractor} |
| 26 |
for post_url in self.posts(): |
| 27 |
yield Message.Queue, post_url, data |
| 28 |
|
| 29 |
def posts(self): |
| 30 |
_find_post_urls = text.re( |
| 31 |
r"""onclick="location\.href='(/post/\d+)'""").findall |
| 32 |
_find_next = text.re( |
| 33 |
r'class="d-none next"[^>]*href="([^"]+)').search |
| 34 |
|
| 35 |
url = self.root + self.groups[0] |
| 36 |
while True: |
| 37 |
page = self.request(url, notfound=self.subcategory).text |
| 38 |
|
| 39 |
post_url = None |
| 40 |
for post_url in util.unique(_find_post_urls(page)): |
| 41 |
yield self.root + post_url |
| 42 |
|
| 43 |
if post_url is None: |
| 44 |
break |
| 45 |
match = _find_next(page) |
| 46 |
if match is None: |
| 47 |
break |
| 48 |
url = self.root + text.unescape(match[1]) |
| 49 |
|
| 50 |
|
| 51 |
class Cosplayrule34PostExtractor(Cosplayrule34Extractor): |
| 52 |
"""Extractor for individual cosplayrule34 posts""" |
| 53 |
subcategory = "post" |
| 54 |
pattern = BASE_PATTERN + r"/post/(\d+)" |
| 55 |
example = "https://cosplayrule34.com/post/12345" |
| 56 |
|
| 57 |
def items(self): |
| 58 |
data, urls = self._extract_post(self.groups[0]) |
| 59 |
data["count"] = len(urls) |
| 60 |
|
| 61 |
yield Message.Directory, "", data |
| 62 |
for data["num"], url in enumerate(urls, 1): |
| 63 |
yield Message.Url, url, text.nameext_from_url(url, data) |
| 64 |
|
| 65 |
def _extract_post(self, post_id): |
| 66 |
url = f"{self.root}/post/{post_id}" |
| 67 |
page = self.request(url, notfound=self.subcategory).text |
| 68 |
|
| 69 |
if title := text.extr(page, 'const title = "', '";'): |
| 70 |
title = util.json_loads(f'"{title}"') |
| 71 |
else: |
| 72 |
title = text.unescape(text.extr(page, '<h1 class="h6">', '<')) |
| 73 |
|
| 74 |
description = text.unescape(text.extr( |
| 75 |
page, '<meta name="description" content="', '"')) |
| 76 |
suffix = " - " + post_id |
| 77 |
if description.endswith(suffix): |
| 78 |
description = description[:-len(suffix)] |
| 79 |
|
| 80 |
tag_region = text.extr( |
| 81 |
page, |
| 82 |
'<div style="padding-top:10px;padding-bottom:10px">', |
| 83 |
'<div class="container mt-4" id="comments"', |
| 84 |
) or page |
| 85 |
|
| 86 |
tags = {"model": [], "cosplay": [], "fandom": []} |
| 87 |
_find_tags = text.re( |
| 88 |
r'href="/(model|cosplay|fandom)/[^"]+">([^<]+)</a>').findall |
| 89 |
for tag_type, tag_name in util.unique(_find_tags(tag_region)): |
| 90 |
tags[tag_type].append(text.unescape(tag_name)) |
| 91 |
|
| 92 |
data = { |
| 93 |
"id" : text.parse_int(post_id), |
| 94 |
"title" : title, |
| 95 |
"description": description, |
| 96 |
**tags, |
| 97 |
"tags" : tags["model"] + tags["cosplay"] + tags["fandom"], |
| 98 |
"album_id" : text.extr(page, 'const albumId = "', '"'), |
| 99 |
"owner_id" : text.extr(page, 'const ownerId = "', '"'), |
| 100 |
"download" : text.parse_int( |
| 101 |
text.extr(page, "const download = ", ";"), 1), |
| 102 |
"download_id": text.parse_int( |
| 103 |
text.extr(page, "const downloadId = ", ";")), |
| 104 |
"counter" : text.parse_int( |
| 105 |
text.extr(page, "let counter = ", ";"), 1), |
| 106 |
"count" : text.parse_int( |
| 107 |
text.extr(page, "const totalPhotos = ", ";")), |
| 108 |
} |
| 109 |
|
| 110 |
self._find_image_urls = text.re( |
| 111 |
r'href="(https://cosplayrule34\.com/images/a/1280/[^"]+)"').findall |
| 112 |
urls = list(util.unique(self._find_image_urls(page))) |
| 113 |
if data["count"] > len(urls): |
| 114 |
urls.extend(self._load_more_photos(data, len(urls))) |
| 115 |
urls = list(util.unique(urls)) |
| 116 |
urls = self._recover_sequence(urls, data["count"]) |
| 117 |
elif not data["count"]: |
| 118 |
data["count"] = len(urls) |
| 119 |
|
| 120 |
return data, urls |
| 121 |
|
| 122 |
def _load_more_photos(self, data, offset): |
| 123 |
url = self.root + "/cms/load-more-photos.php" |
| 124 |
counter = data["counter"] |
| 125 |
photos_per_load = 10 |
| 126 |
remaining = data["count"] - offset |
| 127 |
urls = [] |
| 128 |
|
| 129 |
while remaining > 0: |
| 130 |
response = self.request_json( |
| 131 |
url, |
| 132 |
method="POST", |
| 133 |
json={ |
| 134 |
"album_id" : data["album_id"], |
| 135 |
"owner_id" : data["owner_id"], |
| 136 |
"download" : data["download"], |
| 137 |
"download_id": data["download_id"], |
| 138 |
"offset" : offset, |
| 139 |
"limit" : min(photos_per_load, remaining), |
| 140 |
"title" : data["title"], |
| 141 |
"counter" : counter, |
| 142 |
}, |
| 143 |
fatal=False, |
| 144 |
) |
| 145 |
|
| 146 |
photos = response.get("photos") or () |
| 147 |
if not photos: |
| 148 |
break |
| 149 |
|
| 150 |
for photo in photos: |
| 151 |
urls.extend(self._find_image_urls(photo["html"])) |
| 152 |
counter = photo.get("counter", counter) |
| 153 |
|
| 154 |
count = len(photos) |
| 155 |
offset += count |
| 156 |
remaining -= count |
| 157 |
|
| 158 |
return urls |
| 159 |
|
| 160 |
def _recover_sequence(self, urls, count): |
| 161 |
if count <= len(urls): |
| 162 |
return urls |
| 163 |
|
| 164 |
prefix = extension = None |
| 165 |
numbers = {} |
| 166 |
|
| 167 |
for url in urls: |
| 168 |
base, _, filename = url.rpartition("/") |
| 169 |
stem, dot, ext = filename.partition(".") |
| 170 |
num = text.parse_int(stem, -1) |
| 171 |
if not dot or num < 1: |
| 172 |
return urls |
| 173 |
|
| 174 |
current_prefix = base + "/" |
| 175 |
current_ext = "." + ext |
| 176 |
|
| 177 |
if prefix is None: |
| 178 |
prefix = current_prefix |
| 179 |
extension = current_ext |
| 180 |
elif prefix != current_prefix or extension != current_ext: |
| 181 |
return urls |
| 182 |
|
| 183 |
numbers[num] = url |
| 184 |
|
| 185 |
recovered = [ |
| 186 |
numbers.get(num) or f"{prefix}{num}{extension}" |
| 187 |
for num in range(1, count + 1) |
| 188 |
] |
| 189 |
return recovered |
| 190 |
|
| 191 |
|
| 192 |
class Cosplayrule34ListingExtractor(Cosplayrule34Extractor): |
| 193 |
"""Base class for cosplayrule34 post listings""" |
| 194 |
subcategory = "listing" |
| 195 |
pattern = (BASE_PATTERN + r"(/" |
| 196 |
r"(model|cosplay|fandom|category|search)" |
| 197 |
r"/[^/?#]+(?:/?\?[^#]+)?)") |
| 198 |
example = "https://cosplayrule34.com/model/MODEL" |
| 199 |
|
| 200 |
def __init__(self, match): |
| 201 |
self.subcategory = match[2] |
| 202 |
Cosplayrule34Extractor.__init__(self, match) |
| 203 |
|
| 204 |
|
| 205 |
class Cosplayrule34TopExtractor(Cosplayrule34Extractor): |
| 206 |
"""Extractor for cosplayrule34 top pages""" |
| 207 |
subcategory = "top" |
| 208 |
pattern = BASE_PATTERN + r"(/top(?:/?\?[^#]+)?)$" |
| 209 |
example = "https://cosplayrule34.com/top" |
| 210 |
|
| 211 |
|
| 212 |
class Cosplayrule34PostsExtractor(Cosplayrule34Extractor): |
| 213 |
"""Extractor for cosplayrule34 front-page posts""" |
| 214 |
subcategory = "posts" |
| 215 |
pattern = BASE_PATTERN + r"(/?(?:\?[^#]+)?)$" |
| 216 |
example = "https://cosplayrule34.com/" |