2023-09-08 01:10:09 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2023-09-08 11:01:50 +01:00
|
|
|
import collections
|
|
|
|
import json
|
2023-09-08 01:10:09 +01:00
|
|
|
import sys
|
2023-09-08 11:01:50 +01:00
|
|
|
import typing
|
2023-09-08 01:10:09 +01:00
|
|
|
from urllib.parse import unquote
|
|
|
|
|
|
|
|
import flask
|
2023-09-08 11:01:50 +01:00
|
|
|
import requests
|
2023-09-08 01:10:09 +01:00
|
|
|
|
|
|
|
app = flask.Flask(__name__)
|
|
|
|
app.debug = True
|
|
|
|
|
|
|
|
enwiki = "en.wikipedia.org/wiki/"
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
2023-09-08 03:46:04 +01:00
|
|
|
def start() -> str:
|
2023-09-08 01:10:09 +01:00
|
|
|
"""Start form."""
|
2023-09-08 03:46:04 +01:00
|
|
|
return flask.render_template("wikipedia_url.html")
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/flickr")
|
|
|
|
def flickr_search() -> str:
|
|
|
|
"""Search flickr."""
|
|
|
|
wikipedia_url = flask.request.args["wikipedia"]
|
|
|
|
start = wikipedia_url.find(enwiki) + len(enwiki)
|
|
|
|
name = unquote(wikipedia_url[start:]).replace("_", " ")
|
|
|
|
return flask.render_template(
|
|
|
|
"flickr_search.html", name=name, wikipedia_url=wikipedia_url
|
|
|
|
)
|
2023-09-08 01:10:09 +01:00
|
|
|
|
|
|
|
|
2023-09-08 11:01:50 +01:00
|
|
|
def get_params(line_iter: collections.abc.Iterable[str]) -> str:
|
|
|
|
"""Find and return params from flickr profile page."""
|
|
|
|
look_for = 'params: {"isEditingTestimonial":false,'
|
|
|
|
return next(line[line.find("{") :] for line in line_iter if look_for in line)
|
|
|
|
|
|
|
|
|
|
|
|
def flickr_usrename_to_nsid(username: str) -> str:
|
|
|
|
"""Get NSID from flickr username."""
|
|
|
|
url = f"https://www.flickr.com/people/{username}/"
|
|
|
|
r = requests.get(url)
|
|
|
|
params = json.loads(get_params(r.text.splitlines()))
|
|
|
|
return typing.cast(str, params["nsid"])
|
|
|
|
|
|
|
|
|
2023-09-08 01:10:09 +01:00
|
|
|
@app.route("/message")
|
|
|
|
def show_message() -> str:
|
|
|
|
"""Show message."""
|
|
|
|
flickr_url = flask.request.args["flickr"]
|
|
|
|
wikipedia_url = flask.request.args["wikipedia"]
|
|
|
|
|
|
|
|
start = wikipedia_url.find(enwiki) + len(enwiki)
|
|
|
|
wiki_part1 = wikipedia_url[:start]
|
|
|
|
|
|
|
|
if len(sys.argv) > 4:
|
|
|
|
name = sys.argv[4]
|
|
|
|
else:
|
|
|
|
wiki_part2 = unquote(wikipedia_url[start:])
|
|
|
|
name = wiki_part2
|
|
|
|
|
|
|
|
if "_(" in name:
|
|
|
|
name = name[: name.find("_(")]
|
|
|
|
name = name.replace("_", " ")
|
|
|
|
|
|
|
|
if "/in/" in flickr_url:
|
|
|
|
flickr_url = flickr_url[: flickr_url.find("/in/")]
|
|
|
|
|
2023-09-08 11:01:50 +01:00
|
|
|
flickr_start = "https://flickr.com/photos/"
|
|
|
|
|
|
|
|
assert flickr_url.startswith(flickr_start)
|
|
|
|
flickr_username = flickr_url[
|
|
|
|
len(flickr_start) : flickr_url.find("/", len(flickr_start))
|
|
|
|
]
|
|
|
|
|
|
|
|
nsid = flickr_usrename_to_nsid(flickr_username)
|
|
|
|
assert nsid
|
|
|
|
print(nsid)
|
|
|
|
|
2023-09-08 01:10:09 +01:00
|
|
|
msg = flask.render_template(
|
|
|
|
"message.jinja",
|
|
|
|
flickr_url=flickr_url,
|
|
|
|
wikipedia_url=wikipedia_url,
|
|
|
|
name=name,
|
|
|
|
wiki_part1=wiki_part1,
|
|
|
|
wiki_part2=wiki_part2,
|
|
|
|
)
|
|
|
|
|
2023-09-08 01:52:04 +01:00
|
|
|
subject = f"Request to use your photo of {name} on Wikipedia"
|
|
|
|
|
|
|
|
lines = msg.split("\n\n")
|
|
|
|
|
2023-09-08 11:01:50 +01:00
|
|
|
return flask.render_template(
|
|
|
|
"show_message.html", subject=subject, lines=lines, nsid=nsid
|
|
|
|
)
|
2023-09-08 01:10:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(host="0.0.0.0")
|