[inkbunny] improve error handling (#283)

pull/908/head
Mike Fährmann 4 years ago
parent baf5d0e3c1
commit 57f7d9b790
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -158,37 +158,31 @@ class InkbunnyAPI():
} }
self._call("userrating", params) self._call("userrating", params)
def authenticate(self): def authenticate(self, invalidate=False):
username, password = self.extractor._get_auth_info() username, password = self.extractor._get_auth_info()
if invalidate:
_authenticate_impl.invalidate(username or "guest")
if username: if username:
self.session_id = self._authenticate_impl(username, password) self.session_id = _authenticate_impl(self, username, password)
else: else:
self.session_id = self._authenticate_impl("guest", "") self.session_id = _authenticate_impl(self, "guest", "")
self.set_allowed_ratings() self.set_allowed_ratings()
@cache(maxage=360*24*3600, keyarg=1)
def _authenticate_impl(self, username, password):
self.extractor.log.info("Logging in as %s", username)
url = "https://inkbunny.net/api_login.php"
data = {"username": username, "password": password}
response = self.extractor.request(url, method="POST", data=data)
data = response.json()
if "sid" not in data:
raise exception.AuthenticationError(data.get("error_message"))
return data["sid"]
def _call(self, endpoint, params): def _call(self, endpoint, params):
if not self.session_id: if not self.session_id:
self.authenticate() self.authenticate()
url = "https://inkbunny.net/api_" + endpoint + ".php" url = "https://inkbunny.net/api_" + endpoint + ".php"
params["sid"] = self.session_id params["sid"] = self.session_id
response = self.extractor.request(url, params=params) data = self.extractor.request(url, params=params).json()
if "error_code" in data:
if str(data["error_code"]) == "2":
self.authenticate(invalidate=True)
return self._call(endpoint, params)
raise exception.StopExtraction(data.get("error_message"))
if response.status_code < 400: return data
return response.json()
def _pagination_search(self, params): def _pagination_search(self, params):
params["get_rid"] = "yes" params["get_rid"] = "yes"
@ -207,3 +201,16 @@ class InkbunnyAPI():
params["page"] = 2 params["page"] = 2
else: else:
params["page"] += 1 params["page"] += 1
@cache(maxage=360*24*3600, keyarg=1)
def _authenticate_impl(api, username, password):
api.extractor.log.info("Logging in as %s", username)
url = "https://inkbunny.net/api_login.php"
data = {"username": username, "password": password}
data = api.extractor.request(url, method="POST", data=data).json()
if "sid" not in data:
raise exception.AuthenticationError(data.get("error_message"))
return data["sid"]

Loading…
Cancel
Save