From c3c250134dfef691bc48b8701d092595afa84577 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 29 Sep 2023 20:12:13 +0100 Subject: [PATCH] Pull in some code from one-at-a-time branch --- dab_mechanic/mediawiki_api.py | 34 ++++++++++++++++++++++++++++++++++ dab_mechanic/wikidata_oauth.py | 14 +++++++++----- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/dab_mechanic/mediawiki_api.py b/dab_mechanic/mediawiki_api.py index 26d7a20..d26e60d 100644 --- a/dab_mechanic/mediawiki_api.py +++ b/dab_mechanic/mediawiki_api.py @@ -1,6 +1,7 @@ """Interface with the mediawiki API.""" from typing import Any + from . import wikidata_oauth wiki_hostname = "en.wikipedia.org" @@ -43,3 +44,36 @@ def get_content(title: str) -> str: data = call(params) rev: str = data["query"]["pages"][0]["revisions"][0]["content"] 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 diff --git a/dab_mechanic/wikidata_oauth.py b/dab_mechanic/wikidata_oauth.py index bafb54e..a872da1 100644 --- a/dab_mechanic/wikidata_oauth.py +++ b/dab_mechanic/wikidata_oauth.py @@ -7,8 +7,10 @@ from flask import current_app, session from requests.models import Response from requests_oauthlib import OAuth1Session -wiki_hostname = "en.wikipedia.org" -api_url = f"https://{wiki_hostname}/w/api.php" +WIKI_HOSTNAME = "en.wikipedia.org" +API_URL = f"https://{WIKI_HOSTNAME}/w/api.php" + +TIMEOUT = 20 CallParams = dict[str, str | int] @@ -33,14 +35,15 @@ def api_post_request(params: CallParams) -> Response: resource_owner_key=session["owner_key"], 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 def raw_request(params: CallParams) -> Response: """Raw request.""" app = current_app - url = api_url + "?" + urlencode(params) + url = API_URL + "?" + urlencode(params) client_key = app.config["CLIENT_KEY"] client_secret = app.config["CLIENT_SECRET"] oauth = OAuth1Session( @@ -49,7 +52,8 @@ def raw_request(params: CallParams) -> Response: resource_owner_key=session["owner_key"], 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