2015-04-11 00:17:43 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-10-12 21:56:44 +02:00
|
|
|
|
2022-01-12 21:33:02 +01:00
|
|
|
# Copyright 2014-2022 Mike Fährmann
|
2015-04-11 00:17:43 +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.
|
|
|
|
|
2020-12-08 18:31:59 +01:00
|
|
|
"""Extractors for https://gelbooru.com/"""
|
2015-04-11 00:17:43 +02:00
|
|
|
|
2021-05-07 15:34:53 +02:00
|
|
|
from .common import Extractor, Message
|
2021-02-17 00:12:51 +01:00
|
|
|
from . import gelbooru_v02
|
2022-01-03 16:42:48 +01:00
|
|
|
from .. import text, util, exception
|
2021-05-07 15:34:53 +02:00
|
|
|
import binascii
|
2015-04-11 00:17:43 +02:00
|
|
|
|
2017-02-01 00:53:19 +01:00
|
|
|
|
2020-12-08 18:31:59 +01:00
|
|
|
class GelbooruBase():
|
2015-11-21 02:40:30 +01:00
|
|
|
"""Base class for gelbooru extractors"""
|
|
|
|
category = "gelbooru"
|
2021-02-17 00:12:51 +01:00
|
|
|
basecategory = "booru"
|
2020-12-08 18:31:59 +01:00
|
|
|
root = "https://gelbooru.com"
|
2018-07-03 20:54:37 +02:00
|
|
|
|
2022-01-03 16:42:48 +01:00
|
|
|
def _api_request(self, params):
|
|
|
|
url = self.root + "/index.php?page=dapi&s=post&q=index&json=1"
|
|
|
|
data = self.request(url, params=params).json()
|
|
|
|
if "post" not in data:
|
|
|
|
return ()
|
|
|
|
posts = data["post"]
|
|
|
|
if not isinstance(posts, list):
|
|
|
|
return (posts,)
|
|
|
|
return posts
|
|
|
|
|
|
|
|
def _pagination(self, params):
|
|
|
|
params["pid"] = self.page_start
|
|
|
|
params["limit"] = self.per_page
|
2022-01-26 19:43:14 +01:00
|
|
|
limit = self.per_page // 2
|
2022-01-03 16:42:48 +01:00
|
|
|
|
|
|
|
while True:
|
2022-01-26 19:43:14 +01:00
|
|
|
posts = self._api_request(params)
|
|
|
|
|
2022-01-03 16:42:48 +01:00
|
|
|
for post in posts:
|
|
|
|
yield post
|
|
|
|
|
2022-01-26 19:43:14 +01:00
|
|
|
if len(posts) < limit:
|
2022-01-03 16:42:48 +01:00
|
|
|
return
|
2022-01-26 19:43:14 +01:00
|
|
|
|
|
|
|
if "pid" in params:
|
|
|
|
del params["pid"]
|
|
|
|
params["tags"] = "{} id:<{}".format(self.tags, post["id"])
|
2022-01-03 16:42:48 +01:00
|
|
|
|
2020-12-24 01:04:44 +01:00
|
|
|
@staticmethod
|
|
|
|
def _file_url(post):
|
|
|
|
url = post["file_url"]
|
2022-01-12 21:33:02 +01:00
|
|
|
if url.endswith((".webm", ".mp4")):
|
2020-12-08 18:31:59 +01:00
|
|
|
md5 = post["md5"]
|
2021-03-10 01:48:07 +01:00
|
|
|
path = "/images/{}/{}/{}.webm".format(md5[0:2], md5[2:4], md5)
|
|
|
|
post["_fallback"] = GelbooruBase._video_fallback(path)
|
|
|
|
url = "https://img3.gelbooru.com" + path
|
2020-10-14 20:03:29 +02:00
|
|
|
return url
|
|
|
|
|
2021-03-10 01:48:07 +01:00
|
|
|
@staticmethod
|
|
|
|
def _video_fallback(path):
|
|
|
|
yield "https://img2.gelbooru.com" + path
|
|
|
|
yield "https://img1.gelbooru.com" + path
|
|
|
|
|
2017-11-29 20:48:17 +01:00
|
|
|
|
2021-02-17 00:12:51 +01:00
|
|
|
class GelbooruTagExtractor(GelbooruBase,
|
|
|
|
gelbooru_v02.GelbooruV02TagExtractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Extractor for images from gelbooru.com based on search-tags"""
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = (r"(?:https?://)?(?:www\.)?gelbooru\.com/(?:index\.php)?"
|
|
|
|
r"\?page=post&s=list&tags=(?P<tags>[^&#]+)")
|
|
|
|
test = (
|
2017-12-21 21:42:40 +01:00
|
|
|
("https://gelbooru.com/index.php?page=post&s=list&tags=bonocho", {
|
|
|
|
"count": 5,
|
|
|
|
}),
|
2022-01-26 19:43:14 +01:00
|
|
|
("https://gelbooru.com/index.php?page=post&s=list&tags=meiya_neon", {
|
|
|
|
"range": "196-204",
|
|
|
|
"url": "845a61aa1f90fb4ced841e8b7e62098be2e967bf",
|
|
|
|
"pattern": r"https://img\d\.gelbooru\.com"
|
|
|
|
r"/images/../../[0-9a-f]{32}\.jpg",
|
|
|
|
"count": 9,
|
2017-12-21 21:42:40 +01:00
|
|
|
}),
|
2019-02-08 13:45:40 +01:00
|
|
|
)
|
2015-11-21 02:40:30 +01:00
|
|
|
|
|
|
|
|
2021-02-17 00:12:51 +01:00
|
|
|
class GelbooruPoolExtractor(GelbooruBase,
|
|
|
|
gelbooru_v02.GelbooruV02PoolExtractor):
|
2017-11-29 20:48:17 +01:00
|
|
|
"""Extractor for image-pools from gelbooru.com"""
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = (r"(?:https?://)?(?:www\.)?gelbooru\.com/(?:index\.php)?"
|
|
|
|
r"\?page=pool&s=show&id=(?P<pool>\d+)")
|
2020-05-17 01:36:33 +02:00
|
|
|
test = (
|
|
|
|
("https://gelbooru.com/index.php?page=pool&s=show&id=761", {
|
|
|
|
"count": 6,
|
|
|
|
}),
|
|
|
|
("https://gelbooru.com/index.php?page=pool&s=show&id=761", {
|
|
|
|
"options": (("api", False),),
|
|
|
|
"count": 6,
|
|
|
|
}),
|
|
|
|
)
|
2017-11-29 20:48:17 +01:00
|
|
|
|
2020-12-08 18:31:59 +01:00
|
|
|
def metadata(self):
|
|
|
|
url = "{}/index.php?page=pool&s=show&id={}".format(
|
|
|
|
self.root, self.pool_id)
|
|
|
|
page = self.request(url).text
|
|
|
|
|
|
|
|
name, pos = text.extract(page, "<h3>Now Viewing: ", "</h3>")
|
|
|
|
if not name:
|
|
|
|
raise exception.NotFoundError("pool")
|
|
|
|
self.post_ids = text.extract_iter(page, 'class="" id="p', '"', pos)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"pool": text.parse_int(self.pool_id),
|
|
|
|
"pool_name": text.unescape(name),
|
|
|
|
}
|
|
|
|
|
2022-01-03 16:42:48 +01:00
|
|
|
def posts(self):
|
|
|
|
params = {}
|
|
|
|
for params["id"] in util.advance(self.post_ids, self.page_start):
|
|
|
|
yield from self._api_request(params)
|
|
|
|
|
2017-11-29 20:48:17 +01:00
|
|
|
|
2021-02-17 00:12:51 +01:00
|
|
|
class GelbooruPostExtractor(GelbooruBase,
|
|
|
|
gelbooru_v02.GelbooruV02PostExtractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Extractor for single images from gelbooru.com"""
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = (r"(?:https?://)?(?:www\.)?gelbooru\.com/(?:index\.php)?"
|
|
|
|
r"\?page=post&s=view&id=(?P<post>\d+)")
|
2021-03-10 01:48:07 +01:00
|
|
|
test = (
|
|
|
|
("https://gelbooru.com/index.php?page=post&s=view&id=313638", {
|
|
|
|
"content": "5e255713cbf0a8e0801dc423563c34d896bb9229",
|
|
|
|
"count": 1,
|
|
|
|
}),
|
2021-04-10 19:05:00 +02:00
|
|
|
("https://gelbooru.com/index.php?page=post&s=view&id=6018318", {
|
|
|
|
"options": (("tags", True),),
|
|
|
|
"content": "977caf22f27c72a5d07ea4d4d9719acdab810991",
|
|
|
|
"keyword": {
|
|
|
|
"tags_artist": "kirisaki_shuusei",
|
|
|
|
"tags_character": str,
|
|
|
|
"tags_copyright": "vocaloid",
|
|
|
|
"tags_general": str,
|
|
|
|
"tags_metadata": str,
|
|
|
|
},
|
|
|
|
}),
|
2021-03-10 01:48:07 +01:00
|
|
|
# video
|
|
|
|
("https://gelbooru.com/index.php?page=post&s=view&id=5938076", {
|
|
|
|
"content": "6360452fa8c2f0c1137749e81471238564df832a",
|
|
|
|
"pattern": r"https://img\d\.gelbooru\.com/images"
|
|
|
|
r"/22/61/226111273615049235b001b381707bd0\.webm",
|
|
|
|
}),
|
2021-04-13 23:40:24 +02:00
|
|
|
# notes
|
|
|
|
("https://gelbooru.com/index.php?page=post&s=view&id=5997331", {
|
|
|
|
"options": (("notes", True),),
|
|
|
|
"keywords": {
|
|
|
|
"notes": [
|
|
|
|
{
|
|
|
|
"height": 553,
|
|
|
|
"body": "Look over this way when you talk~",
|
|
|
|
"width": 246,
|
|
|
|
"x": 35,
|
|
|
|
"y": 72
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"height": 557,
|
|
|
|
"body": "Hey~\nAre you listening~?",
|
|
|
|
"width": 246,
|
|
|
|
"x": 1233,
|
|
|
|
"y": 109
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}),
|
2021-03-10 01:48:07 +01:00
|
|
|
)
|
2021-05-07 15:34:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
class GelbooruRedirectExtractor(GelbooruBase, Extractor):
|
|
|
|
subcategory = "redirect"
|
|
|
|
pattern = (r"(?:https?://)?(?:www\.)?gelbooru\.com"
|
|
|
|
r"/redirect\.php\?s=([^&#]+)")
|
|
|
|
test = (("https://gelbooru.com/redirect.php?s=Ly9nZWxib29ydS5jb20vaW5kZXgu"
|
|
|
|
"cGhwP3BhZ2U9cG9zdCZzPXZpZXcmaWQ9MTgzMDA0Ng=="), {
|
|
|
|
"pattern": r"https://gelbooru.com/index.php"
|
|
|
|
r"\?page=post&s=view&id=1830046"
|
|
|
|
})
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
Extractor.__init__(self, match)
|
|
|
|
self.redirect_url = text.ensure_http_scheme(
|
|
|
|
binascii.a2b_base64(match.group(1)).decode())
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
data = {"_extractor": GelbooruPostExtractor}
|
|
|
|
yield Message.Queue, self.redirect_url, data
|