2023-09-29 17:52:06 +01:00
|
|
|
"""Wikidata OAuth."""
|
|
|
|
|
|
|
|
import typing
|
2022-08-15 21:10:14 +01:00
|
|
|
from urllib.parse import urlencode
|
|
|
|
|
|
|
|
from flask import current_app, session
|
2023-09-29 17:52:06 +01:00
|
|
|
from requests.models import Response
|
2022-08-15 21:10:14 +01:00
|
|
|
from requests_oauthlib import OAuth1Session
|
|
|
|
|
2022-08-18 20:49:22 +01:00
|
|
|
WIKI_HOSTNAME = "en.wikipedia.org"
|
|
|
|
API_URL = f"https://{WIKI_HOSTNAME}/w/api.php"
|
|
|
|
|
|
|
|
TIMEOUT = 20
|
2022-08-15 21:10:14 +01:00
|
|
|
|
2023-09-29 17:52:06 +01:00
|
|
|
CallParams = dict[str, str | int]
|
|
|
|
|
2022-08-15 21:10:14 +01:00
|
|
|
|
|
|
|
def get_edit_proxy() -> dict[str, str]:
|
|
|
|
"""Retrieve proxy information from config."""
|
|
|
|
edit_proxy = current_app.config.get("EDIT_PROXY")
|
|
|
|
if edit_proxy:
|
|
|
|
return {"http": edit_proxy, "https": edit_proxy}
|
|
|
|
else:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2023-09-29 17:52:06 +01:00
|
|
|
def api_post_request(params: CallParams) -> Response:
|
2022-08-15 21:10:14 +01:00
|
|
|
"""HTTP Post using Oauth."""
|
|
|
|
app = current_app
|
|
|
|
client_key = app.config["CLIENT_KEY"]
|
|
|
|
client_secret = app.config["CLIENT_SECRET"]
|
|
|
|
oauth = OAuth1Session(
|
|
|
|
client_key,
|
|
|
|
client_secret=client_secret,
|
|
|
|
resource_owner_key=session["owner_key"],
|
|
|
|
resource_owner_secret=session["owner_secret"],
|
|
|
|
)
|
|
|
|
proxies = get_edit_proxy()
|
2023-09-29 18:06:48 +01:00
|
|
|
r: Response = oauth.post(API_URL, data=params, timeout=TIMEOUT, proxies=proxies)
|
2023-09-29 17:52:06 +01:00
|
|
|
return r
|
2022-08-15 21:10:14 +01:00
|
|
|
|
|
|
|
|
2023-09-29 17:52:06 +01:00
|
|
|
def raw_request(params: CallParams) -> Response:
|
|
|
|
"""Raw request."""
|
2022-08-15 21:10:14 +01:00
|
|
|
app = current_app
|
2022-08-18 20:49:22 +01:00
|
|
|
url = API_URL + "?" + urlencode(params)
|
2022-08-15 21:10:14 +01:00
|
|
|
client_key = app.config["CLIENT_KEY"]
|
|
|
|
client_secret = app.config["CLIENT_SECRET"]
|
|
|
|
oauth = OAuth1Session(
|
|
|
|
client_key,
|
|
|
|
client_secret=client_secret,
|
|
|
|
resource_owner_key=session["owner_key"],
|
|
|
|
resource_owner_secret=session["owner_secret"],
|
|
|
|
)
|
|
|
|
proxies = get_edit_proxy()
|
2023-09-29 18:06:48 +01:00
|
|
|
r: Response = oauth.get(url, timeout=TIMEOUT, proxies=proxies)
|
2023-09-29 17:52:06 +01:00
|
|
|
return r
|
2022-08-15 21:10:14 +01:00
|
|
|
|
|
|
|
|
2023-09-29 17:52:06 +01:00
|
|
|
def api_request(params: CallParams) -> dict[str, typing.Any]:
|
|
|
|
"""Make API request and return object parsed from JSON."""
|
|
|
|
return typing.cast(dict[str, typing.Any], raw_request(params).json())
|
2022-08-15 21:10:14 +01:00
|
|
|
|
|
|
|
|
2023-09-29 17:52:06 +01:00
|
|
|
def get_token() -> str:
|
|
|
|
"""Get csrftoken from MediaWiki API."""
|
|
|
|
params: CallParams = {
|
2022-08-15 21:10:14 +01:00
|
|
|
"action": "query",
|
|
|
|
"meta": "tokens",
|
|
|
|
"format": "json",
|
|
|
|
"formatversion": 2,
|
|
|
|
}
|
|
|
|
reply = api_request(params)
|
2023-09-29 17:52:06 +01:00
|
|
|
token: str = reply["query"]["tokens"]["csrftoken"]
|
2022-08-15 21:10:14 +01:00
|
|
|
|
|
|
|
return token
|
|
|
|
|
|
|
|
|
2023-09-29 17:52:06 +01:00
|
|
|
def userinfo_call() -> dict[str, typing.Any]:
|
2022-08-15 21:10:14 +01:00
|
|
|
"""Request user information via OAuth."""
|
2023-09-29 17:52:06 +01:00
|
|
|
params: CallParams = {"action": "query", "meta": "userinfo", "format": "json"}
|
2022-08-15 21:10:14 +01:00
|
|
|
return api_request(params)
|
|
|
|
|
|
|
|
|
2023-09-29 17:52:06 +01:00
|
|
|
def get_username() -> str | None:
|
|
|
|
"""Get username for current user."""
|
2022-08-15 21:10:14 +01:00
|
|
|
if "owner_key" not in session:
|
2023-09-29 17:52:06 +01:00
|
|
|
return None # not authorized
|
2022-08-15 21:10:14 +01:00
|
|
|
|
|
|
|
if "username" in session:
|
2023-09-29 17:52:06 +01:00
|
|
|
assert isinstance(session["username"], str)
|
2022-08-15 21:10:14 +01:00
|
|
|
return session["username"]
|
|
|
|
|
|
|
|
reply = userinfo_call()
|
|
|
|
if "query" not in reply:
|
2023-09-29 17:52:06 +01:00
|
|
|
return None
|
|
|
|
username = reply["query"]["userinfo"]["name"]
|
|
|
|
assert isinstance(username, str)
|
|
|
|
session["username"] = username
|
2022-08-15 21:10:14 +01:00
|
|
|
|
2023-09-29 17:52:06 +01:00
|
|
|
return username
|