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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
# -*- coding: utf-8 -*-
# Copyright 2022-2025 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://itaku.ee/"""
from .common import Extractor, Message, Dispatch
from ..cache import memcache
from .. import text, util
BASE_PATTERN = r"(?:https?://)?itaku\.ee"
USER_PATTERN = rf"{BASE_PATTERN}/profile/([^/?#]+)"
class ItakuExtractor(Extractor):
"""Base class for itaku extractors"""
category = "itaku"
root = "https://itaku.ee"
directory_fmt = ("{category}", "{owner_username}")
filename_fmt = ("{id}{title:? //}.{extension}")
archive_fmt = "{id}"
request_interval = (0.5, 1.5)
def _init(self):
self.api = ItakuAPI(self)
self.videos = self.config("videos", True)
def items(self):
if images := self.images():
for image in images:
image["date"] = self.parse_datetime_iso(image["date_added"])
for category, tags in image.pop("categorized_tags").items():
image[f"tags_{category.lower()}"] = [
t["name"] for t in tags]
image["tags"] = [t["name"] for t in image["tags"]]
sections = []
for s in image["sections"]:
if group := s["group"]:
sections.append(f"{group['title']}/{s['title']}")
else:
sections.append(s["title"])
image["sections"] = sections
if self.videos and image["video"]:
url = image["video"]["video"]
else:
url = image["image"]
yield Message.Directory, "", image
yield Message.Url, url, text.nameext_from_url(url, image)
return
if posts := self.posts():
for post in posts:
images = post.pop("gallery_images") or ()
post["count"] = len(images)
post["date"] = self.parse_datetime_iso(post["date_added"])
post["tags"] = [t["name"] for t in post["tags"]]
yield Message.Directory, "", post
for post["num"], image in enumerate(images, 1):
post["file"] = image
image["date"] = self.parse_datetime_iso(
image["date_added"])
url = image["image"]
yield Message.Url, url, text.nameext_from_url(url, post)
return
if users := self.users():
base = f"{self.root}/profile/"
for user in users:
url = f"{base}{user['owner_username']}"
user["_extractor"] = ItakuUserExtractor
yield Message.Queue, url, user
return
images = posts = users = util.noop
class ItakuGalleryExtractor(ItakuExtractor):
"""Extractor for an itaku user's gallery"""
subcategory = "gallery"
pattern = rf"{USER_PATTERN}/gallery(?:/(\d+))?"
example = "https://itaku.ee/profile/USER/gallery"
def images(self):
user, section = self.groups
return self.api.galleries_images({
"owner" : self.api.user_id(user),
"sections": section,
})
class ItakuPostsExtractor(ItakuExtractor):
"""Extractor for an itaku user's posts"""
subcategory = "posts"
directory_fmt = ("{category}", "{owner_username}", "Posts",
"{id}{title:? //}")
filename_fmt = "{file[id]}{file[title]:? //}.{extension}"
archive_fmt = "{id}_{file[id]}"
pattern = rf"{USER_PATTERN}/posts(?:/(\d+))?"
example = "https://itaku.ee/profile/USER/posts"
def posts(self):
user, folder = self.groups
return self.api.posts({
"owner" : self.api.user_id(user),
"folders": folder,
})
class ItakuStarsExtractor(ItakuExtractor):
"""Extractor for an itaku user's starred images"""
subcategory = "stars"
pattern = rf"{USER_PATTERN}/stars(?:/(\d+))?"
example = "https://itaku.ee/profile/USER/stars"
def images(self):
user, section = self.groups
return self.api.galleries_images({
"stars_of": self.api.user_id(user),
"sections": section,
"ordering": "-like_date",
}, "/user_starred_imgs")
class ItakuFollowingExtractor(ItakuExtractor):
subcategory = "following"
pattern = rf"{USER_PATTERN}/following"
example = "https://itaku.ee/profile/USER/following"
def users(self):
return self.api.user_profiles({
"followed_by": self.api.user_id(self.groups[0]),
})
class ItakuFollowersExtractor(ItakuExtractor):
subcategory = "followers"
pattern = rf"{USER_PATTERN}/followers"
example = "https://itaku.ee/profile/USER/followers"
def users(self):
return self.api.user_profiles({
"followers_of": self.api.user_id(self.groups[0]),
})
class ItakuBookmarksExtractor(ItakuExtractor):
"""Extractor for an itaku bookmarks folder"""
subcategory = "bookmarks"
pattern = rf"{USER_PATTERN}/bookmarks/(image|user)/(\d+)"
example = "https://itaku.ee/profile/USER/bookmarks/image/12345"
def _init(self):
if self.groups[1] == "user":
self.images = util.noop
ItakuExtractor._init(self)
def images(self):
return self.api.galleries_images({
"bookmark_folder": self.groups[2],
})
def users(self):
return self.api.user_profiles({
"bookmark_folder": self.groups[2],
})
class ItakuUserExtractor(Dispatch, ItakuExtractor):
"""Extractor for itaku user profiles"""
pattern = rf"{USER_PATTERN}/?(?:$|\?|#)"
example = "https://itaku.ee/profile/USER"
def items(self):
base = f"{self.root}/profile/{self.groups[0]}/"
return self._dispatch_extractors((
(ItakuGalleryExtractor , f"{base}gallery"),
(ItakuPostsExtractor , f"{base}posts"),
(ItakuFollowersExtractor, f"{base}followers"),
(ItakuFollowingExtractor, f"{base}following"),
(ItakuStarsExtractor , f"{base}stars"),
), ("gallery",))
class ItakuImageExtractor(ItakuExtractor):
subcategory = "image"
pattern = rf"{BASE_PATTERN}/images/(\d+)"
example = "https://itaku.ee/images/12345"
def images(self):
return (self.api.image(self.groups[0]),)
class ItakuPostExtractor(ItakuExtractor):
subcategory = "post"
directory_fmt = ("{category}", "{owner_username}", "Posts",
"{id}{title:? //}")
filename_fmt = "{file[id]}{file[title]:? //}.{extension}"
archive_fmt = "{id}_{file[id]}"
pattern = rf"{BASE_PATTERN}/posts/(\d+)"
example = "https://itaku.ee/posts/12345"
def posts(self):
return (self.api.post(self.groups[0]),)
class ItakuSearchExtractor(ItakuExtractor):
subcategory = "search"
pattern = rf"{BASE_PATTERN}/home/images/?\?([^#]+)"
example = "https://itaku.ee/home/images?tags=SEARCH"
def images(self):
required_tags = []
negative_tags = []
optional_tags = []
params = text.parse_query_list(
self.groups[0], {"tags", "maturity_rating"})
if tags := params.pop("tags", None):
for tag in tags:
if not tag:
pass
elif tag[0] == "-":
negative_tags.append(tag[1:])
elif tag[0] == "~":
optional_tags.append(tag[1:])
else:
required_tags.append(tag)
return self.api.galleries_images({
"required_tags": required_tags,
"negative_tags": negative_tags,
"optional_tags": optional_tags,
})
class ItakuAPI():
def __init__(self, extractor):
self.extractor = extractor
self.root = f"{extractor.root}/api"
self.headers = {
"Accept": "application/json, text/plain, */*",
}
def galleries_images(self, params, path=""):
endpoint = f"/galleries/images{path}/"
params = {
"cursor" : None,
"date_range": "",
"maturity_rating": ("SFW", "Questionable", "NSFW"),
"ordering" : self._order(),
"page" : "1",
"page_size" : "30",
"visibility": ("PUBLIC", "PROFILE_ONLY"),
**params,
}
return self._pagination(endpoint, params, self.image)
def posts(self, params):
endpoint = "/posts/"
params = {
"cursor" : None,
"date_range": "",
"maturity_rating": ("SFW", "Questionable", "NSFW"),
"ordering" : self._order(),
"page" : "1",
"page_size" : "30",
**params,
}
return self._pagination(endpoint, params)
def user_profiles(self, params):
endpoint = "/user_profiles/"
params = {
"cursor" : None,
"ordering" : self._order(),
"page" : "1",
"page_size": "50",
"sfw_only" : "false",
**params,
}
return self._pagination(endpoint, params)
def image(self, image_id):
endpoint = f"/galleries/images/{image_id}/"
return self._call(endpoint)
def post(self, post_id):
endpoint = f"/posts/{post_id}/"
return self._call(endpoint)
@memcache(keyarg=1)
def user(self, username):
return self._call(f"/user_profiles/{username}/")
def user_id(self, username):
if username.startswith("id:"):
return int(username[3:])
return self.user(username)["owner"]
def _call(self, endpoint, params=None):
if not endpoint.startswith("http"):
endpoint = f"{self.root}{endpoint}"
return self.extractor.request_json(
endpoint, params=params, headers=self.headers)
def _pagination(self, endpoint, params, extend=None):
data = self._call(endpoint, params)
while True:
if extend is None:
yield from data["results"]
else:
for result in data["results"]:
yield extend(result["id"])
url_next = data["links"].get("next")
if not url_next:
return
data = self._call(url_next)
def _order(self):
if order := self.extractor.config("order"):
if order in {"a", "asc", "r", "reverse"}:
return "date_added"
if order not in {"d", "desc"}:
return order
return "-date_added"
|