add-links/add_links/mediawiki_api.py
Edward Betts 95ca5f755d Fix User-Agent header, timeouts, and JSON error handling
mediawiki_oauth: set User-Agent on all OAuth1Session instances so
Wikimedia doesn't reject token and API requests with 403; add timeout
parameter to api_post_request (default 4s).

mediawiki_api: add APIError exception; wrap .json() in call() to raise
APIError with status code and response body on decode failure; raise
timeout to 30s for edit POSTs.

api: wrap call_get_diff .json() with the same JSONDecodeError guard,
raising MediawikiError with HTTP status and body on failure.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-09 18:11:23 +01:00

110 lines
2.9 KiB
Python

"""Interface with the mediawiki API."""
import typing
from pprint import pprint
from typing import Any, cast
import requests
from . import mediawiki_oauth
class APIError(Exception):
"""Unexpected response from the MediaWiki API."""
wiki_hostname = "en.wikipedia.org"
wiki_api_php = f"https://{wiki_hostname}/w/api.php"
user_agent = "add-links/0.1"
def parse_page(enwiki: str) -> dict[str, Any]:
"""Call mediawiki parse API for given article."""
params: dict[str, str | int] = {
"action": "parse",
"format": "json",
"formatversion": 2,
"disableeditsection": 1,
"page": enwiki,
"prop": "text|links|headhtml",
"disabletoc": 1,
}
parse: dict[str, Any] = call(params)["parse"]
return parse
def call(params: dict[str, str | int], timeout: int = 4) -> dict[str, typing.Any]:
"""Make GET request to mediawiki API."""
r = mediawiki_oauth.api_post_request(params, timeout=timeout)
try:
return cast(dict[str, Any], r.json())
except requests.exceptions.JSONDecodeError:
raise APIError(f"HTTP {r.status_code}: {r.text[:200]!r}")
def article_exists(title: str) -> bool:
"""Get article text."""
params: dict[str, str | int] = {
"action": "query",
"format": "json",
"formatversion": 2,
"titles": title,
}
return not call(params)["query"]["pages"][0].get("missing")
def get_content(title: str) -> tuple[str, int]:
"""Get article text."""
params: dict[str, str | int] = {
"action": "query",
"format": "json",
"formatversion": 2,
"prop": "revisions|info",
"rvprop": "content|timestamp|ids",
"titles": title,
}
data = call(params)
rev = data["query"]["pages"][0]["revisions"][0]
content: str = rev["content"]
revid: int = int(rev["revid"])
return content, revid
def compare(title: str, new_text: str) -> str:
"""Generate a diff for the new article text."""
params: dict[str, str | int] = {
"format": "json",
"formatversion": 2,
"action": "compare",
"fromtitle": title,
"toslots": "main",
"totext-main": new_text,
"prop": "diff",
}
diff: str = call(params)["compare"]["body"]
return diff
def edit_page(
pageid: int, section: str | int, text: str, summary: str, baserevid: str, token: str
) -> str:
"""Edit a page on Wikipedia."""
params: dict[str, str | int] = {
"format": "json",
"formatversion": 2,
"action": "edit",
"pageid": pageid,
"text": text,
"baserevid": baserevid,
"token": token,
"nocreate": 1,
"summary": summary,
"section": section,
}
ret = call(params, timeout=30)
if "edit" not in ret:
print("params")
pprint(params)
print()
pprint(ret)
return typing.cast(str, ret["edit"])