Handle Wikipedia category API failures
This commit is contained in:
parent
3de78c75c5
commit
b7ba24b567
3 changed files with 102 additions and 1 deletions
16
main.py
16
main.py
|
|
@ -354,6 +354,7 @@ class CategoryResult:
|
|||
|
||||
articles: list[ArticleWithoutImage]
|
||||
gcmcontinue: str | None
|
||||
error: str | None = None
|
||||
|
||||
|
||||
# Common non-content images to ignore when checking if an article has images
|
||||
|
|
@ -435,6 +436,7 @@ def get_articles_without_images(
|
|||
articles_without_images: list[ArticleWithoutImage] = []
|
||||
seen_pageids: set[int] = set()
|
||||
next_gcmcontinue: str | None = None
|
||||
error: str | None = None
|
||||
|
||||
# Build initial continue params from the external pagination token
|
||||
continue_params: dict[str, str] = {}
|
||||
|
|
@ -451,8 +453,20 @@ def get_articles_without_images(
|
|||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if api_error := data.get("error"):
|
||||
raise requests.RequestException(
|
||||
f"{api_error.get('code', 'unknown')}: "
|
||||
f"{api_error.get('info', 'unknown Wikipedia API error')}"
|
||||
)
|
||||
except (requests.RequestException, json.JSONDecodeError) as e:
|
||||
print(f"Wikipedia API error: {e}")
|
||||
if articles_without_images:
|
||||
error = (
|
||||
"Wikipedia stopped responding before the whole category could "
|
||||
"be checked. Showing partial results; please try again."
|
||||
)
|
||||
else:
|
||||
error = "Unable to load articles from Wikipedia. Please try again."
|
||||
break
|
||||
|
||||
pages = data.get("query", {}).get("pages", {})
|
||||
|
|
@ -491,6 +505,7 @@ def get_articles_without_images(
|
|||
return CategoryResult(
|
||||
articles=articles_without_images,
|
||||
gcmcontinue=next_gcmcontinue,
|
||||
error=error,
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -891,6 +906,7 @@ def category_search() -> str:
|
|||
category_name=category_name,
|
||||
articles=result.articles,
|
||||
gcmcontinue=result.gcmcontinue,
|
||||
error=result.error,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue