2022-07-27 22:58:23 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2023-01-03 15:14:23 +01:00
|
|
|
# Copyright 2022-2023 Mike Fährmann
|
2022-07-27 22:58:23 +02:00
|
|
|
#
|
|
|
|
# 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://www.zerochan.net/"""
|
|
|
|
|
|
|
|
from .booru import BooruExtractor
|
2022-07-29 12:49:04 +02:00
|
|
|
from ..cache import cache
|
2024-02-25 00:36:14 +01:00
|
|
|
from .. import text, util, exception
|
2024-07-23 09:50:06 +02:00
|
|
|
import collections
|
2024-07-29 11:19:10 +02:00
|
|
|
import re
|
2022-07-27 22:58:23 +02:00
|
|
|
|
|
|
|
BASE_PATTERN = r"(?:https?://)?(?:www\.)?zerochan\.net"
|
|
|
|
|
|
|
|
|
|
|
|
class ZerochanExtractor(BooruExtractor):
|
|
|
|
"""Base class for zerochan extractors"""
|
|
|
|
category = "zerochan"
|
|
|
|
root = "https://www.zerochan.net"
|
|
|
|
filename_fmt = "{id}.{extension}"
|
|
|
|
archive_fmt = "{id}"
|
2024-02-25 00:36:14 +01:00
|
|
|
page_start = 1
|
|
|
|
per_page = 250
|
2023-07-21 22:38:39 +02:00
|
|
|
cookies_domain = ".zerochan.net"
|
|
|
|
cookies_names = ("z_id", "z_hash")
|
2024-02-25 00:36:14 +01:00
|
|
|
request_interval = (0.5, 1.5)
|
2022-07-29 12:49:04 +02:00
|
|
|
|
|
|
|
def login(self):
|
2022-12-15 23:22:48 +01:00
|
|
|
self._logged_in = True
|
2023-07-21 22:38:39 +02:00
|
|
|
if self.cookies_check(self.cookies_names):
|
|
|
|
return
|
|
|
|
|
|
|
|
username, password = self._get_auth_info()
|
|
|
|
if username:
|
|
|
|
return self.cookies_update(self._login_impl(username, password))
|
|
|
|
|
|
|
|
self._logged_in = False
|
2022-07-29 12:49:04 +02:00
|
|
|
|
|
|
|
@cache(maxage=90*86400, keyarg=1)
|
|
|
|
def _login_impl(self, username, password):
|
|
|
|
self.log.info("Logging in as %s", username)
|
|
|
|
|
|
|
|
url = self.root + "/login"
|
|
|
|
headers = {
|
|
|
|
"Origin" : self.root,
|
|
|
|
"Referer" : url,
|
|
|
|
}
|
|
|
|
data = {
|
|
|
|
"ref" : "/",
|
|
|
|
"name" : username,
|
|
|
|
"password": password,
|
|
|
|
"login" : "Login",
|
|
|
|
}
|
|
|
|
|
|
|
|
response = self.request(url, method="POST", headers=headers, data=data)
|
|
|
|
if not response.history:
|
|
|
|
raise exception.AuthenticationError()
|
|
|
|
|
|
|
|
return response.cookies
|
2022-07-27 22:58:23 +02:00
|
|
|
|
2022-09-01 21:44:22 +02:00
|
|
|
def _parse_entry_html(self, entry_id):
|
2022-07-27 22:58:23 +02:00
|
|
|
url = "{}/{}".format(self.root, entry_id)
|
|
|
|
extr = text.extract_from(self.request(url).text)
|
|
|
|
|
2022-12-15 23:22:48 +01:00
|
|
|
data = {
|
|
|
|
"id" : text.parse_int(entry_id),
|
2023-11-24 21:21:14 +01:00
|
|
|
"author" : text.parse_unicode_escapes(extr(' "name": "', '"')),
|
2022-07-27 22:58:23 +02:00
|
|
|
"file_url": extr('"contentUrl": "', '"'),
|
2022-12-15 23:22:48 +01:00
|
|
|
"date" : text.parse_datetime(extr('"datePublished": "', '"')),
|
|
|
|
"width" : text.parse_int(extr('"width": "', ' ')),
|
|
|
|
"height" : text.parse_int(extr('"height": "', ' ')),
|
|
|
|
"size" : text.parse_bytes(extr('"contentSize": "', 'B')),
|
|
|
|
"path" : text.split_html(extr(
|
2023-11-24 21:21:14 +01:00
|
|
|
'class="breadcrumbs', '</nav>'))[2:],
|
2022-12-15 23:22:48 +01:00
|
|
|
"uploader": extr('href="/user/', '"'),
|
|
|
|
"tags" : extr('<ul id="tags"', '</ul>'),
|
2024-07-23 09:34:44 +02:00
|
|
|
"source" : text.unescape(text.extr(
|
|
|
|
extr('id="source-url"', '</a>'), 'href="', '"')),
|
2022-09-01 21:44:22 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 23:22:48 +01:00
|
|
|
html = data["tags"]
|
|
|
|
tags = data["tags"] = []
|
|
|
|
for tag in html.split("<li class=")[1:]:
|
2024-07-23 09:15:16 +02:00
|
|
|
category = text.extr(tag, '"', '"')
|
2023-11-24 21:21:14 +01:00
|
|
|
name = text.extr(tag, 'data-tag="', '"')
|
2024-07-23 09:15:16 +02:00
|
|
|
tags.append(category.partition(" ")[0].capitalize() + ":" + name)
|
2022-12-15 23:22:48 +01:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
2024-02-25 00:36:14 +01:00
|
|
|
def _parse_entry_api(self, entry_id):
|
2022-12-15 23:22:48 +01:00
|
|
|
url = "{}/{}?json".format(self.root, entry_id)
|
2024-07-29 11:19:10 +02:00
|
|
|
text = self.request(url).text
|
|
|
|
try:
|
|
|
|
item = util.json_loads(text)
|
|
|
|
except ValueError as exc:
|
|
|
|
if " control character " not in str(exc):
|
|
|
|
raise
|
|
|
|
text = re.sub(r"[\x00-\x1f\x7f]", "", text)
|
|
|
|
item = util.json_loads(text)
|
2022-12-15 23:22:48 +01:00
|
|
|
|
|
|
|
data = {
|
|
|
|
"id" : item["id"],
|
|
|
|
"file_url": item["full"],
|
|
|
|
"width" : item["width"],
|
|
|
|
"height" : item["height"],
|
|
|
|
"size" : item["size"],
|
|
|
|
"name" : item["primary"],
|
|
|
|
"md5" : item["hash"],
|
|
|
|
"source" : item.get("source"),
|
2022-07-27 22:58:23 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 23:22:48 +01:00
|
|
|
if not self._logged_in:
|
|
|
|
data["tags"] = item["tags"]
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2024-07-23 09:50:06 +02:00
|
|
|
def _tags(self, post, page):
|
|
|
|
tags = collections.defaultdict(list)
|
|
|
|
for tag in post["tags"]:
|
|
|
|
category, _, name = tag.partition(":")
|
|
|
|
tags[category].append(name)
|
|
|
|
for key, value in tags.items():
|
|
|
|
post["tags_" + key.lower()] = value
|
|
|
|
|
2022-07-27 22:58:23 +02:00
|
|
|
|
|
|
|
class ZerochanTagExtractor(ZerochanExtractor):
|
|
|
|
subcategory = "tag"
|
|
|
|
directory_fmt = ("{category}", "{search_tags}")
|
|
|
|
pattern = BASE_PATTERN + r"/(?!\d+$)([^/?#]+)/?(?:\?([^#]+))?"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.zerochan.net/TAG"
|
2022-07-27 22:58:23 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
ZerochanExtractor.__init__(self, match)
|
|
|
|
self.search_tag, self.query = match.groups()
|
|
|
|
|
2024-02-25 00:36:14 +01:00
|
|
|
def _init(self):
|
|
|
|
if self.config("pagination") == "html":
|
|
|
|
self.posts = self.posts_html
|
|
|
|
self.per_page = 24
|
|
|
|
else:
|
|
|
|
self.posts = self.posts_api
|
|
|
|
self.session.headers["User-Agent"] = util.USERAGENT
|
|
|
|
|
2022-07-27 22:58:23 +02:00
|
|
|
def metadata(self):
|
|
|
|
return {"search_tags": text.unquote(
|
|
|
|
self.search_tag.replace("+", " "))}
|
|
|
|
|
2024-02-25 00:36:14 +01:00
|
|
|
def posts_html(self):
|
2022-07-27 22:58:23 +02:00
|
|
|
url = self.root + "/" + self.search_tag
|
|
|
|
params = text.parse_query(self.query)
|
2024-02-25 00:36:14 +01:00
|
|
|
params["p"] = text.parse_int(params.get("p"), self.page_start)
|
2022-09-01 21:44:22 +02:00
|
|
|
metadata = self.config("metadata")
|
2022-07-27 22:58:23 +02:00
|
|
|
|
|
|
|
while True:
|
|
|
|
page = self.request(url, params=params).text
|
2022-11-04 23:39:38 +01:00
|
|
|
thumbs = text.extr(page, '<ul id="thumbs', '</ul>')
|
2022-07-27 22:58:23 +02:00
|
|
|
extr = text.extract_from(thumbs)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
post = extr('<li class="', '>')
|
|
|
|
if not post:
|
|
|
|
break
|
2022-09-01 21:44:22 +02:00
|
|
|
|
|
|
|
if metadata:
|
|
|
|
entry_id = extr('href="/', '"')
|
|
|
|
post = self._parse_entry_html(entry_id)
|
2024-02-25 00:36:14 +01:00
|
|
|
post.update(self._parse_entry_api(entry_id))
|
2022-09-01 21:44:22 +02:00
|
|
|
yield post
|
|
|
|
else:
|
|
|
|
yield {
|
|
|
|
"id" : extr('href="/', '"'),
|
|
|
|
"name" : extr('alt="', '"'),
|
2024-02-15 02:51:01 +01:00
|
|
|
"width" : extr('title="', '✕'),
|
2022-09-01 21:44:22 +02:00
|
|
|
"height": extr('', ' '),
|
2024-02-15 02:51:01 +01:00
|
|
|
"size" : extr('', 'b'),
|
2022-09-01 21:44:22 +02:00
|
|
|
"file_url": "https://static." + extr(
|
|
|
|
'<a href="https://static.', '"'),
|
|
|
|
}
|
2022-07-27 22:58:23 +02:00
|
|
|
|
|
|
|
if 'rel="next"' not in page:
|
|
|
|
break
|
|
|
|
params["p"] += 1
|
|
|
|
|
2024-02-25 00:36:14 +01:00
|
|
|
def posts_api(self):
|
|
|
|
url = self.root + "/" + self.search_tag
|
|
|
|
metadata = self.config("metadata")
|
|
|
|
params = {
|
|
|
|
"json": "1",
|
|
|
|
"l" : self.per_page,
|
|
|
|
"p" : self.page_start,
|
|
|
|
}
|
|
|
|
|
|
|
|
static = "https://static.zerochan.net/.full."
|
|
|
|
|
|
|
|
while True:
|
2024-07-26 19:56:39 +02:00
|
|
|
response = self.request(url, params=params, allow_redirects=False)
|
2024-08-10 11:32:30 +02:00
|
|
|
|
2024-07-26 19:56:39 +02:00
|
|
|
if response.status_code >= 300:
|
|
|
|
url = text.urljoin(self.root, response.headers["location"])
|
2024-08-10 11:32:30 +02:00
|
|
|
self.log.warning("HTTP redirect to %s", url)
|
|
|
|
if self.config("redirects"):
|
|
|
|
continue
|
|
|
|
raise exception.StopExtraction()
|
2024-07-26 19:56:39 +02:00
|
|
|
|
2024-08-10 11:32:30 +02:00
|
|
|
data = response.json()
|
2024-02-25 00:36:14 +01:00
|
|
|
try:
|
|
|
|
posts = data["items"]
|
2024-07-05 02:54:56 +02:00
|
|
|
except Exception:
|
|
|
|
self.log.debug("Server response: %s", data)
|
2024-02-25 00:36:14 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
if metadata:
|
|
|
|
for post in posts:
|
|
|
|
post_id = post["id"]
|
|
|
|
post.update(self._parse_entry_html(post_id))
|
|
|
|
post.update(self._parse_entry_api(post_id))
|
2024-07-20 02:11:27 +02:00
|
|
|
yield post
|
2024-02-25 00:36:14 +01:00
|
|
|
else:
|
|
|
|
for post in posts:
|
|
|
|
base = static + str(post["id"])
|
|
|
|
post["file_url"] = base + ".jpg"
|
|
|
|
post["_fallback"] = (base + ".png",)
|
2024-07-20 02:11:27 +02:00
|
|
|
yield post
|
2024-02-25 00:36:14 +01:00
|
|
|
|
|
|
|
if not data.get("next"):
|
|
|
|
return
|
|
|
|
params["p"] += 1
|
|
|
|
|
2022-07-27 22:58:23 +02:00
|
|
|
|
|
|
|
class ZerochanImageExtractor(ZerochanExtractor):
|
|
|
|
subcategory = "image"
|
|
|
|
pattern = BASE_PATTERN + r"/(\d+)"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.zerochan.net/12345"
|
2022-07-27 22:58:23 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
ZerochanExtractor.__init__(self, match)
|
|
|
|
self.image_id = match.group(1)
|
|
|
|
|
|
|
|
def posts(self):
|
2022-09-01 21:44:22 +02:00
|
|
|
post = self._parse_entry_html(self.image_id)
|
|
|
|
if self.config("metadata"):
|
2024-02-25 00:36:14 +01:00
|
|
|
post.update(self._parse_entry_api(self.image_id))
|
2022-09-01 21:44:22 +02:00
|
|
|
return (post,)
|