From 257e9fb435ee3f20c0fe87ced03a9c9e69231226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 15 Apr 2024 17:55:19 +0200 Subject: [PATCH] [gelbooru] improve pagination logic for meta tags (#5478) similar to 494acabd38fefdc8c1b482b584695aaa788f9112 --- gallery_dl/extractor/gelbooru.py | 35 +++++++++++++++++++++++++++----- test/results/gelbooru.py | 16 +++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/gallery_dl/extractor/gelbooru.py b/gallery_dl/extractor/gelbooru.py index 2459a61f..37c776e6 100644 --- a/gallery_dl/extractor/gelbooru.py +++ b/gallery_dl/extractor/gelbooru.py @@ -51,19 +51,44 @@ class GelbooruBase(): params["pid"] = self.page_start params["limit"] = self.per_page limit = self.per_page // 2 + pid = False + + if "tags" in params: + tags = params["tags"].split() + op = "<" + id = False + + for tag in tags: + if tag.startswith("sort:"): + if tag == "sort:id:asc": + op = ">" + elif tag == "sort:id" or tag.startswith("sort:id:"): + op = "<" + else: + pid = True + elif tag.startswith("id:"): + id = True + + if not pid: + if id: + tag = "id:" + op + tags = [t for t in tags if not t.startswith(tag)] + tags = "{} id:{}".format(" ".join(tags), op) while True: posts = self._api_request(params) - for post in posts: - yield post + yield from posts if len(posts) < limit: return - if "pid" in params: - del params["pid"] - params["tags"] = "{} id:<{}".format(self.tags, post["id"]) + if pid: + params["pid"] += 1 + else: + if "pid" in params: + del params["pid"] + params["tags"] = tags + str(posts[-1]["id"]) def _pagination_html(self, params): url = self.root + "/index.php" diff --git a/test/results/gelbooru.py b/test/results/gelbooru.py index 6302d56f..3f09ea69 100644 --- a/test/results/gelbooru.py +++ b/test/results/gelbooru.py @@ -39,6 +39,22 @@ __tests__ = ( "#sha1_url": "845a61aa1f90fb4ced841e8b7e62098be2e967bf", }, +{ + "#url" : "https://gelbooru.com/index.php?page=post&s=list&tags=id:>=67800+id:<=68000", + "#comment" : "meta tags (#5478)", + "#category": ("booru", "gelbooru", "tag"), + "#class" : gelbooru.GelbooruTagExtractor, + "#count" : 187, +}, + +{ + "#url" : "https://gelbooru.com/index.php?page=post&s=list&tags=id:>=67800+id:<=68000+sort:id:asc", + "#comment" : "meta + sort tags (#5478)", + "#category": ("booru", "gelbooru", "tag"), + "#class" : gelbooru.GelbooruTagExtractor, + "#count" : 187, +}, + { "#url" : "https://gelbooru.com/index.php?page=pool&s=show&id=761", "#category": ("booru", "gelbooru", "pool"),