Pull in some code from one-at-a-time branch

This commit is contained in:
Edward Betts 2023-09-29 20:12:13 +01:00
parent 0ff82e1c05
commit c3c250134d
2 changed files with 43 additions and 5 deletions

View file

@ -1,6 +1,7 @@
"""Interface with the mediawiki API.""" """Interface with the mediawiki API."""
from typing import Any from typing import Any
from . import wikidata_oauth from . import wikidata_oauth
wiki_hostname = "en.wikipedia.org" wiki_hostname = "en.wikipedia.org"
@ -43,3 +44,36 @@ def get_content(title: str) -> str:
data = call(params) data = call(params)
rev: str = data["query"]["pages"][0]["revisions"][0]["content"] rev: str = data["query"]["pages"][0]["revisions"][0]["content"]
return rev return rev
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(
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

View file

@ -7,8 +7,10 @@ from flask import current_app, session
from requests.models import Response from requests.models import Response
from requests_oauthlib import OAuth1Session from requests_oauthlib import OAuth1Session
wiki_hostname = "en.wikipedia.org" WIKI_HOSTNAME = "en.wikipedia.org"
api_url = f"https://{wiki_hostname}/w/api.php" API_URL = f"https://{WIKI_HOSTNAME}/w/api.php"
TIMEOUT = 20
CallParams = dict[str, str | int] CallParams = dict[str, str | int]
@ -33,14 +35,15 @@ def api_post_request(params: CallParams) -> Response:
resource_owner_key=session["owner_key"], resource_owner_key=session["owner_key"],
resource_owner_secret=session["owner_secret"], resource_owner_secret=session["owner_secret"],
) )
r: Response = oauth.post(api_url, data=params, timeout=10, proxies=get_edit_proxy()) proxies = get_edit_proxy()
r: Response = oauth.post(API_URL, data=params, timeout=TIMEOUT, proxies=proxies)
return r return r
def raw_request(params: CallParams) -> Response: def raw_request(params: CallParams) -> Response:
"""Raw request.""" """Raw request."""
app = current_app app = current_app
url = api_url + "?" + urlencode(params) url = API_URL + "?" + urlencode(params)
client_key = app.config["CLIENT_KEY"] client_key = app.config["CLIENT_KEY"]
client_secret = app.config["CLIENT_SECRET"] client_secret = app.config["CLIENT_SECRET"]
oauth = OAuth1Session( oauth = OAuth1Session(
@ -49,7 +52,8 @@ def raw_request(params: CallParams) -> Response:
resource_owner_key=session["owner_key"], resource_owner_key=session["owner_key"],
resource_owner_secret=session["owner_secret"], resource_owner_secret=session["owner_secret"],
) )
r: Response = oauth.get(url, timeout=10, proxies=get_edit_proxy()) proxies = get_edit_proxy()
r: Response = oauth.get(url, timeout=TIMEOUT, proxies=proxies)
return r return r