| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://itaku.ee/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message, Dispatch |
| 12 |
from .. import text, util |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?itaku\.ee" |
| 15 |
USER_PATTERN = BASE_PATTERN + r"/profile/([^/?#]+)" |
| 16 |
|
| 17 |
|
| 18 |
class ItakuExtractor(Extractor): |
| 19 |
"""Base class for itaku extractors""" |
| 20 |
category = "itaku" |
| 21 |
root = "https://itaku.ee" |
| 22 |
directory_fmt = ("{category}", "{owner_username}") |
| 23 |
filename_fmt = ("{id}{title:? //}.{extension}") |
| 24 |
archive_fmt = "{id}" |
| 25 |
request_interval = (0.5, 1.5) |
| 26 |
|
| 27 |
def _init(self): |
| 28 |
self.api = ItakuAPI(self) |
| 29 |
self.videos = self.config("videos", True) |
| 30 |
|
| 31 |
def items(self): |
| 32 |
if images := self.images(): |
| 33 |
for image in images: |
| 34 |
image["date"] = self.parse_datetime_iso(image["date_added"]) |
| 35 |
for category, tags in image.pop("categorized_tags").items(): |
| 36 |
image["tags_" + category.lower()] = [ |
| 37 |
t["name"] for t in tags] |
| 38 |
image["tags"] = [t["name"] for t in image["tags"]] |
| 39 |
|
| 40 |
sections = [] |
| 41 |
for s in image["sections"]: |
| 42 |
if group := s["group"]: |
| 43 |
sections.append(f"{group['title']}/{s['title']}") |
| 44 |
else: |
| 45 |
sections.append(s["title"]) |
| 46 |
image["sections"] = sections |
| 47 |
|
| 48 |
if self.videos and image["video"]: |
| 49 |
url = image["video"]["video"] |
| 50 |
else: |
| 51 |
url = image["image"] |
| 52 |
|
| 53 |
yield Message.Directory, "", image |
| 54 |
yield Message.Url, url, text.nameext_from_url(url, image) |
| 55 |
return |
| 56 |
|
| 57 |
if posts := self.posts(): |
| 58 |
for post in posts: |
| 59 |
images = post.pop("gallery_images") or () |
| 60 |
post["count"] = len(images) |
| 61 |
post["date"] = self.parse_datetime_iso(post["date_added"]) |
| 62 |
post["tags"] = [t["name"] for t in post["tags"]] |
| 63 |
|
| 64 |
yield Message.Directory, "", post |
| 65 |
for post["num"], image in enumerate(images, 1): |
| 66 |
post["file"] = image |
| 67 |
image["date"] = self.parse_datetime_iso( |
| 68 |
image["date_added"]) |
| 69 |
|
| 70 |
url = image["image"] |
| 71 |
yield Message.Url, url, text.nameext_from_url(url, post) |
| 72 |
return |
| 73 |
|
| 74 |
if users := self.users(): |
| 75 |
base = self.root + "/profile/" |
| 76 |
for user in users: |
| 77 |
url = base + user["owner_username"] |
| 78 |
user["_extractor"] = ItakuUserExtractor |
| 79 |
yield Message.Queue, url, user |
| 80 |
return |
| 81 |
|
| 82 |
images = posts = users = util.noop |
| 83 |
|
| 84 |
|
| 85 |
class ItakuGalleryExtractor(ItakuExtractor): |
| 86 |
"""Extractor for an itaku user's gallery""" |
| 87 |
subcategory = "gallery" |
| 88 |
pattern = USER_PATTERN + r"/gallery(?:/(\d+))?" |
| 89 |
example = "https://itaku.ee/profile/USER/gallery" |
| 90 |
|
| 91 |
def images(self): |
| 92 |
user, section = self.groups |
| 93 |
return self.api.galleries_images({ |
| 94 |
"owner" : self.api.user_id(user), |
| 95 |
"sections": section, |
| 96 |
}) |
| 97 |
|
| 98 |
|
| 99 |
class ItakuPostsExtractor(ItakuExtractor): |
| 100 |
"""Extractor for an itaku user's posts""" |
| 101 |
subcategory = "posts" |
| 102 |
directory_fmt = ("{category}", "{owner_username}", "Posts", |
| 103 |
"{id}{title:? //}") |
| 104 |
filename_fmt = "{file[id]}{file[title]:? //}.{extension}" |
| 105 |
archive_fmt = "{id}_{file[id]}" |
| 106 |
pattern = USER_PATTERN + r"/posts(?:/(\d+))?" |
| 107 |
example = "https://itaku.ee/profile/USER/posts" |
| 108 |
|
| 109 |
def posts(self): |
| 110 |
user, folder = self.groups |
| 111 |
return self.api.posts({ |
| 112 |
"owner" : self.api.user_id(user), |
| 113 |
"folders": folder, |
| 114 |
}) |
| 115 |
|
| 116 |
|
| 117 |
class ItakuStarsExtractor(ItakuExtractor): |
| 118 |
"""Extractor for an itaku user's starred images""" |
| 119 |
subcategory = "stars" |
| 120 |
pattern = USER_PATTERN + r"/stars(?:/(\d+))?" |
| 121 |
example = "https://itaku.ee/profile/USER/stars" |
| 122 |
|
| 123 |
def images(self): |
| 124 |
user, section = self.groups |
| 125 |
return self.api.galleries_images({ |
| 126 |
"stars_of": self.api.user_id(user), |
| 127 |
"sections": section, |
| 128 |
"ordering": "-like_date", |
| 129 |
}, "/user_starred_imgs") |
| 130 |
|
| 131 |
|
| 132 |
class ItakuFollowingExtractor(ItakuExtractor): |
| 133 |
subcategory = "following" |
| 134 |
pattern = USER_PATTERN + r"/following" |
| 135 |
example = "https://itaku.ee/profile/USER/following" |
| 136 |
|
| 137 |
def users(self): |
| 138 |
return self.api.user_profiles({ |
| 139 |
"followed_by": self.api.user_id(self.groups[0]), |
| 140 |
}) |
| 141 |
|
| 142 |
|
| 143 |
class ItakuFollowersExtractor(ItakuExtractor): |
| 144 |
subcategory = "followers" |
| 145 |
pattern = USER_PATTERN + r"/followers" |
| 146 |
example = "https://itaku.ee/profile/USER/followers" |
| 147 |
|
| 148 |
def users(self): |
| 149 |
return self.api.user_profiles({ |
| 150 |
"followers_of": self.api.user_id(self.groups[0]), |
| 151 |
}) |
| 152 |
|
| 153 |
|
| 154 |
class ItakuBookmarksExtractor(ItakuExtractor): |
| 155 |
"""Extractor for an itaku bookmarks folder""" |
| 156 |
subcategory = "bookmarks" |
| 157 |
pattern = USER_PATTERN + r"/bookmarks/(image|user)/(\d+)" |
| 158 |
example = "https://itaku.ee/profile/USER/bookmarks/image/12345" |
| 159 |
|
| 160 |
def _init(self): |
| 161 |
if self.groups[1] == "user": |
| 162 |
self.images = util.noop |
| 163 |
ItakuExtractor._init(self) |
| 164 |
|
| 165 |
def images(self): |
| 166 |
return self.api.galleries_images({ |
| 167 |
"bookmark_folder": self.groups[2], |
| 168 |
}) |
| 169 |
|
| 170 |
def users(self): |
| 171 |
return self.api.user_profiles({ |
| 172 |
"bookmark_folder": self.groups[2], |
| 173 |
}) |
| 174 |
|
| 175 |
|
| 176 |
class ItakuUserExtractor(Dispatch, ItakuExtractor): |
| 177 |
"""Extractor for itaku user profiles""" |
| 178 |
pattern = USER_PATTERN + r"/?(?:$|\?|#)" |
| 179 |
example = "https://itaku.ee/profile/USER" |
| 180 |
|
| 181 |
def items(self): |
| 182 |
base = f"{self.root}/profile/{self.groups[0]}/" |
| 183 |
return self._dispatch_extractors(( |
| 184 |
(ItakuGalleryExtractor , base + "gallery"), |
| 185 |
(ItakuPostsExtractor , base + "posts"), |
| 186 |
(ItakuFollowersExtractor, base + "followers"), |
| 187 |
(ItakuFollowingExtractor, base + "following"), |
| 188 |
(ItakuStarsExtractor , base + "stars"), |
| 189 |
), ("gallery",)) |
| 190 |
|
| 191 |
|
| 192 |
class ItakuImageExtractor(ItakuExtractor): |
| 193 |
subcategory = "image" |
| 194 |
pattern = BASE_PATTERN + r"/images/(\d+)" |
| 195 |
example = "https://itaku.ee/images/12345" |
| 196 |
|
| 197 |
def images(self): |
| 198 |
return (self.api.image(self.groups[0]),) |
| 199 |
|
| 200 |
|
| 201 |
class ItakuPostExtractor(ItakuExtractor): |
| 202 |
subcategory = "post" |
| 203 |
directory_fmt = ("{category}", "{owner_username}", "Posts", |
| 204 |
"{id}{title:? //}") |
| 205 |
filename_fmt = "{file[id]}{file[title]:? //}.{extension}" |
| 206 |
archive_fmt = "{id}_{file[id]}" |
| 207 |
pattern = BASE_PATTERN + r"/posts/(\d+)" |
| 208 |
example = "https://itaku.ee/posts/12345" |
| 209 |
|
| 210 |
def posts(self): |
| 211 |
return (self.api.post(self.groups[0]),) |
| 212 |
|
| 213 |
|
| 214 |
class ItakuSearchExtractor(ItakuExtractor): |
| 215 |
subcategory = "search" |
| 216 |
pattern = BASE_PATTERN + r"/home/images/?\?([^#]+)" |
| 217 |
example = "https://itaku.ee/home/images?tags=SEARCH" |
| 218 |
|
| 219 |
def images(self): |
| 220 |
required_tags = [] |
| 221 |
negative_tags = [] |
| 222 |
optional_tags = [] |
| 223 |
|
| 224 |
params = text.parse_query_list( |
| 225 |
self.groups[0], {"tags", "maturity_rating"}) |
| 226 |
if tags := params.pop("tags", None): |
| 227 |
for tag in tags: |
| 228 |
if not tag: |
| 229 |
pass |
| 230 |
elif tag[0] == "-": |
| 231 |
negative_tags.append(tag[1:]) |
| 232 |
elif tag[0] == "~": |
| 233 |
optional_tags.append(tag[1:]) |
| 234 |
else: |
| 235 |
required_tags.append(tag) |
| 236 |
|
| 237 |
return self.api.galleries_images({ |
| 238 |
"required_tags": required_tags, |
| 239 |
"negative_tags": negative_tags, |
| 240 |
"optional_tags": optional_tags, |
| 241 |
}) |
| 242 |
|
| 243 |
|
| 244 |
class ItakuAPI(): |
| 245 |
|
| 246 |
def __init__(self, extractor): |
| 247 |
self.extractor = extractor |
| 248 |
self.root = extractor.root + "/api" |
| 249 |
self.headers = { |
| 250 |
"Accept": "application/json, text/plain, */*", |
| 251 |
} |
| 252 |
|
| 253 |
def galleries_images(self, params, path=""): |
| 254 |
endpoint = f"/galleries/images{path}/" |
| 255 |
params = { |
| 256 |
"cursor" : None, |
| 257 |
"date_range": "", |
| 258 |
"maturity_rating": ("SFW", "Questionable", "NSFW"), |
| 259 |
"ordering" : self._order(), |
| 260 |
"page" : "1", |
| 261 |
"page_size" : "30", |
| 262 |
"visibility": ("PUBLIC", "PROFILE_ONLY"), |
| 263 |
**params, |
| 264 |
} |
| 265 |
return self._pagination(endpoint, params, self.image) |
| 266 |
|
| 267 |
def posts(self, params): |
| 268 |
endpoint = "/posts/" |
| 269 |
params = { |
| 270 |
"cursor" : None, |
| 271 |
"date_range": "", |
| 272 |
"maturity_rating": ("SFW", "Questionable", "NSFW"), |
| 273 |
"ordering" : self._order(), |
| 274 |
"page" : "1", |
| 275 |
"page_size" : "30", |
| 276 |
**params, |
| 277 |
} |
| 278 |
return self._pagination(endpoint, params) |
| 279 |
|
| 280 |
def user_profiles(self, params): |
| 281 |
endpoint = "/user_profiles/" |
| 282 |
params = { |
| 283 |
"cursor" : None, |
| 284 |
"ordering" : self._order(), |
| 285 |
"page" : "1", |
| 286 |
"page_size": "50", |
| 287 |
"sfw_only" : "false", |
| 288 |
**params, |
| 289 |
} |
| 290 |
return self._pagination(endpoint, params) |
| 291 |
|
| 292 |
def image(self, image_id): |
| 293 |
endpoint = f"/galleries/images/{image_id}/" |
| 294 |
return self._call(endpoint) |
| 295 |
|
| 296 |
def post(self, post_id): |
| 297 |
endpoint = f"/posts/{post_id}/" |
| 298 |
return self._call(endpoint) |
| 299 |
|
| 300 |
def user(self, username): |
| 301 |
return self._call(f"/user_profiles/{username}/") |
| 302 |
|
| 303 |
def user_id(self, username): |
| 304 |
if username.startswith("id:"): |
| 305 |
return int(username[3:]) |
| 306 |
return self.extractor.cache(self.user, username)["owner"] |
| 307 |
|
| 308 |
def _call(self, endpoint, params=None): |
| 309 |
if not endpoint.startswith("http"): |
| 310 |
endpoint = self.root + endpoint |
| 311 |
return self.extractor.request_json( |
| 312 |
endpoint, params=params, headers=self.headers) |
| 313 |
|
| 314 |
def _pagination(self, endpoint, params, extend=None): |
| 315 |
data = self._call(endpoint, params) |
| 316 |
|
| 317 |
while True: |
| 318 |
if extend is None: |
| 319 |
yield from data["results"] |
| 320 |
else: |
| 321 |
for result in data["results"]: |
| 322 |
yield extend(result["id"]) |
| 323 |
|
| 324 |
url_next = data["links"].get("next") |
| 325 |
if not url_next: |
| 326 |
return |
| 327 |
|
| 328 |
data = self._call(url_next) |
| 329 |
|
| 330 |
def _order(self): |
| 331 |
if order := self.extractor.config("order"): |
| 332 |
if order in {"a", "asc", "r", "reverse"}: |
| 333 |
return "date_added" |
| 334 |
if order not in {"d", "desc"}: |
| 335 |
return order |
| 336 |
return "-date_added" |