This commit is contained in:
Edward Betts 2022-08-17 14:48:20 +01:00
parent 5f8900a47a
commit b1f402e1f9
2 changed files with 16 additions and 8 deletions

View file

@ -147,7 +147,7 @@ class Article:
def __init__(self, enwiki: str) -> None: def __init__(self, enwiki: str) -> None:
"""Make a new Article object.""" """Make a new Article object."""
self.enwiki = enwiki self.enwiki = enwiki.replace("_", " ")
self.links = get_article_links(enwiki) self.links = get_article_links(enwiki)

View file

@ -3,6 +3,7 @@
import inspect import inspect
import json import json
import re import re
from typing import Optional
import flask import flask
import lxml.html import lxml.html
@ -123,16 +124,23 @@ def save(enwiki: str) -> Response | str:
) )
def redirect_if_needed(enwiki: str) -> Optional[Response]:
"""Check if there are spaces in the article name and redirect."""
return (
flask.redirect(
flask.url_for(flask.request.endpoint, enwiki=enwiki.replace(" ", "_"))
)
if " " in enwiki
else None
)
@app.route("/enwiki/<path:enwiki>") @app.route("/enwiki/<path:enwiki>")
def article_page(enwiki: str) -> Response: def article_page(enwiki: str) -> Response:
"""Article Page.""" """Article Page."""
enwiki_orig = enwiki redirect = redirect_if_needed(enwiki)
enwiki = enwiki.replace("_", " ") if redirect:
enwiki_underscore = enwiki.replace(" ", "_") return redirect
if " " in enwiki_orig:
return flask.redirect(
flask.url_for(flask.request.endpoint, enwiki=enwiki_underscore)
)
article = wikipedia.Article(enwiki) article = wikipedia.Article(enwiki)
article.load() article.load()