dab-mechanic/dab_mechanic/mediawiki_api.py

92 lines
2.4 KiB
Python
Raw Normal View History

2022-08-17 14:38:30 +01:00
"""Interface with the mediawiki API."""
from typing import Any
from . import wikidata_oauth
2022-08-17 14:38:30 +01:00
wiki_hostname = "en.wikipedia.org"
wiki_api_php = f"https://{wiki_hostname}/w/api.php"
user_agent = "dab-mechanic/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"]
2022-08-17 14:38:30 +01:00
return parse
def call(params: dict[str, str | int]) -> dict[str, Any]:
2022-08-17 14:38:30 +01:00
"""Make GET request to mediawiki API."""
data: dict[str, Any] = wikidata_oauth.api_post_request(params)
return data.json()
2022-08-17 14:38:30 +01:00
2023-09-29 14:17:56 +01:00
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]:
2022-08-17 14:38:30 +01:00
"""Get article text."""
params: dict[str, str | int] = {
"action": "query",
"format": "json",
"formatversion": 2,
"prop": "revisions|info",
2023-09-29 14:17:56 +01:00
"rvprop": "content|timestamp|ids",
2022-08-17 14:38:30 +01:00
"titles": title,
}
data = call(params)
2023-09-29 14:17:56 +01:00
rev = data["query"]["pages"][0]["revisions"][0]
content: str = rev["content"]
revid: int = int(rev["revid"])
return content, revid
2022-08-21 11:30:02 +01:00
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
2023-09-29 14:17:56 +01:00
def edit_page(
title: str, text: str, summary: str, baserevid: str, token: str
) -> dict[str, str | int]:
"""Edit a page on Wikipedia."""
params: dict[str, str | int] = {
"format": "json",
"formatversion": 2,
"action": "edit",
"title": title,
"text": text,
"baserevid": baserevid,
"token": token,
"summary": summary,
}
edit: str = call(params)["edit"]
return edit