[danbooru] reduce API requests for fetching extended 'metadata'
Instead of using one additional API request per post object (N+1), this requires only one request per 200-post batch.
This commit is contained in:
@@ -1148,7 +1148,7 @@ Description
|
|||||||
See `available_includes <https://github.com/danbooru/danbooru/blob/2cf7baaf6c5003c1a174a8f2d53db010cf05dca7/app/models/post.rb#L1842-L1849>`__
|
See `available_includes <https://github.com/danbooru/danbooru/blob/2cf7baaf6c5003c1a174a8f2d53db010cf05dca7/app/models/post.rb#L1842-L1849>`__
|
||||||
for possible field names. ``aibooru`` also supports ``ai_metadata``.
|
for possible field names. ``aibooru`` also supports ``ai_metadata``.
|
||||||
|
|
||||||
Note: This requires 1 additional HTTP request per post.
|
Note: This requires 1 additional HTTP request per 200-post batch.
|
||||||
|
|
||||||
|
|
||||||
extractor.{Danbooru].threshold
|
extractor.{Danbooru].threshold
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ class DanbooruExtractor(BaseExtractor):
|
|||||||
BaseExtractor.__init__(self, match)
|
BaseExtractor.__init__(self, match)
|
||||||
self.ugoira = self.config("ugoira", False)
|
self.ugoira = self.config("ugoira", False)
|
||||||
self.external = self.config("external", False)
|
self.external = self.config("external", False)
|
||||||
|
self.includes = False
|
||||||
|
|
||||||
threshold = self.config("threshold")
|
threshold = self.config("threshold")
|
||||||
if isinstance(threshold, int):
|
if isinstance(threshold, int):
|
||||||
@@ -54,6 +55,7 @@ class DanbooruExtractor(BaseExtractor):
|
|||||||
includes = ",".join(includes)
|
includes = ",".join(includes)
|
||||||
elif not isinstance(includes, str):
|
elif not isinstance(includes, str):
|
||||||
includes = "artist_commentary,children,notes,parent,uploader"
|
includes = "artist_commentary,children,notes,parent,uploader"
|
||||||
|
self.includes = includes + ",id"
|
||||||
|
|
||||||
data = self.metadata()
|
data = self.metadata()
|
||||||
for post in self.posts():
|
for post in self.posts():
|
||||||
@@ -77,11 +79,6 @@ class DanbooruExtractor(BaseExtractor):
|
|||||||
url = post["large_file_url"]
|
url = post["large_file_url"]
|
||||||
post["extension"] = "webm"
|
post["extension"] = "webm"
|
||||||
|
|
||||||
if includes:
|
|
||||||
meta_url = "{}/posts/{}.json?only={}".format(
|
|
||||||
self.root, post["id"], includes)
|
|
||||||
post.update(self.request(meta_url).json())
|
|
||||||
|
|
||||||
if url[0] == "/":
|
if url[0] == "/":
|
||||||
url = self.root + url
|
url = self.root + url
|
||||||
|
|
||||||
@@ -104,6 +101,19 @@ class DanbooruExtractor(BaseExtractor):
|
|||||||
posts = self.request(url, params=params).json()
|
posts = self.request(url, params=params).json()
|
||||||
if "posts" in posts:
|
if "posts" in posts:
|
||||||
posts = posts["posts"]
|
posts = posts["posts"]
|
||||||
|
|
||||||
|
if self.includes and posts:
|
||||||
|
if not pages and "only" not in params:
|
||||||
|
params["page"] = "b{}".format(posts[0]["id"] + 1)
|
||||||
|
params["only"] = self.includes
|
||||||
|
data = {
|
||||||
|
meta["id"]: meta
|
||||||
|
for meta in self.request(url, params=params).json()
|
||||||
|
}
|
||||||
|
for post in posts:
|
||||||
|
post.update(data[post["id"]])
|
||||||
|
params["only"] = None
|
||||||
|
|
||||||
yield from posts
|
yield from posts
|
||||||
|
|
||||||
if len(posts) < self.threshold:
|
if len(posts) < self.threshold:
|
||||||
@@ -255,7 +265,11 @@ class DanbooruPostExtractor(DanbooruExtractor):
|
|||||||
|
|
||||||
def posts(self):
|
def posts(self):
|
||||||
url = "{}/posts/{}.json".format(self.root, self.post_id)
|
url = "{}/posts/{}.json".format(self.root, self.post_id)
|
||||||
return (self.request(url).json(),)
|
post = self.request(url).json()
|
||||||
|
if self.includes:
|
||||||
|
params = {"only": self.includes}
|
||||||
|
post.update(self.request(url, params=params).json())
|
||||||
|
return (post,)
|
||||||
|
|
||||||
|
|
||||||
class DanbooruPopularExtractor(DanbooruExtractor):
|
class DanbooruPopularExtractor(DanbooruExtractor):
|
||||||
|
|||||||
Reference in New Issue
Block a user