[gelbooru] add 'order-posts' option for favorites (#5220)

This commit is contained in:
Mike Fährmann
2024-03-23 13:30:09 +01:00
parent 55e8fdad29
commit 31e7ca73b6
3 changed files with 53 additions and 14 deletions

View File

@@ -172,26 +172,31 @@ class GelbooruFavoriteExtractor(GelbooruBase,
"id" : self.favorite_id,
"limit": "2",
}
data = self._api_request(params, None, True)
count = data["@attributes"]["count"]
if count <= self.offset:
return ()
self.log.debug("API reports %s favorite entries", count)
favs = data["favorite"]
try:
order = 1 if favs[0]["id"] < favs[1]["id"] else -1
except LookupError:
order = 0
except LookupError as exc:
self.log.debug(
"Error when determining API favorite order (%s: %s)",
exc.__class__.__name__, exc)
order = -1
else:
self.log.debug("API yields favorites in %sscending order",
"a" if order > 0 else "de")
if order > 0:
self.log.debug("API yields favorites in ascending order")
order_favs = self.config("order-posts")
if order_favs and order_favs[0] in ("r", "a"):
self.log.debug("Returning them in reverse")
return self._pagination_reverse(params, count)
order = -order
self.log.debug("API yields favorites in descending order")
return self._pagination(params, count)
if order < 0:
return self._pagination(params, count)
return self._pagination_reverse(params, count)
def _pagination(self, params, count):
if self.offset:
@@ -203,7 +208,7 @@ class GelbooruFavoriteExtractor(GelbooruBase,
params["limit"] = self.per_page
while True:
favs = self._api_request(params, "favorite", True)
favs = self._api_request(params, "favorite")
if not favs:
return
@@ -232,7 +237,7 @@ class GelbooruFavoriteExtractor(GelbooruBase,
params["limit"] = self.per_page
while True:
favs = self._api_request(params, "favorite", True)
favs = self._api_request(params, "favorite")
favs.reverse()
if skip: