| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://joyreactor.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text, util |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?joyreactor\.c(om|c)" |
| 15 |
|
| 16 |
|
| 17 |
class JoyreactorExtractor(Extractor): |
| 18 |
"""Base class for joyreactor extractors""" |
| 19 |
basecategory = "reactor" |
| 20 |
category = "joyreactor" |
| 21 |
root = "https://joyreactor.com" |
| 22 |
filename_fmt = ("{post_id}_{num:>02}_{file_id}" |
| 23 |
"{title:?_//[b:200]}.{extension}") |
| 24 |
archive_fmt = "{post_id}_{num}" |
| 25 |
request_interval = (3.0, 6.0) |
| 26 |
|
| 27 |
def __init__(self, match): |
| 28 |
self.root = "https://joyreactor.c" + match[1] |
| 29 |
Extractor.__init__(self, match) |
| 30 |
|
| 31 |
def _init(self): |
| 32 |
self.embeds = self.config("embeds", False) |
| 33 |
self.metadata = self.config("metadata", False) |
| 34 |
self.videos = self.config("videos", True) |
| 35 |
self.videos_formats = self.config("format") |
| 36 |
|
| 37 |
if isinstance(self.videos_formats, str): |
| 38 |
self.videos_formats = self.videos_formats.split(",") |
| 39 |
|
| 40 |
def items(self): |
| 41 |
headers = {"Referer": self.root + "/"} |
| 42 |
if not self.metadata: |
| 43 |
dfmt = ("%d.%m.%y, %H:%M" if self.groups[0] == "c" else |
| 44 |
"%d/%m/%y, %H:%M") |
| 45 |
|
| 46 |
for html, data in self.posts(): |
| 47 |
files = self._extract_files(html) |
| 48 |
|
| 49 |
extr = text.extract_from(html) |
| 50 |
post = { |
| 51 |
"count" : len(files), |
| 52 |
"user" : text.unescape(extr(' alt="', '"')), |
| 53 |
"user_id": extr("/avatar/user/", '"'), |
| 54 |
"tags" : text.split_html(extr( |
| 55 |
'class="post-tags', '</div>'))[1:], |
| 56 |
"content": extr( |
| 57 |
'class="post-content">', '<div class="post-footer">'), |
| 58 |
"date" : extr("</span></button>", "</div>"), |
| 59 |
"post_id": extr('"/post/', '"'), |
| 60 |
"_http_headers": headers, |
| 61 |
} |
| 62 |
|
| 63 |
if data is None: |
| 64 |
post["date"] = self.parse_datetime( |
| 65 |
post["date"].rpartition(">")[2], dfmt) |
| 66 |
else: |
| 67 |
post["date"] = self.parse_datetime_iso(data.get("createdAt")) |
| 68 |
post["nsfw"] = data.get("nsfw") |
| 69 |
post["unsafe"] = data.get("unsafe") |
| 70 |
post["banned"] = data.get("banned") |
| 71 |
post["comments"] = data.get("commentsCount") |
| 72 |
post["rating"] = data.get("rating") |
| 73 |
if seo := data.get("seoAttributes"): |
| 74 |
post["title"] = seo.get("title") |
| 75 |
|
| 76 |
yield Message.Directory, "", post |
| 77 |
for post["num"], file in enumerate(files, 1): |
| 78 |
url = file["file_url"] |
| 79 |
text.nameext_from_url(url, post) |
| 80 |
post.update(file) |
| 81 |
post["file_id"] = post["filename"].rpartition("-")[2] |
| 82 |
yield Message.Url, url, post |
| 83 |
|
| 84 |
def _extract_files(self, html): |
| 85 |
files = [] |
| 86 |
last = "" |
| 87 |
|
| 88 |
for url in text.extract_iter(html, 'src="', '"'): |
| 89 |
if "/avatar/" in url or last == url: |
| 90 |
continue |
| 91 |
last = url |
| 92 |
|
| 93 |
if ".joyreactor.c" not in url: |
| 94 |
if not self.embeds: |
| 95 |
continue |
| 96 |
files.append({"file_url": "ytdl:"+url, "type": "embed"}) |
| 97 |
elif "/webm/" in url: |
| 98 |
if not self.videos: |
| 99 |
continue |
| 100 |
if self.videos_formats is None: |
| 101 |
files.append({ |
| 102 |
"file_url": url, |
| 103 |
"type" : "video", |
| 104 |
"format" : "webm", |
| 105 |
}) |
| 106 |
else: |
| 107 |
for fmt in self.videos_formats: |
| 108 |
lhs, _, rhs = url.rpartition("/webm/") |
| 109 |
files.append({ |
| 110 |
"file_url": f"{lhs}/{fmt}/" |
| 111 |
f"{rhs[:rhs.find('.')]}.{fmt}", |
| 112 |
"type" : "video", |
| 113 |
"format" : fmt, |
| 114 |
}) |
| 115 |
else: |
| 116 |
lhs, _, rhs = url.rpartition("/") |
| 117 |
url = f"{lhs}/full/{rhs}" |
| 118 |
files.append({"file_url": url, "type": "image"}) |
| 119 |
|
| 120 |
return files |
| 121 |
|
| 122 |
def _request_graphql(self, opname, variables): |
| 123 |
url = "https://api.joyreactor.com/graphql" |
| 124 |
data = { |
| 125 |
"variables": variables, |
| 126 |
"query" : self.utils("graphql", opname), |
| 127 |
} |
| 128 |
return self.request_json( |
| 129 |
url, method="POST", json=data, interval=False)["data"] |
| 130 |
|
| 131 |
def _pagination(self, url, opname, variables): |
| 132 |
data = path = None |
| 133 |
|
| 134 |
while True: |
| 135 |
html = self.request(url).text |
| 136 |
html_posts = html.split('class="content"') |
| 137 |
del html_posts[0] |
| 138 |
|
| 139 |
if path is None and ('class="expand-wrapper relative">' |
| 140 |
'<div class="absolute' in html_posts[0]): |
| 141 |
del html_posts[0] |
| 142 |
|
| 143 |
pgn = text.extr(html, 'ant-pagination-next', "</li>") |
| 144 |
path = text.extr(pgn, 'href="', '"') |
| 145 |
|
| 146 |
if self.metadata: |
| 147 |
variables["page"] = (text.parse_int(path[path.rfind("/")+1:])+1 |
| 148 |
if path else None) |
| 149 |
data = self._request_graphql(opname, variables) |
| 150 |
data_posts = data.popitem()[1]["postPager"]["posts"] |
| 151 |
yield from zip(html_posts, data_posts) |
| 152 |
else: |
| 153 |
for post in html_posts: |
| 154 |
yield post, None |
| 155 |
|
| 156 |
if not path or not path.endswith("/1/rev"): |
| 157 |
break |
| 158 |
url = self.root + path |
| 159 |
|
| 160 |
|
| 161 |
class JoyreactorPostExtractor(JoyreactorExtractor): |
| 162 |
"""Extractor for single joyreactor posts""" |
| 163 |
subcategory = "post" |
| 164 |
pattern = BASE_PATTERN + r"/post/(\d+)" |
| 165 |
example = "http://joyreactor.com/post/12345" |
| 166 |
|
| 167 |
def posts(self): |
| 168 |
pid = self.groups[1] |
| 169 |
url = f"{self.root}/post/{pid}" |
| 170 |
page = self.request(url).text |
| 171 |
html = text.extr(page, 'class="content"', 'class="comment"') |
| 172 |
|
| 173 |
if self.metadata: |
| 174 |
data = self._request_graphql("IdPostPageQuery", { |
| 175 |
"id": util.b64encode(b"Post:" + bytes(str(pid), "ascii")), |
| 176 |
"isAuthorised": False, |
| 177 |
})["node"] |
| 178 |
else: |
| 179 |
data = None |
| 180 |
|
| 181 |
return ((html, data),) |
| 182 |
|
| 183 |
|
| 184 |
class JoyreactorTagExtractor(JoyreactorExtractor): |
| 185 |
"""Extractor for joyreactor tag searches""" |
| 186 |
subcategory = "tag" |
| 187 |
directory_fmt = ("{category}", "{search_tags}") |
| 188 |
archive_fmt = "{search_tags}_{post_id}_{num}" |
| 189 |
pattern = BASE_PATTERN + r"(/tag/([^/?#]+)(?:/[^/?#]+)?)" |
| 190 |
example = "http://joyreactor.com/tag/TAG" |
| 191 |
|
| 192 |
def posts(self): |
| 193 |
_, path, tag = self.groups |
| 194 |
self.kwdict["search_tags"] = tag = text.unquote(tag) |
| 195 |
|
| 196 |
variables = { |
| 197 |
"name" : tag, |
| 198 |
"page" : None, |
| 199 |
"lineType" : "GOOD", |
| 200 |
"isAuthorised": False, |
| 201 |
"isHomepage" : False, |
| 202 |
} |
| 203 |
return self._pagination( |
| 204 |
self.root + path, "TagPageQuery", variables) |
| 205 |
|
| 206 |
|
| 207 |
class JoyreactorUserExtractor(JoyreactorExtractor): |
| 208 |
"""Extractor for posts of a joyreactor user""" |
| 209 |
subcategory = "user" |
| 210 |
directory_fmt = ("{category}", "user", "{user}") |
| 211 |
pattern = BASE_PATTERN + r"(/user/([^/?#]+)(?:/[^/?#]+)?)" |
| 212 |
example = "http://joyreactor.com/user/USER" |
| 213 |
|
| 214 |
def posts(self): |
| 215 |
_, path, user = self.groups |
| 216 |
|
| 217 |
variables = { |
| 218 |
"username" : text.unquote(user), |
| 219 |
"page" : None, |
| 220 |
"lineType" : "GOOD", |
| 221 |
"isAuthorised": False, |
| 222 |
"isHomepage" : False, |
| 223 |
} |
| 224 |
return self._pagination( |
| 225 |
self.root + path, "UserProfilePageQuery", variables) |
| 226 |
|
| 227 |
|
| 228 |
class JoyreactorSearchExtractor(JoyreactorExtractor): |
| 229 |
"""Extractor for joyreactor search results""" |
| 230 |
subcategory = "search" |
| 231 |
directory_fmt = ("{category}", "{search_tags}") |
| 232 |
archive_fmt = "{search_tags}_{post_id}_{num}" |
| 233 |
pattern = BASE_PATTERN + r"(/search/([^/?#]+)(?:/[^/?#]+)?)" |
| 234 |
example = "http://joyreactor.com/search/TAG" |
| 235 |
|
| 236 |
def posts(self): |
| 237 |
_, path, query = self.groups |
| 238 |
self.kwdict["search_tags"] = query = text.unquote(query) |
| 239 |
|
| 240 |
variables = { |
| 241 |
"query" : query, |
| 242 |
"page" : None, |
| 243 |
"tagNames" : (), |
| 244 |
"username" : None, |
| 245 |
"isAuthorised": False, |
| 246 |
"showUnsafe" : None, |
| 247 |
"showNsfw" : True, |
| 248 |
"showOnlyNsfw": False, |
| 249 |
"minRating" : None, |
| 250 |
"maxRating" : None, |
| 251 |
"sortByDate" : False, |
| 252 |
"sortByRating": False, |
| 253 |
} |
| 254 |
|
| 255 |
return self._pagination( |
| 256 |
self.root + path, "SearchPageQuery", variables) |