diff --git a/main.py b/main.py index b84e6f7..e2aef86 100755 --- a/main.py +++ b/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: diff --git a/templates/combined.html b/templates/combined.html index 394327b..1f4ee7f 100644 --- a/templates/combined.html +++ b/templates/combined.html @@ -63,6 +63,7 @@

← Back to category

{% endif %}

Wikipedia article: {{ name }}

+ {% if wikipedia_extract %}

{{ wikipedia_extract }}

{% endif %}
{% if cat %}{% endif %} @@ -151,6 +152,7 @@

← Back to category

{% endif %}

Wikipedia article: {{ name }}

+ {% if wikipedia_extract %}

{{ wikipedia_extract }}

{% endif %} {% if cat %}{% endif %}