From 1b8c98823a4ba7029065a06efdd849382ac8b15c Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Mon, 16 Aug 2021 16:20:40 +0200 Subject: Retrieve title as well --- wikimini/__init__.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/wikimini/__init__.py b/wikimini/__init__.py index 7dc573e..ca8bfab 100644 --- a/wikimini/__init__.py +++ b/wikimini/__init__.py @@ -17,7 +17,7 @@ def page_url(title): return title.replace(" ", "_") -def retrieve(title: str) -> mwp.wikicode.Wikicode: +def retrieve(title: str) -> (str, mwp.wikicode.Wikicode): params = { "action": "query", "prop": "revisions", @@ -31,9 +31,11 @@ def retrieve(title: str) -> mwp.wikicode.Wikicode: headers = {"User-Agent": "Wikimini/1.0"} req = requests.get(API_URL, headers=headers, params=params) res = req.json() - revision = res["query"]["pages"][0]["revisions"][0] + page = res["query"]["pages"][0] + title = page["title"] + revision = page["revisions"][0] text = revision["slots"]["main"]["content"] - return mwp.parse(text) + return (title, mwp.parse(text)) def render_convert(obj: mwp.nodes.template.Template) -> str: @@ -178,10 +180,17 @@ def _convert(obj: Union[mwp.nodes.Node, mwp.wikicode.Wikicode]) -> str: return render_convert(obj) elif name in {"lang", "script"}: return _convert(obj.params[1].value) + elif name == "lang-ar": + return "Arabic {}/{}".format( + _convert(obj.params[0].value), + _convert(obj.params[1].value), + ) elif name in {"reign", "r.", "ruled", "rexit"}: return render_reign(obj) elif name in {"cite book", "cite journal", "cite news"}: return render_cite_book(obj) + elif name.startswith("ipa"): + return "pronounced [{}]".format(_convert(obj.params[0].value)) elif name == "main": return render_main(obj) else: -- cgit v1.2.3