First form can take name or URL

Closes: #2
This commit is contained in:
Edward Betts 2023-10-02 19:48:50 +01:00
parent b202889dce
commit e4e9df88d7
2 changed files with 27 additions and 13 deletions

30
main.py
View file

@ -50,14 +50,29 @@ def exception_handler(e: werkzeug.exceptions.InternalServerError) -> tuple[str,
@app.route("/") @app.route("/")
def start() -> str: def start() -> str:
"""Start form.""" """Start form."""
wikipedia_url = flask.request.args.get("wikipedia") enwp = flask.request.args.get("enwp")
if not wikipedia_url: if not enwp:
return flask.render_template("combined.html")
enwp = enwp.strip()
if not enwp:
return flask.render_template("combined.html") return flask.render_template("combined.html")
start = wikipedia_url.find(enwiki) + len(enwiki) input_is = "url" if enwp.startswith(enwiki) else "title"
wiki_part2 = unquote(wikipedia_url[start:])
wikipedia_url: str
wiki_part1: str
wiki_part2: str
if input_is == "url":
start = enwp.find(enwiki) + len(enwiki)
wiki_part2 = unquote(enwp[start:])
name = wiki_part2 name = wiki_part2
wiki_part1 = enwp[:start]
wikipedia_url = enwp
else:
name = enwp
wiki_part1 = enwiki
wiki_part2 = name.replace(" ", "_")
wikipedia_url = wiki_part1 + wiki_part2
if "_(" in name: if "_(" in name:
name = name[: name.find("_(")] name = name[: name.find("_(")]
@ -68,11 +83,9 @@ def start() -> str:
return flask.render_template( return flask.render_template(
"combined.html", "combined.html",
name=name, name=name,
wikipedia_url=wikipedia_url, enwp=enwp,
) )
wiki_part1 = wikipedia_url[:start]
if "/in/" in flickr_url: if "/in/" in flickr_url:
flickr_url = flickr_url[: flickr_url.find("/in/")] flickr_url = flickr_url[: flickr_url.find("/in/")]
@ -90,6 +103,7 @@ def start() -> str:
msg = flask.render_template( msg = flask.render_template(
"message.jinja", "message.jinja",
flickr_url=flickr_url, flickr_url=flickr_url,
enwp=enwp,
wikipedia_url=wikipedia_url, wikipedia_url=wikipedia_url,
name=name, name=name,
wiki_part1=wiki_part1, wiki_part1=wiki_part1,
@ -103,7 +117,7 @@ def start() -> str:
return flask.render_template( return flask.render_template(
"combined.html", "combined.html",
name=name, name=name,
wikipedia_url=wikipedia_url, enwp=enwp,
flickr_url=flickr_url, flickr_url=flickr_url,
subject=subject, subject=subject,
lines=lines, lines=lines,

View file

@ -8,8 +8,8 @@
<h1>Flickr mail</h1> <h1>Flickr mail</h1>
<form action="{{ url_for(request.endpoint) }}"> <form action="{{ url_for(request.endpoint) }}">
<div class="mb-3"> <div class="mb-3">
<label for="wikipedia" class="form-label">Wikipedia URL:</label> <label for="enwp" class="form-label">Wikipedia article URL or title:</label>
<input type="text" class="form-control" id="wikipedia" name="wikipedia" value="{{ wikipedia_url }}" required> <input type="text" class="form-control" id="enwp" name="enwp" value="{{ enwp }}" required>
</div> </div>
<input type="submit" value="Submit"> <input type="submit" value="Submit">
@ -21,7 +21,7 @@
<p><a href="https://flickr.com/search/?view_all=1&safe_search=3&text={{ '"' + name + '"' | urlencode }}" target="_blank">Search flickr</a></p> <p><a href="https://flickr.com/search/?view_all=1&safe_search=3&text={{ '"' + name + '"' | urlencode }}" target="_blank">Search flickr</a></p>
<form action="{{ url_for(request.endpoint) }}"> <form action="{{ url_for(request.endpoint) }}">
<input type="hidden" name="wikipedia" value="{{ wikipedia_url }}"></input> <input type="hidden" name="enwp" value="{{ enwp }}"></input>
<div class="mb-3"> <div class="mb-3">
<label for="flickr" class="form-label">Flickr URL:</label> <label for="flickr" class="form-label">Flickr URL:</label>
<input type="text" class="form-control" id="flickr" name="flickr" value="{{ flickr_url }}" required> <input type="text" class="form-control" id="flickr" name="flickr" value="{{ flickr_url }}" required>