Show Wikipedia article extract on Flickr search results page

Fetches the introductory paragraph from the Wikipedia API so users
can see what/who they're looking for when browsing Flickr search results.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Edward Betts 2026-02-08 14:56:57 +00:00
parent 32e81c2d93
commit 937735c396
2 changed files with 27 additions and 0 deletions

25
main.py
View file

@ -386,6 +386,28 @@ def has_content_image(images: list[dict]) -> bool:
return False return False
def get_wikipedia_extract(title: str) -> str:
"""Get the first paragraph extract of a Wikipedia article."""
params = {
"action": "query",
"prop": "extracts",
"exintro": "true",
"explaintext": "true",
"titles": title,
"format": "json",
}
headers = {"User-Agent": WIKIMEDIA_USER_AGENT}
try:
r = requests.get(WIKIPEDIA_API, params=params, headers=headers)
data = r.json()
pages = data.get("query", {}).get("pages", {})
for page in pages.values():
return page.get("extract", "")
except Exception:
pass
return ""
def get_articles_without_images( def get_articles_without_images(
category: str, category: str,
limit: int = 200, limit: int = 200,
@ -730,6 +752,8 @@ def start() -> str:
flickr_search = f'"{without_middle}"' flickr_search = f'"{without_middle}"'
search_result = search_flickr(flickr_search, page) search_result = search_flickr(flickr_search, page)
wikipedia_extract = get_wikipedia_extract(wiki_part2)
return flask.render_template( return flask.render_template(
"combined.html", "combined.html",
name=name, name=name,
@ -737,6 +761,7 @@ def start() -> str:
search_result=search_result, search_result=search_result,
cat=cat, cat=cat,
flickr_search=flickr_search, flickr_search=flickr_search,
wikipedia_extract=wikipedia_extract,
) )
if "/in/" in flickr_url: if "/in/" in flickr_url:

View file

@ -63,6 +63,7 @@
<p><a href="{{ url_for('category_search', cat=cat) }}">&larr; Back to category</a></p> <p><a href="{{ url_for('category_search', cat=cat) }}">&larr; Back to category</a></p>
{% endif %} {% endif %}
<p>Wikipedia article: {{ name }}</p> <p>Wikipedia article: {{ name }}</p>
{% if wikipedia_extract %}<p class="text-muted">{{ wikipedia_extract }}</p>{% endif %}
<form action="{{ url_for(request.endpoint) }}" class="mb-3 d-flex align-items-center gap-2"> <form action="{{ url_for(request.endpoint) }}" class="mb-3 d-flex align-items-center gap-2">
<input type="hidden" name="enwp" value="{{ enwp }}"> <input type="hidden" name="enwp" value="{{ enwp }}">
{% if cat %}<input type="hidden" name="cat" value="{{ cat }}">{% endif %} {% if cat %}<input type="hidden" name="cat" value="{{ cat }}">{% endif %}
@ -151,6 +152,7 @@
<p><a href="{{ url_for('category_search', cat=cat) }}">&larr; Back to category</a></p> <p><a href="{{ url_for('category_search', cat=cat) }}">&larr; Back to category</a></p>
{% endif %} {% endif %}
<p>Wikipedia article: {{ name }}</p> <p>Wikipedia article: {{ name }}</p>
{% if wikipedia_extract %}<p class="text-muted">{{ wikipedia_extract }}</p>{% endif %}
<form action="{{ url_for(request.endpoint) }}" class="mb-3 d-flex align-items-center gap-2"> <form action="{{ url_for(request.endpoint) }}" class="mb-3 d-flex align-items-center gap-2">
<input type="hidden" name="enwp" value="{{ enwp }}"> <input type="hidden" name="enwp" value="{{ enwp }}">
{% if cat %}<input type="hidden" name="cat" value="{{ cat }}">{% endif %} {% if cat %}<input type="hidden" name="cat" value="{{ cat }}">{% endif %}