Skip to content

Commit

Permalink
[gelbooru_v02] use total number of posts as end marker (#5830)
Browse files Browse the repository at this point in the history
… and potentially retry on empty responses
  • Loading branch information
mikf committed Jul 12, 2024
1 parent 6110e3f commit 51fd14f
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions gallery_dl/extractor/gelbooru_v02.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def _pagination(self, params):
params["pid"] = self.page_start
params["limit"] = self.per_page

post = None
post = total = None
count = 0

while True:
try:
root = self._api_request(params)
Expand All @@ -50,12 +52,29 @@ def _pagination(self, params):
params["pid"] = 0
continue

if total is None:
try:
total = int(root.attrib["count"])
self.log.debug("%s posts in total", total)
except Exception as exc:
total = 0
self.log.debug(
"Failed to get total number of posts (%s: %s)",
exc.__class__.__name__, exc)

post = None
for post in root:
yield post.attrib

if len(root) < self.per_page:
return
num = len(root)
count += num
if num < self.per_page:
if not total or count >= total:
return
if not num:
self.log.debug("Empty response - Retrying")
continue

params["pid"] += 1

def _pagination_html(self, params):
Expand Down

0 comments on commit 51fd14f

Please sign in to comment.