| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://nozomi.la/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text, util, dt |
| 13 |
|
| 14 |
|
| 15 |
def decode_nozomi(n): |
| 16 |
for i in range(0, len(n), 4): |
| 17 |
yield (n[i] << 24) + (n[i+1] << 16) + (n[i+2] << 8) + n[i+3] |
| 18 |
|
| 19 |
|
| 20 |
class NozomiExtractor(Extractor): |
| 21 |
"""Base class for nozomi extractors""" |
| 22 |
category = "nozomi" |
| 23 |
root = "https://nozomi.la" |
| 24 |
domain = "gold-usergeneratedcontent.net" |
| 25 |
filename_fmt = "{postid} {dataid}.{extension}" |
| 26 |
archive_fmt = "{dataid}" |
| 27 |
|
| 28 |
def _init(self): |
| 29 |
self.session.headers["Origin"] = self.root |
| 30 |
self.offset = 0 |
| 31 |
|
| 32 |
def items(self): |
| 33 |
data = self.metadata() |
| 34 |
|
| 35 |
post_ids = self.posts() |
| 36 |
if self.offset: |
| 37 |
post_ids = util.advance(post_ids, self.offset) |
| 38 |
for post_id in post_ids: |
| 39 |
post_id = str(post_id) |
| 40 |
url = (f"https://j.{self.domain}/post" |
| 41 |
f"/{post_id[-1]}/{post_id[-3:-1]}/{post_id}.json") |
| 42 |
response = self.request(url, fatal=False) |
| 43 |
|
| 44 |
if response.status_code >= 400: |
| 45 |
self.log.warning( |
| 46 |
"Skipping post %s ('%s %s')", |
| 47 |
post_id, response.status_code, response.reason) |
| 48 |
continue |
| 49 |
|
| 50 |
post = response.json() |
| 51 |
post["tags"] = self._list(post.get("general")) |
| 52 |
post["artist"] = self._list(post.get("artist")) |
| 53 |
post["copyright"] = self._list(post.get("copyright")) |
| 54 |
post["character"] = self._list(post.get("character")) |
| 55 |
|
| 56 |
try: |
| 57 |
post["date"] = dt.parse_iso(post["date"] + ":00") |
| 58 |
except Exception: |
| 59 |
post["date"] = dt.NONE |
| 60 |
|
| 61 |
post.update(data) |
| 62 |
|
| 63 |
images = post["imageurls"] |
| 64 |
for key in ("general", "imageurl", "imageurls"): |
| 65 |
if key in post: |
| 66 |
del post[key] |
| 67 |
|
| 68 |
yield Message.Directory, "", post |
| 69 |
for post["num"], image in enumerate(images, 1): |
| 70 |
post["filename"] = post["dataid"] = did = image["dataid"] |
| 71 |
post["is_video"] = video = \ |
| 72 |
True if image.get("is_video") else False |
| 73 |
|
| 74 |
ext = image["type"] |
| 75 |
if video: |
| 76 |
subdomain = "v" |
| 77 |
elif ext == "gif": |
| 78 |
subdomain = "g" |
| 79 |
else: |
| 80 |
subdomain = "w" |
| 81 |
ext = "webp" |
| 82 |
|
| 83 |
post["extension"] = ext |
| 84 |
post["url"] = url = (f"https://{subdomain}.{self.domain}" |
| 85 |
f"/{did[-1]}/{did[-3:-1]}/{did}.{ext}") |
| 86 |
yield Message.Url, url, post |
| 87 |
|
| 88 |
def posts(self): |
| 89 |
pnum, self.offset = divmod(self.offset, 64) |
| 90 |
pnum += text.parse_int(self.pnum, 1) |
| 91 |
return self._pagination("https://n.nozomi.la" + self.nozomi, pnum) |
| 92 |
|
| 93 |
def _pagination(self, url, pnum): |
| 94 |
offset = (pnum-1) * 256 |
| 95 |
|
| 96 |
while True: |
| 97 |
headers = {"Range": f"bytes={offset}-{offset + 255}"} |
| 98 |
|
| 99 |
try: |
| 100 |
response = self.request(url, headers=headers) |
| 101 |
except self.exc.HttpError as exc: |
| 102 |
if exc.status == 416: |
| 103 |
break |
| 104 |
raise |
| 105 |
|
| 106 |
yield from decode_nozomi(response.content) |
| 107 |
|
| 108 |
offset += 256 |
| 109 |
cr = response.headers.get("Content-Range", "").rpartition("/")[2] |
| 110 |
if text.parse_int(cr, offset) <= offset: |
| 111 |
break |
| 112 |
|
| 113 |
def skip_posts(self, num): |
| 114 |
self.offset = num |
| 115 |
return num |
| 116 |
|
| 117 |
def metadata(self): |
| 118 |
return {} |
| 119 |
|
| 120 |
def _list(self, src): |
| 121 |
return [x["tagname_display"] for x in src] if src else () |
| 122 |
|
| 123 |
|
| 124 |
class NozomiPostExtractor(NozomiExtractor): |
| 125 |
"""Extractor for individual posts on nozomi.la""" |
| 126 |
subcategory = "post" |
| 127 |
pattern = r"(?:https?://)?nozomi\.la/post/(\d+)" |
| 128 |
example = "https://nozomi.la/post/12345.html" |
| 129 |
skip_posts = None |
| 130 |
|
| 131 |
def __init__(self, match): |
| 132 |
NozomiExtractor.__init__(self, match) |
| 133 |
self.post_id = match[1] |
| 134 |
|
| 135 |
def posts(self): |
| 136 |
return (self.post_id,) |
| 137 |
|
| 138 |
|
| 139 |
class NozomiIndexExtractor(NozomiExtractor): |
| 140 |
"""Extractor for the nozomi.la index""" |
| 141 |
subcategory = "index" |
| 142 |
pattern = (r"(?:https?://)?nozomi\.la/" |
| 143 |
r"(?:(index(?:-Popular)?)-(\d+)\.html)?(?:$|#|\?)") |
| 144 |
example = "https://nozomi.la/index-1.html" |
| 145 |
|
| 146 |
def __init__(self, match): |
| 147 |
NozomiExtractor.__init__(self, match) |
| 148 |
index, self.pnum = match.groups() |
| 149 |
self.nozomi = f"/{index or 'index'}.nozomi" |
| 150 |
|
| 151 |
|
| 152 |
class NozomiTagExtractor(NozomiExtractor): |
| 153 |
"""Extractor for posts from tag searches on nozomi.la""" |
| 154 |
subcategory = "tag" |
| 155 |
directory_fmt = ("{category}", "{search_tags}") |
| 156 |
archive_fmt = "t_{search_tags}_{dataid}" |
| 157 |
pattern = r"(?:https?://)?nozomi\.la/tag/([^/?#]+)-(\d+)\." |
| 158 |
example = "https://nozomi.la/tag/TAG-1.html" |
| 159 |
|
| 160 |
def __init__(self, match): |
| 161 |
NozomiExtractor.__init__(self, match) |
| 162 |
tags, self.pnum = match.groups() |
| 163 |
self.tags = text.unquote(tags) |
| 164 |
self.nozomi = f"/nozomi/{self.tags}.nozomi" |
| 165 |
|
| 166 |
def metadata(self): |
| 167 |
return {"search_tags": self.tags} |
| 168 |
|
| 169 |
|
| 170 |
class NozomiSearchExtractor(NozomiExtractor): |
| 171 |
"""Extractor for search results on nozomi.la""" |
| 172 |
subcategory = "search" |
| 173 |
directory_fmt = ("{category}", "{search_tags:J }") |
| 174 |
archive_fmt = "t_{search_tags}_{dataid}" |
| 175 |
pattern = r"(?:https?://)?nozomi\.la/search\.html\?q=([^&#]+)" |
| 176 |
example = "https://nozomi.la/search.html?q=QUERY" |
| 177 |
|
| 178 |
def __init__(self, match): |
| 179 |
NozomiExtractor.__init__(self, match) |
| 180 |
self.tags = text.unquote(match[1]).split() |
| 181 |
|
| 182 |
def metadata(self): |
| 183 |
return {"search_tags": self.tags} |
| 184 |
|
| 185 |
def posts(self): |
| 186 |
result = None |
| 187 |
positive = [] |
| 188 |
negative = [] |
| 189 |
|
| 190 |
def nozomi(path): |
| 191 |
url = f"https://j.{self.domain}/{path}.nozomi" |
| 192 |
return decode_nozomi(self.request(url).content) |
| 193 |
|
| 194 |
for tag in self.tags: |
| 195 |
(negative if tag[0] == "-" else positive).append( |
| 196 |
text.quote(tag.replace("/", ""))) |
| 197 |
|
| 198 |
for tag in positive: |
| 199 |
ids = nozomi("nozomi/" + tag) |
| 200 |
if result is None: |
| 201 |
result = set(ids) |
| 202 |
else: |
| 203 |
result.intersection_update(ids) |
| 204 |
|
| 205 |
if result is None: |
| 206 |
result = set(nozomi("index")) |
| 207 |
for tag in negative: |
| 208 |
result.difference_update(nozomi("nozomi/" + tag[1:])) |
| 209 |
|
| 210 |
return sorted(result, reverse=True) if result else () |