From a27f43dad1c124b8c7ff3565723cc6000acc3368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sat, 28 Dec 2019 22:06:58 +0100 Subject: [PATCH] [pixiv] wait and retry after rate limit error (closes #535) --- gallery_dl/extractor/pixiv.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/gallery_dl/extractor/pixiv.py b/gallery_dl/extractor/pixiv.py index 8428e8f4f6..ce3d947670 100644 --- a/gallery_dl/extractor/pixiv.py +++ b/gallery_dl/extractor/pixiv.py @@ -13,6 +13,7 @@ from ..cache import cache from datetime import datetime, timedelta import hashlib +import time class PixivExtractor(Extractor): @@ -527,12 +528,20 @@ def _call(self, endpoint, params=None): self.login() response = self.extractor.request(url, params=params, fatal=False) + data = response.json() - if response.status_code < 400: - return response.json() - if response.status_code == 404: - raise exception.NotFoundError() - raise exception.StopExtraction("API request failed: %s", response.text) + if "error" in data: + if response.status_code == 404: + raise exception.NotFoundError() + + error = data["error"] + if "rate limit" in (error.get("message") or "").lower(): + self.log.info("Waiting two minutes for API rate limit reset.") + time.sleep(120) + return self._call(endpoint, params) + raise exception.StopExtraction("API request failed: %s", error) + + return data def _pagination(self, endpoint, params, key="illusts"): while True: