[kemonoparty] fix login / update favorites extractor (#6415)

This commit is contained in:
Mike Fährmann
2024-11-28 14:41:16 +01:00
parent e1aa4a7162
commit 7c7b8a25c3
2 changed files with 22 additions and 28 deletions

View File

@@ -158,20 +158,23 @@ class KemonopartyExtractor(Extractor):
self.cookies_update(self._login_impl(
(username, self.cookies_domain), password))
@cache(maxage=28*86400, keyarg=1)
@cache(maxage=3650*86400, keyarg=1)
def _login_impl(self, username, password):
username = username[0]
self.log.info("Logging in as %s", username)
url = self.root + "/account/login"
url = self.root + "/api/v1/authentication/login"
data = {"username": username, "password": password}
response = self.request(url, method="POST", data=data)
if response.url.endswith("/account/login") and \
"Username or password is incorrect" in response.text:
raise exception.AuthenticationError()
response = self.request(url, method="POST", json=data, fatal=False)
if response.status_code >= 400:
try:
msg = '"' + response.json()["error"] + '"'
except Exception:
msg = '"0/1 Username or password is incorrect"'
raise exception.AuthenticationError(msg)
return {c.name: c.value for c in response.history[0].cookies}
return {c.name: c.value for c in response.cookies}
def _file(self, post):
file = post["file"]
@@ -430,26 +433,21 @@ class KemonopartyDiscordServerExtractor(KemonopartyExtractor):
class KemonopartyFavoriteExtractor(KemonopartyExtractor):
"""Extractor for kemono.su favorites"""
subcategory = "favorite"
pattern = BASE_PATTERN + r"/favorites(?:/?\?([^#]+))?"
pattern = BASE_PATTERN + r"/favorites()()(?:/?\?([^#]+))?"
example = "https://kemono.su/favorites"
def __init__(self, match):
KemonopartyExtractor.__init__(self, match)
self.params = text.parse_query(match.group(3))
self.favorites = (self.params.get("type") or
self.config("favorites") or
"artist")
def items(self):
self._prepare_ddosguard_cookies()
self.login()
sort = self.params.get("sort")
order = self.params.get("order") or "desc"
params = text.parse_query(self.groups[4])
type = params.get("type") or self.config("favorites") or "artist"
if self.favorites == "artist":
users = self.request(
self.root + "/api/v1/account/favorites?type=artist").json()
sort = params.get("sort")
order = params.get("order") or "desc"
if type == "artist":
users = self.api.account_favorites("artist")
if not sort:
sort = "updated"
@@ -462,9 +460,8 @@ class KemonopartyFavoriteExtractor(KemonopartyExtractor):
self.root, user["service"], user["id"])
yield Message.Queue, url, user
elif self.favorites == "post":
posts = self.request(
self.root + "/api/v1/account/favorites?type=post").json()
elif type == "post":
posts = self.api.account_favorites("post")
if not sort:
sort = "faved_seq"
@@ -549,11 +546,6 @@ class KemonoAPI():
params = {"type": type}
return self._call(endpoint, params)
def authentication_login(self, username, password):
endpoint = "/authentication/login"
params = {"username": username, "password": password}
return self._call(endpoint, params)
def _call(self, endpoint, params=None):
url = self.root + endpoint
response = self.extractor.request(url, params=params)

View File

@@ -399,6 +399,7 @@ __tests__ = (
"https://kemono.su/patreon/user/881792",
"https://kemono.su/fanbox/user/6993449",
"https://kemono.su/subscribestar/user/alcorart",
"https://kemono.su/gumroad/user/shengtian",
),
},
@@ -412,6 +413,7 @@ __tests__ = (
"https://kemono.su/fanbox/user/6993449",
"https://kemono.su/patreon/user/881792",
"https://kemono.su/subscribestar/user/alcorart",
"https://kemono.su/gumroad/user/shengtian",
),
},