diff --git a/.gitignore b/.gitignore
index 0dcd64f..613f75e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@ __pycache__
.mypy_cache/
node_modules
package-lock.json
+static/bootstrap
diff --git a/add_links/wikidata_oauth.py b/add_links/wikidata_oauth.py
index d91b66e..9b4de72 100644
--- a/add_links/wikidata_oauth.py
+++ b/add_links/wikidata_oauth.py
@@ -1,7 +1,10 @@
+"""Wikipedia OAuth."""
+
import typing
import urllib
from typing import cast
+import requests
from flask import current_app, session
from requests_oauthlib import OAuth1Session
@@ -9,6 +12,12 @@ wiki_hostname = "en.wikipedia.org"
api_url = f"https://{wiki_hostname}/w/api.php"
+class LoginNeeded(Exception):
+ """Not logged in."""
+
+ pass
+
+
def get_edit_proxy() -> dict[str, str]:
"""Retrieve proxy information from config."""
edit_proxy = current_app.config.get("EDIT_PROXY")
@@ -18,7 +27,7 @@ def get_edit_proxy() -> dict[str, str]:
return {}
-def api_post_request(params: dict[str, str | int]):
+def api_post_request(params: dict[str, str | int]) -> requests.Response:
"""HTTP Post using Oauth."""
app = current_app
# url = "https://www.wikidata.org/w/api.php"
@@ -34,12 +43,14 @@ def api_post_request(params: dict[str, str | int]):
return oauth.post(api_url, data=params, timeout=4, proxies=proxies)
-def raw_request(params: typing.Mapping[str, str | int]):
+def raw_request(params: typing.Mapping[str, str | int]) -> requests.Response:
"""Low-level API request."""
app = current_app
# url = "https://www.wikidata.org/w/api.php?" + urlencode(params)
client_key = app.config["CLIENT_KEY"]
client_secret = app.config["CLIENT_SECRET"]
+ if "owner_key" not in session or "owner_secret" not in session:
+ raise LoginNeeded
oauth = OAuth1Session(
client_key,
client_secret=client_secret,
diff --git a/templates/article.html b/templates/article.html
index ec3d3e4..55d05b0 100644
--- a/templates/article.html
+++ b/templates/article.html
@@ -1,56 +1,48 @@
{% extends "base.html" %}
-{% block title %}{{ title }}{% endblock %}
+{% block title %}Link '{{ title }}' in '{{ hit_title }}'{% endblock %}
{% block style %}
-
+
{% endblock %}
{% block content %}
-
{{ self.title() }}
-
-
+
+
Username: {{ g.user }}
+
+
+
+
+
+
total: {{ total }}
+
with link: {{ with_link }}
+
ratio: {{ "{:.1%}".format(with_link / total) }}
+ {#
hit: {{ hit }}
#}
+
replacement: {{ found.replacement }}
+
section: {{ found.section }}
+
+
+
+
+ {% for hit in hits %}
+ {% set url = url_for("article_page", url_title=url_title, title=hit.title) %}
+ - {{ hit.title }} – {{ hit.snippet | safe }}
+ {% endfor %}
+
-
-
-
-
-
{% endblock %}
diff --git a/templates/article2.html b/templates/article2.html
deleted file mode 100644
index e6ef74c..0000000
--- a/templates/article2.html
+++ /dev/null
@@ -1,41 +0,0 @@
-{% extends "base.html" %}
-
-{% block title %}Link '{{ title }}' in '{{ hit.title }}'{% endblock %}
-
-{% block style %}
-
-{% endblock %}
-
-{% block content %}
-
-
Link '{{ title }}' in '{{ hit.title }}'
-
-
-
Username: {{ g.user }}
-
-
-
-
-
-
total: {{ total }}
-
with link: {{ with_link }}
-
ratio: {{ "{:.1%}".format(with_link / total) }}
- {#
hit: {{ hit }}
#}
-
replacement: {{ found.replacement }}
-
section: {{ found.section }}
-
-
-
-{% endblock %}
-
diff --git a/templates/article_old.html b/templates/article_old.html
new file mode 100644
index 0000000..ec3d3e4
--- /dev/null
+++ b/templates/article_old.html
@@ -0,0 +1,56 @@
+{% extends "base.html" %}
+
+{% block title %}{{ title }}{% endblock %}
+
+{% block style %}
+
+{% endblock %}
+
+{% block content %}
+
+
{{ self.title() }}
+
+
+
+
+
+
+
+
+{% endblock %}
+
diff --git a/web_view.py b/web_view.py
index 5e1e507..436307a 100755
--- a/web_view.py
+++ b/web_view.py
@@ -236,7 +236,7 @@ def match_type(q: str, snippet: str) -> str | None:
class NoGoodHit(Exception):
- pass
+ """No good hit."""
def get_best_hit(title: str, hits: list[Hit]) -> tuple[Hit, dict[str, typing.Any]]:
@@ -266,35 +266,47 @@ def article_page(url_title: str) -> str | Response:
if flask.request.method == "POST":
hit_title = flask.request.form["hit"]
- do_save(from_title, hit_title)
+ try:
+ do_save(from_title, hit_title)
+ except wikidata_oauth.LoginNeeded:
+ return flask.redirect(flask.url_for("start_oauth"))
return flask.redirect(
flask.url_for("article_page", url_title=url_title, after=hit_title)
)
+ article_title = flask.request.args.get("title")
+
total = search_count(from_title)
with_link = search_count_with_link(from_title)
no_link_count, hits = search_no_link(from_title)
- after = flask.request.args.get("after")
- if after:
- print(after)
- hits_iter = itertools.dropwhile(lambda hit: hit["title"] != after, hits)
- skip = next(hits_iter, None)
- if skip:
- hits = list(hits_iter)
+ by_title = {hit["title"]: hit for hit in hits}
- try:
- hit, found = get_best_hit(from_title, hits)
- except NoGoodHit:
- return flask.render_template("all_done.html")
+ if article_title in by_title:
+ hit = by_title[article_title]
+ found = get_diff(from_title, hit["title"], None)
+ else:
+ after = flask.request.args.get("after")
+ if after:
+ print(after)
+ hits_iter = itertools.dropwhile(lambda hit: hit["title"] != after, hits)
+ skip = next(hits_iter, None)
+ if skip:
+ hits = list(hits_iter)
+
+ try:
+ hit, found = get_best_hit(from_title, hits)
+ except NoGoodHit:
+ return flask.render_template("all_done.html")
return flask.render_template(
- "article2.html",
+ "article.html",
title=from_title,
total=total,
with_link=with_link,
- hit=hit,
+ hit_title=hit["title"],
+ hits=hits,
replacement=found["replacement"],
diff=found["diff"],
found=found,