| 1 | import { NextResponse } from 'next/server'; |
| 2 | |
| 3 | export const dynamic = 'force-dynamic'; |
| 4 | |
| 5 | /** |
| 6 | * OSIRIS — Global Incidents API (GDELT Fallback / RSS OSINT Mapper) |
| 7 | * Since GDELT v2 Geo is frequently down (404/Timeout), this fallback |
| 8 | * aggregates global news RSS (BBC, Al Jazeera, etc.) and performs |
| 9 | * lightweight keyword geo-mapping to generate incident points. |
| 10 | */ |
| 11 | |
| 12 | const RSS_FEEDS = [ |
| 13 | { url: 'https://feeds.bbci.co.uk/news/world/rss.xml', source: 'BBC World' }, |
| 14 | { url: 'https://www.aljazeera.com/xml/rss/all.xml', source: 'Al Jazeera' }, |
| 15 | { url: 'https://rss.nytimes.com/services/xml/rss/nyt/World.xml', source: 'NYT World' } |
| 16 | ]; |
| 17 | |
| 18 | // Lightweight geo-dictionary for mapping news keywords to coordinates |
| 19 | const GEO_DICT: Record<string, [number, number]> = { |
| 20 | 'ukraine': [31.1656, 48.3794], |
| 21 | 'kyiv': [30.5234, 50.4501], |
| 22 | 'russia': [37.6173, 55.7558], |
| 23 | 'moscow': [37.6173, 55.7558], |
| 24 | 'gaza': [34.4668, 31.5017], |
| 25 | 'israel': [34.8516, 31.0461], |
| 26 | 'tel aviv': [34.7818, 32.0853], |
| 27 | 'palestine': [35.2332, 31.9522], |
| 28 | 'iran': [53.6880, 32.4279], |
| 29 | 'tehran': [51.3890, 35.6892], |
| 30 | 'syria': [38.9968, 34.8021], |
| 31 | 'lebanon': [35.8623, 33.8547], |
| 32 | 'beirut': [35.5018, 33.8938], |
| 33 | 'yemen': [47.5868, 15.5527], |
| 34 | 'houthi': [44.2066, 15.3694], // Sana'a |
| 35 | 'sudan': [30.2176, 12.8628], |
| 36 | 'china': [116.4074, 39.9042], |
| 37 | 'taiwan': [120.9605, 23.6978], |
| 38 | 'korea': [127.7669, 35.9078], |
| 39 | 'usa': [-77.0369, 38.9072], |
| 40 | 'myanmar': [95.9560, 21.9162], |
| 41 | 'haiti': [-72.2852, 18.9712], |
| 42 | 'somalia': [46.1996, 5.1521], |
| 43 | 'bulgaria': [25.4858, 42.7339], |
| 44 | 'serbia': [21.0059, 44.0165], |
| 45 | 'greece': [21.8243, 39.0742], |
| 46 | 'turkey': [35.2433, 38.9637], |
| 47 | 'macedonia': [21.7453, 41.6086], |
| 48 | 'romania': [24.9668, 45.9432], |
| 49 | 'france': [2.2137, 46.2276], |
| 50 | 'germany': [10.4515, 51.1657], |
| 51 | 'uk': [-3.4359, 55.3781], |
| 52 | 'mexico': [-102.5528, 23.6345] |
| 53 | }; |
| 54 | |
| 55 | const CONFLICT_KEYWORDS = ['attack', 'strike', 'missile', 'drone', 'war', 'troops', 'military', 'protest', 'riot', 'police', 'clash', 'bomb', 'killed', 'forces']; |
| 56 | |
| 57 | export async function GET() { |
| 58 | try { |
| 59 | const allEvents: any[] = []; |
| 60 | let eventId = 0; |
| 61 | |
| 62 | for (const feed of RSS_FEEDS) { |
| 63 | try { |
| 64 | const res = await fetch(feed.url, { signal: AbortSignal.timeout(5000) }); |
| 65 | if (!res.ok) continue; |
| 66 | const xml = await res.text(); |
| 67 | |
| 68 | // Very rudimentary regex to extract items to avoid heavy XML parser deps |
| 69 | const items = xml.match(/<item>([\s\S]*?)<\/item>/gi) || []; |
| 70 | |
| 71 | for (const item of items) { |
| 72 | const titleMatch = item.match(/<title><!\[CDATA\[(.*?)\]\]><\/title>/i) || item.match(/<title>(.*?)<\/title>/i); |
| 73 | const linkMatch = item.match(/<link>(.*?)<\/link>/i); |
| 74 | const descMatch = item.match(/<description><!\[CDATA\[(.*?)\]\]><\/description>/i) || item.match(/<description>(.*?)<\/description>/i); |
| 75 | |
| 76 | if (!titleMatch || !linkMatch) continue; |
| 77 | |
| 78 | const title = titleMatch[1]; |
| 79 | const link = linkMatch[1]; |
| 80 | const desc = descMatch ? descMatch[1] : ''; |
| 81 | |
| 82 | const textToSearch = (title + ' ' + desc).toLowerCase(); |
| 83 | |
| 84 | // Check if it's a conflict event |
| 85 | const isConflict = CONFLICT_KEYWORDS.some(kw => textToSearch.includes(kw)); |
| 86 | if (!isConflict) continue; |
| 87 | |
| 88 | // Try to geo-map |
| 89 | let coords: [number, number] | null = null; |
| 90 | for (const [location, point] of Object.entries(GEO_DICT)) { |
| 91 | // using word boundary regex |
| 92 | const regex = new RegExp(`\\b${location}\\b`, 'i'); |
| 93 | if (regex.test(textToSearch)) { |
| 94 | // Deterministic jitter based on event index so events in the same country don't overlap |
| 95 | const jitterLng = ((eventId * 137.5) % 200 - 100) / 100 * 1.5; |
| 96 | const jitterLat = ((eventId * 251.3) % 200 - 100) / 100 * 1.5; |
| 97 | coords = [point[0] + jitterLng, point[1] + jitterLat]; |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (coords) { |
| 103 | allEvents.push({ |
| 104 | id: `osint-${feed.source.replace(/\s+/g, '')}-${eventId++}`, |
| 105 | lat: coords[1], |
| 106 | lng: coords[0], |
| 107 | name: `[${feed.source}] ${title}`, |
| 108 | url: link, |
| 109 | html: `<a href="${link}" target="_blank">${title}</a><br/><i>Source: ${feed.source}</i>`, |
| 110 | type: 'conflict', |
| 111 | }); |
| 112 | } |
| 113 | } |
| 114 | } catch (e) { |
| 115 | console.warn(`Failed to fetch ${feed.source}`); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return NextResponse.json({ |
| 120 | events: allEvents, |
| 121 | total: allEvents.length, |
| 122 | timestamp: new Date().toISOString(), |
| 123 | source: 'OSINT RSS Mapping' |
| 124 | }, { |
| 125 | headers: { |
| 126 | 'Cache-Control': 'public, s-maxage=300, stale-while-revalidate=600', |
| 127 | }, |
| 128 | }); |
| 129 | } catch (error) { |
| 130 | console.error('OSINT RSS fetch error:', error); |
| 131 | return NextResponse.json({ events: [], error: 'Failed to fetch OSINT data' }, { status: 500 }); |
| 132 | } |
| 133 | } |