[pixiv] wait and retry after rate limit error (closes #535)

This commit is contained in:
Mike Fährmann
2019-12-28 22:06:58 +01:00
parent 6b373cb7e2
commit a27f43dad1

View File

@@ -13,6 +13,7 @@ from .. import text, exception
from ..cache import cache from ..cache import cache
from datetime import datetime, timedelta from datetime import datetime, timedelta
import hashlib import hashlib
import time
class PixivExtractor(Extractor): class PixivExtractor(Extractor):
@@ -527,12 +528,20 @@ class PixivAppAPI():
self.login() self.login()
response = self.extractor.request(url, params=params, fatal=False) response = self.extractor.request(url, params=params, fatal=False)
data = response.json()
if response.status_code < 400: if "error" in data:
return response.json() if response.status_code == 404:
if response.status_code == 404: raise exception.NotFoundError()
raise exception.NotFoundError()
raise exception.StopExtraction("API request failed: %s", response.text) 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"): def _pagination(self, endpoint, params, key="illusts"):
while True: while True: