Compare commits
1 commit
one-at-a-t
...
main
Author | SHA1 | Date | |
---|---|---|---|
Edward Betts | c3c250134d |
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue