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:
parent
32e81c2d93
commit
937735c396
2 changed files with 27 additions and 0 deletions
25
main.py
25
main.py
|
|
@ -386,6 +386,28 @@ def has_content_image(images: list[dict]) -> bool:
|
|||
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(
|
||||
category: str,
|
||||
limit: int = 200,
|
||||
|
|
@ -730,6 +752,8 @@ def start() -> str:
|
|||
flickr_search = f'"{without_middle}"'
|
||||
search_result = search_flickr(flickr_search, page)
|
||||
|
||||
wikipedia_extract = get_wikipedia_extract(wiki_part2)
|
||||
|
||||
return flask.render_template(
|
||||
"combined.html",
|
||||
name=name,
|
||||
|
|
@ -737,6 +761,7 @@ def start() -> str:
|
|||
search_result=search_result,
|
||||
cat=cat,
|
||||
flickr_search=flickr_search,
|
||||
wikipedia_extract=wikipedia_extract,
|
||||
)
|
||||
|
||||
if "/in/" in flickr_url:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue