Handle non-JSON API responses gracefully
Some boorus return empty/HTML responses for tag-limited queries. All API clients now catch JSON parse errors and return empty results instead of crashing.
This commit is contained in:
parent
c93cd9b97c
commit
ee329519de
@ -29,7 +29,10 @@ class DanbooruClient(BooruClient):
|
|||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
log.warning(f" body: {resp.text[:500]}")
|
log.warning(f" body: {resp.text[:500]}")
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
data = resp.json()
|
try:
|
||||||
|
data = resp.json()
|
||||||
|
except Exception:
|
||||||
|
return []
|
||||||
|
|
||||||
# Some Danbooru forks wrap in {"posts": [...]}
|
# Some Danbooru forks wrap in {"posts": [...]}
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
|
|||||||
@ -49,7 +49,10 @@ class E621Client(BooruClient):
|
|||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
log.warning(f" body: {resp.text[:500]}")
|
log.warning(f" body: {resp.text[:500]}")
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
data = resp.json()
|
try:
|
||||||
|
data = resp.json()
|
||||||
|
except Exception:
|
||||||
|
return []
|
||||||
|
|
||||||
# e621 wraps posts in {"posts": [...]}
|
# e621 wraps posts in {"posts": [...]}
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
|
|||||||
@ -44,7 +44,11 @@ class GelbooruClient(BooruClient):
|
|||||||
log.warning(f" body: {resp.text[:500]}")
|
log.warning(f" body: {resp.text[:500]}")
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
|
|
||||||
data = resp.json()
|
try:
|
||||||
|
data = resp.json()
|
||||||
|
except Exception:
|
||||||
|
log.warning(f" non-JSON response: {resp.text[:200]}")
|
||||||
|
return []
|
||||||
log.debug(f" json type: {type(data).__name__}, keys: {list(data.keys()) if isinstance(data, dict) else f'list[{len(data)}]'}")
|
log.debug(f" json type: {type(data).__name__}, keys: {list(data.keys()) if isinstance(data, dict) else f'list[{len(data)}]'}")
|
||||||
# Gelbooru wraps posts in {"post": [...]} or returns {"post": []}
|
# Gelbooru wraps posts in {"post": [...]} or returns {"post": []}
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
|
|||||||
@ -23,7 +23,10 @@ class MoebooruClient(BooruClient):
|
|||||||
|
|
||||||
resp = await self.client.get(f"{self.base_url}/post.json", params=params)
|
resp = await self.client.get(f"{self.base_url}/post.json", params=params)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
data = resp.json()
|
try:
|
||||||
|
data = resp.json()
|
||||||
|
except Exception:
|
||||||
|
return []
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
data = data.get("posts", data.get("post", []))
|
data = data.get("posts", data.get("post", []))
|
||||||
if not isinstance(data, list):
|
if not isinstance(data, list):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user