| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Generic extractors for *reactor sites""" |
| 10 |
|
| 11 |
from .common import BaseExtractor, Message |
| 12 |
from .. import text, util |
| 13 |
import urllib.parse |
| 14 |
|
| 15 |
|
| 16 |
class ReactorExtractor(BaseExtractor): |
| 17 |
"""Base class for *reactor.cc extractors""" |
| 18 |
basecategory = "reactor" |
| 19 |
filename_fmt = "{post_id}_{num:>02}{title[:100]:?_//}.{extension}" |
| 20 |
archive_fmt = "{post_id}_{num}" |
| 21 |
request_interval = (3.0, 6.0) |
| 22 |
|
| 23 |
def __init__(self, match): |
| 24 |
BaseExtractor.__init__(self, match) |
| 25 |
|
| 26 |
url = text.ensure_http_scheme(match[0], "http://") |
| 27 |
pos = url.index("/", 10) |
| 28 |
self.root = url[:pos] |
| 29 |
self.path = url[pos:] |
| 30 |
|
| 31 |
if self.category == "reactor": |
| 32 |
|
| 33 |
netloc = urllib.parse.urlsplit(self.root).netloc |
| 34 |
self.category = netloc.rpartition(".")[0] |
| 35 |
|
| 36 |
def _init(self): |
| 37 |
self.gif = self.config("gif", False) |
| 38 |
|
| 39 |
def items(self): |
| 40 |
data = self.metadata() |
| 41 |
yield Message.Directory, "", data |
| 42 |
for post in self.posts(): |
| 43 |
for image in self._parse_post(post): |
| 44 |
url = image["url"] |
| 45 |
image.update(data) |
| 46 |
yield Message.Url, url, text.nameext_from_url(url, image) |
| 47 |
|
| 48 |
def metadata(self): |
| 49 |
"""Collect metadata for extractor-job""" |
| 50 |
return {} |
| 51 |
|
| 52 |
def posts(self): |
| 53 |
"""Return all relevant post-objects""" |
| 54 |
return self._pagination(self.root + self.path) |
| 55 |
|
| 56 |
def _pagination(self, url): |
| 57 |
while True: |
| 58 |
response = self.request(url) |
| 59 |
if response.history: |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
cnt_old = response.history[0].url.count("/") |
| 65 |
cnt_new = response.url.count("/") |
| 66 |
if cnt_old == 5 and cnt_new == 4: |
| 67 |
return |
| 68 |
page = response.text |
| 69 |
|
| 70 |
yield from text.extract_iter( |
| 71 |
page, '<div class="uhead">', '<div class="ufoot">') |
| 72 |
|
| 73 |
try: |
| 74 |
pos = page.index("class='next'") |
| 75 |
pos = page.rindex("class='current'", 0, pos) |
| 76 |
url = self.root + text.extract(page, "href='", "'", pos)[0] |
| 77 |
except (ValueError, TypeError): |
| 78 |
return |
| 79 |
|
| 80 |
def _parse_post(self, post): |
| 81 |
post, _, script = post.partition('<script type="application/ld+json">') |
| 82 |
if not script: |
| 83 |
return |
| 84 |
images = text.extract_iter(post, '<div class="image">', '</div>') |
| 85 |
script = script[:script.index("</")].strip() |
| 86 |
|
| 87 |
try: |
| 88 |
data = util.json_loads(script) |
| 89 |
except ValueError: |
| 90 |
try: |
| 91 |
|
| 92 |
mapping = dict.fromkeys(range(32)) |
| 93 |
script = script.translate(mapping).replace("\\", "\\\\") |
| 94 |
data = util.json_loads(script) |
| 95 |
except ValueError as exc: |
| 96 |
self.log.warning("Unable to parse JSON data: %s", exc) |
| 97 |
return |
| 98 |
|
| 99 |
num = 0 |
| 100 |
date = self.parse_datetime_iso(data["datePublished"]) |
| 101 |
user = data["author"]["name"] |
| 102 |
description = text.unescape(data["description"]) |
| 103 |
title, _, tags = text.unescape(data["headline"]).partition(" / ") |
| 104 |
post_id = text.parse_int( |
| 105 |
data["mainEntityOfPage"]["@id"].rpartition("/")[2]) |
| 106 |
|
| 107 |
if not tags: |
| 108 |
title, tags = tags, title |
| 109 |
tags = tags.split(" :: ") |
| 110 |
tags.sort() |
| 111 |
|
| 112 |
for image in images: |
| 113 |
url = text.extr(image, ' src="', '"') |
| 114 |
if not url: |
| 115 |
continue |
| 116 |
if url.startswith("//"): |
| 117 |
url = "http:" + url |
| 118 |
width = text.extr(image, ' width="', '"') |
| 119 |
height = text.extr(image, ' height="', '"') |
| 120 |
image_id = url.rpartition("-")[2].partition(".")[0] |
| 121 |
num += 1 |
| 122 |
|
| 123 |
if image.startswith("<iframe "): |
| 124 |
url = "ytdl:" + text.unescape(url) |
| 125 |
elif "/post/webm/" not in url and "/post/mp4/" not in url: |
| 126 |
url = url.replace("/post/", "/post/full/") |
| 127 |
|
| 128 |
if self.gif and ("/post/webm/" in url or "/post/mp4/" in url): |
| 129 |
gif_url = text.extr(image, '<a href="', '"') |
| 130 |
if not gif_url: |
| 131 |
continue |
| 132 |
url = gif_url |
| 133 |
|
| 134 |
yield { |
| 135 |
"url": url, |
| 136 |
"post_id": post_id, |
| 137 |
"image_id": text.parse_int(image_id), |
| 138 |
"width": text.parse_int(width), |
| 139 |
"height": text.parse_int(height), |
| 140 |
"title": title, |
| 141 |
"description": description, |
| 142 |
"tags": tags, |
| 143 |
"date": date, |
| 144 |
"user": user, |
| 145 |
"num": num, |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
BASE_PATTERN = ReactorExtractor.update({ |
| 150 |
"reactor" : { |
| 151 |
"root": "http://reactor.cc", |
| 152 |
"pattern": r"(?:[^/.]+\.)?reactor\.cc", |
| 153 |
}, |
| 154 |
"pornreactor": { |
| 155 |
"root": "http://pornreactor.cc", |
| 156 |
"pattern": r"(?:www\.)?(?:pornreactor\.cc|fapreactor.com)", |
| 157 |
}, |
| 158 |
"thatpervert": { |
| 159 |
"root": "http://thatpervert.com", |
| 160 |
"pattern": r"thatpervert\.com", |
| 161 |
}, |
| 162 |
}) |
| 163 |
|
| 164 |
|
| 165 |
class ReactorTagExtractor(ReactorExtractor): |
| 166 |
"""Extractor for tag searches on *reactor.cc sites""" |
| 167 |
subcategory = "tag" |
| 168 |
directory_fmt = ("{category}", "{search_tags}") |
| 169 |
archive_fmt = "{search_tags}_{post_id}_{num}" |
| 170 |
pattern = BASE_PATTERN + r"/tag/([^/?#]+)(?:/[^/?#]+)?" |
| 171 |
example = "http://reactor.cc/tag/TAG" |
| 172 |
|
| 173 |
def __init__(self, match): |
| 174 |
ReactorExtractor.__init__(self, match) |
| 175 |
self.tag = self.groups[-1] |
| 176 |
|
| 177 |
def metadata(self): |
| 178 |
return {"search_tags": text.unescape(self.tag).replace("+", " ")} |
| 179 |
|
| 180 |
|
| 181 |
class ReactorSearchExtractor(ReactorExtractor): |
| 182 |
"""Extractor for search results on *reactor.cc sites""" |
| 183 |
subcategory = "search" |
| 184 |
directory_fmt = ("{category}", "search", "{search_tags}") |
| 185 |
archive_fmt = "s_{search_tags}_{post_id}_{num}" |
| 186 |
pattern = BASE_PATTERN + r"/search(?:/|\?q=)([^/?#]+)" |
| 187 |
example = "http://reactor.cc/search?q=QUERY" |
| 188 |
|
| 189 |
def __init__(self, match): |
| 190 |
ReactorExtractor.__init__(self, match) |
| 191 |
self.tag = self.groups[-1] |
| 192 |
|
| 193 |
def metadata(self): |
| 194 |
return {"search_tags": text.unescape(self.tag).replace("+", " ")} |
| 195 |
|
| 196 |
|
| 197 |
class ReactorUserExtractor(ReactorExtractor): |
| 198 |
"""Extractor for all posts of a user on *reactor.cc sites""" |
| 199 |
subcategory = "user" |
| 200 |
directory_fmt = ("{category}", "user", "{user}") |
| 201 |
pattern = BASE_PATTERN + r"/user/([^/?#]+)" |
| 202 |
example = "http://reactor.cc/user/USER" |
| 203 |
|
| 204 |
def __init__(self, match): |
| 205 |
ReactorExtractor.__init__(self, match) |
| 206 |
self.user = self.groups[-1] |
| 207 |
|
| 208 |
def metadata(self): |
| 209 |
return {"user": text.unescape(self.user).replace("+", " ")} |
| 210 |
|
| 211 |
|
| 212 |
class ReactorPostExtractor(ReactorExtractor): |
| 213 |
"""Extractor for single posts on *reactor.cc sites""" |
| 214 |
subcategory = "post" |
| 215 |
pattern = BASE_PATTERN + r"/post/(\d+)" |
| 216 |
example = "http://reactor.cc/post/12345" |
| 217 |
|
| 218 |
def __init__(self, match): |
| 219 |
ReactorExtractor.__init__(self, match) |
| 220 |
self.post_id = self.groups[-1] |
| 221 |
|
| 222 |
def items(self): |
| 223 |
post = self.request(self.root + self.path).text |
| 224 |
pos = post.find('class="uhead">') |
| 225 |
for image in self._parse_post(post[pos:]): |
| 226 |
if image["num"] == 1: |
| 227 |
yield Message.Directory, "", image |
| 228 |
url = image["url"] |
| 229 |
yield Message.Url, url, text.nameext_from_url(url, image) |