1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# -*- coding: utf-8 -*-
# Copyright 2022 Mike Fährmann
#
# 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
from ..cache import cache
from .. import text, exception
from xml.etree import ElementTree
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}"
cookiedomain = ".zerochan.net"
cookienames = ("z_id", "z_hash")
def login(self):
if not self._check_cookies(self.cookienames):
username, password = self._get_auth_info()
if username:
self._update_cookies(self._login_impl(username, password))
# force legacy layout
self.session.cookies.set("v3", "0", domain=self.cookiedomain)
@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
def _parse_entry_html(self, entry_id):
url = "{}/{}".format(self.root, entry_id)
extr = text.extract_from(self.request(url).text)
return {
"id" : entry_id,
"author": extr('"author": "', '"'),
"file_url": extr('"contentUrl": "', '"'),
"date" : text.parse_datetime(extr(
'"datePublished": "', '"'), "%a %b %d %H:%M:%S %Y"),
"width" : extr('"width": "', ' '),
"height": extr('"height": "', ' '),
"size" : text.parse_bytes(extr('"contentSize": "', 'B')),
"path" : text.split_html(extr(
'class="breadcrumbs', '</p>'))[3::2],
"tags" : extr('alt="Tags: Anime, ', '"').split(", ")
}
def _parse_entry_xml(self, entry_id):
url = "{}/{}?xml".format(self.root, entry_id)
item = ElementTree.fromstring(self.request(url).text)[0][-1]
# content = item[4].attrib
return {
# "id" : entry_id,
# "file_url": content["url"],
# "width" : content["width"],
# "height": content["height"],
# "size" : content["filesize"],
"name" : item[2].text,
"tags" : item[5].text.lstrip().split(", "),
"md5" : item[6].text,
}
class ZerochanTagExtractor(ZerochanExtractor):
subcategory = "tag"
directory_fmt = ("{category}", "{search_tags}")
pattern = BASE_PATTERN + r"/(?!\d+$)([^/?#]+)/?(?:\?([^#]+))?"
test = ("https://www.zerochan.net/Perth+%28Kantai+Collection%29", {
"pattern": r"https://static\.zerochan\.net/.+\.full\.\d+\.(jpg|png)",
"count": "> 24",
"keywords": {
"extension": r"re:jpg|png",
"file_url": "",
"filename": r"re:Perth.\(Kantai.Collection\).full.\d+",
"height": r"re:^\d+$",
"id": r"re:^\d+$",
"name": "Perth (Kantai Collection)",
"search_tags": "Perth (Kantai Collection)",
"size": r"re:^\d+k$",
"width": r"re:^\d+$",
},
})
def __init__(self, match):
ZerochanExtractor.__init__(self, match)
self.search_tag, self.query = match.groups()
def metadata(self):
return {"search_tags": text.unquote(
self.search_tag.replace("+", " "))}
def posts(self):
url = self.root + "/" + self.search_tag
params = text.parse_query(self.query)
params["p"] = text.parse_int(params.get("p"), 1)
metadata = self.config("metadata")
while True:
page = self.request(url, params=params).text
thumbs = text.extract(page, '<ul id="thumbs', '</ul>')[0]
extr = text.extract_from(thumbs)
while True:
post = extr('<li class="', '>')
if not post:
break
if metadata:
entry_id = extr('href="/', '"')
post = self._parse_entry_html(entry_id)
post.update(self._parse_entry_xml(entry_id))
yield post
else:
yield {
"id" : extr('href="/', '"'),
"name" : extr('alt="', '"'),
"width" : extr('title="', 'x'),
"height": extr('', ' '),
"size" : extr('', 'B'),
"file_url": "https://static." + extr(
'<a href="https://static.', '"'),
}
if 'rel="next"' not in page:
break
params["p"] += 1
class ZerochanImageExtractor(ZerochanExtractor):
subcategory = "image"
pattern = BASE_PATTERN + r"/(\d+)"
test = ("https://www.zerochan.net/2920445", {
"pattern": r"https://static\.zerochan\.net/"
r"Perth\.%28Kantai\.Collection%29\.full.2920445\.jpg",
"keyword": {
"author": "YukinoTokisaki",
"date": "dt:2020-04-24 21:33:44",
"file_url": str,
"filename": "Perth.(Kantai.Collection).full.2920445",
"height": "1366",
"id": "2920445",
"size": "1929k",
"width": "1920",
},
})
def __init__(self, match):
ZerochanExtractor.__init__(self, match)
self.image_id = match.group(1)
def posts(self):
post = self._parse_entry_html(self.image_id)
if self.config("metadata"):
post.update(self._parse_entry_xml(self.image_id))
return (post,)
|