1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 18:53:21 +01:00

[gelbooru] detect returned favorites order (#5220)

This commit is contained in:
Mike Fährmann 2024-03-18 20:40:02 +01:00
parent e1c51c0dfb
commit 0d69af94d5
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

View File

@ -32,6 +32,9 @@ class GelbooruBase():
url = self.root + "/index.php?page=dapi&q=index&json=1"
data = self.request(url, params=params).json()
if not key:
return data
try:
posts = data[key]
except KeyError:
@ -167,13 +170,54 @@ class GelbooruFavoriteExtractor(GelbooruBase,
params = {
"s" : "favorite",
"id" : self.favorite_id,
"limit": "1",
"limit": "2",
}
count = self._api_request(params, "@attributes", True)[0]["count"]
if count <= self.offset:
return
data = self._api_request(params, None, True)
count = data["@attributes"]["count"]
if count <= self.offset:
return ()
favs = data["favorite"]
try:
order = 1 if favs[0]["id"] < favs[1]["id"] else -1
except LookupError:
order = 0
if order > 0:
self.log.debug("API yields favorites in ascending order")
self.log.debug("Returning them in reverse")
return self._pagination_reverse(params, count)
self.log.debug("API yields favorites in descending order")
return self._pagination(params, count)
def _pagination(self, params, count):
if self.offset:
pnum, skip = divmod(self.offset, self.per_page)
else:
pnum = skip = 0
params["pid"] = pnum
params["limit"] = self.per_page
while True:
favs = self._api_request(params, "favorite", True)
if not favs:
return
if skip:
favs = favs[skip:]
skip = 0
for fav in favs:
yield from self._api_request({"id": fav["favorite"]})
params["pid"] += 1
def _pagination_reverse(self, params, count):
pnum, last = divmod(count-1, self.per_page)
if self.offset > last:
# page number change
@ -182,7 +226,6 @@ class GelbooruFavoriteExtractor(GelbooruBase,
pnum -= diff + 1
skip = self.offset
# paginate over them in reverse
params["pid"] = pnum
params["limit"] = self.per_page