diff --git a/src/routers/public_api.py b/src/routers/public_api.py index 70156de..d5c4913 100644 --- a/src/routers/public_api.py +++ b/src/routers/public_api.py @@ -269,7 +269,7 @@ async def get_globe_feed( inc_ids = [i["id"] for i in incidents] ids_sql = ",".join(str(i) for i in inc_ids) - # Locations mit Artikel-Headlines + # Locations mit vollstaendigen Artikel-Details cursor = await db.execute( f"""SELECT al.location_name_normalized as name, @@ -278,37 +278,69 @@ async def get_globe_feed( al.country_code, al.category, al.incident_id, - COUNT(*) as article_count, - MAX(al.confidence) as confidence, - GROUP_CONCAT(DISTINCT a.headline_de) as headlines + al.article_id FROM article_locations al - LEFT JOIN articles a ON al.article_id = a.id WHERE al.incident_id IN ({ids_sql}) - GROUP BY al.location_name_normalized, al.incident_id - ORDER BY article_count DESC - LIMIT 500""" + ORDER BY al.location_name_normalized""" ) - locations = [dict(r) for r in await cursor.fetchall()] + loc_rows = [dict(r) for r in await cursor.fetchall()] + + # Artikel-IDs sammeln + all_article_ids = list(set(r["article_id"] for r in loc_rows if r.get("article_id"))) + + # Artikel laden + articles_map = {{}} + if all_article_ids: + aids_sql = ",".join(str(a) for a in all_article_ids[:1000]) + cursor = await db.execute( + f"""SELECT id, headline_de, headline, source, source_url, content_de, + published_at, collected_at + FROM articles WHERE id IN ({{aids_sql}})""" + ) + for a in await cursor.fetchall(): + a = dict(a) + articles_map[a["id"]] = a + + # Nach Ort gruppieren + from collections import defaultdict + loc_groups = defaultdict(lambda: {{"articles": [], "lat": 0, "lon": 0, "country": "", "category": "", "incident_id": 0}}) + for r in loc_rows: + key = r["name"] or "unknown" + g = loc_groups[key] + g["lat"] = r["lat"] + g["lon"] = r["lon"] + g["country"] = r["country_code"] + g["category"] = r["category"] + g["incident_id"] = r["incident_id"] + if r.get("article_id") and r["article_id"] in articles_map: + art = articles_map[r["article_id"]] + if art not in g["articles"]: + g["articles"].append(art) # Als GeoJSON features = [] - for loc in locations: - inc = next((i for i in incidents if i["id"] == loc["incident_id"]), None) - headlines = (loc.get("headlines") or "").split(",")[:5] - features.append({ + for name, g in list(loc_groups.items())[:500]: + inc = next((i for i in incidents if i["id"] == g["incident_id"]), None) + art_list = g["articles"][:5] + features.append({{ "type": "Feature", - "geometry": {"type": "Point", "coordinates": [loc["lon"], loc["lat"]]}, - "properties": { - "name": loc["name"], - "country": loc["country_code"], - "category": loc["category"], - "article_count": loc["article_count"], - "confidence": loc["confidence"], - "incident_id": loc["incident_id"], + "geometry": {{"type": "Point", "coordinates": [g["lon"], g["lat"]]}}, + "properties": {{ + "name": name, + "country": g["country"], + "category": g["category"], + "article_count": len(g["articles"]), + "incident_id": g["incident_id"], "incident_title": inc["title"] if inc else "", - "headlines": headlines, - }, - }) + "articles": [{{ + "headline": a.get("headline_de") or a.get("headline", ""), + "source": a.get("source", ""), + "url": a.get("source_url", ""), + "summary": (a.get("content_de") or "")[:300], + "date": a.get("published_at") or a.get("collected_at", ""), + }} for a in art_list], + }}, + }}) # Incident-Summaries inc_summaries = []