dab-mechanic/web_view.py

215 lines
6.1 KiB
Python
Raw Normal View History

2022-08-13 13:16:49 +01:00
#!/usr/bin/python3
2022-08-13 16:25:07 +01:00
import json
2022-08-13 13:16:49 +01:00
from collections import defaultdict
2022-08-13 16:25:07 +01:00
from typing import Any
2022-08-13 13:16:49 +01:00
import flask
import lxml.html
import requests
from werkzeug.wrappers import Response
app = flask.Flask(__name__)
app.debug = True
@app.route("/")
def index():
articles = [line[:-1] for line in open("article_list")]
return flask.render_template("index.html", articles=articles)
def get_article_html(enwiki: str) -> str:
2022-08-13 16:25:07 +01:00
"""Parse article wikitext and return HTML."""
2022-08-13 13:16:49 +01:00
url = "https://en.wikipedia.org/w/api.php"
2022-08-13 16:25:07 +01:00
params: dict[str, str | int] = {
2022-08-13 13:16:49 +01:00
"action": "parse",
"format": "json",
"formatversion": 2,
"disableeditsection": 1,
"page": enwiki,
}
r = requests.get(url, params=params)
html: str = r.json()["parse"]["text"]
return html
disambig_templates = [
"Template:Disambiguation",
"Template:Airport disambiguation",
"Template:Biology disambiguation",
"Template:Call sign disambiguation",
"Template:Caselaw disambiguation",
"Template:Chinese title disambiguation",
"Template:Disambiguation cleanup",
"Template:Genus disambiguation",
"Template:Hospital disambiguation",
"Template:Human name disambiguation",
"Template:Human name disambiguation cleanup",
"Template:Letter-number combination disambiguation",
"Template:Mathematical disambiguation",
"Template:Military unit disambiguation",
"Template:Music disambiguation",
"Template:Number disambiguation",
"Template:Opus number disambiguation",
"Template:Phonetics disambiguation",
"Template:Place name disambiguation",
"Template:Portal disambiguation",
"Template:Road disambiguation",
"Template:School disambiguation",
"Template:Species Latin name abbreviation disambiguation",
"Template:Species Latin name disambiguation",
"Template:Station disambiguation",
"Template:Synagogue disambiguation",
"Template:Taxonomic authority disambiguation",
"Template:Taxonomy disambiguation",
"Template:Template disambiguation",
"Template:WoO number disambiguation",
]
2022-08-13 16:25:07 +01:00
def link_params(enwiki: str) -> dict[str, str | int]:
"""Parameters for finding article links from the API."""
params: dict[str, str | int] = {
2022-08-13 13:16:49 +01:00
"action": "query",
"format": "json",
"formatversion": 2,
"titles": enwiki,
"generator": "links",
"gpllimit": "max",
"gplnamespace": 0,
"tllimit": "max",
"tlnamespace": 10,
"tltemplates": "|".join(disambig_templates),
"prop": "templates",
}
2022-08-13 16:25:07 +01:00
return params
def needs_disambig(link: dict[str, Any]) -> bool:
"""Is this a disambiguation link."""
return bool(
not link["title"].endswith(" (disambiguation)") and link.get("templates")
)
2022-08-13 13:16:49 +01:00
2022-08-13 16:25:07 +01:00
def get_article_links(enwiki: str) -> list[str]:
"""Get links that appear in this article."""
url = "https://en.wikipedia.org/w/api.php"
params: dict[str, str | int] = link_params(enwiki)
links: set[str] = set()
2022-08-13 13:16:49 +01:00
while True:
2022-08-13 16:25:07 +01:00
data = requests.get(url, params=params).json()
links.update(
page["title"] for page in data["query"]["pages"] if needs_disambig(page)
)
if "continue" not in data:
2022-08-13 13:16:49 +01:00
break
2022-08-13 16:25:07 +01:00
params["gplcontinue"] = data["continue"]["gplcontinue"]
2022-08-13 13:16:49 +01:00
2022-08-13 16:25:07 +01:00
return list(links)
2022-08-13 13:16:49 +01:00
# return {link["title"] for link in r.json()["query"]["pages"][0]["links"]}
2022-08-13 16:25:07 +01:00
def delete_toc(root: lxml.html.HtmlElement) -> None:
"""Delete table of contents from article HTML."""
for toc in root.findall(".//div[@class='toc']"):
toc.getparent().remove(toc)
def get_dab_html(dab_num: int, title: str) -> str:
"""Parse dab page and rewrite links."""
dab_html = get_article_html(title)
root = lxml.html.fromstring(dab_html)
delete_toc(root)
element_id_map = {e.get("id"): e for e in root.findall(".//*[@id]")}
for a in root.findall(".//a[@href]"):
href: str | None = a.get("href")
if not href:
continue
if not href.startswith("#"):
a.set("href", "#")
a.set("onclick", f"return select_dab(this, {dab_num})")
continue
destination_element = element_id_map[href[1:]]
assert destination_element is not None
destination_element.set("id", f"{dab_num}{href[1:]}")
a.set("href", f"#{dab_num}{href[1:]}")
html: str = lxml.html.tostring(root, encoding=str)
return html
@app.route("/save/<path:enwiki>", methods=["POST"])
def save(enwiki: str) -> Response | str:
"""Save edits to article."""
edits = json.loads(flask.request.form["edits"])
return flask.render_template("save.html", title=enwiki, edits=edits)
2022-08-13 13:16:49 +01:00
@app.route("/enwiki/<path:enwiki>")
def article(enwiki: str) -> Response:
"""Article Page."""
2022-08-13 16:25:07 +01:00
enwiki_orig = enwiki
enwiki = enwiki.replace("_", " ")
enwiki_underscore = enwiki.replace(" ", "_")
if " " in enwiki_orig:
return flask.redirect(
flask.url_for(flask.request.endpoint, enwiki=enwiki_underscore)
)
2022-08-13 13:16:49 +01:00
html = get_article_html(enwiki)
links = get_article_links(enwiki)
root = lxml.html.fromstring(html)
html_links = defaultdict(list)
seen = set()
dab_list = []
2022-08-13 16:25:07 +01:00
dab_lookup = {}
2022-08-13 13:16:49 +01:00
dab_num = 0
for a in root.findall(".//a[@href]"):
title = a.get("title")
if title is None:
continue
if title not in links:
continue
a.set("class", "disambig")
if title not in seen:
dab_num += 1
a.set("id", f"dab-{dab_num}")
seen.add(title)
2022-08-13 16:25:07 +01:00
dab_html = get_dab_html(dab_num, title)
2022-08-13 13:16:49 +01:00
dab_list.append({"num": dab_num, "title": title, "html": dab_html})
2022-08-13 16:25:07 +01:00
dab_lookup[dab_num] = title
2022-08-13 13:16:49 +01:00
html_links[title].append(a)
return flask.render_template(
"article.html",
title=enwiki,
2022-08-13 16:25:07 +01:00
enwiki_underscore=enwiki_underscore,
2022-08-13 13:16:49 +01:00
text=lxml.html.tostring(root, encoding=str),
links=links,
html_links=html_links,
dab_list=dab_list,
2022-08-13 16:25:07 +01:00
dab_lookup=dab_lookup,
2022-08-13 13:16:49 +01:00
)
if __name__ == "__main__":
app.run(host="0.0.0.0")