| 1 | |
| 2 | import { NextResponse } from 'next/server'; |
| 3 | |
| 4 | /** |
| 5 | * OSIRIS — Space Weather API |
| 6 | * Fetches real-time solar activity from NOAA Space Weather Prediction Center |
| 7 | * FREE — No API key required |
| 8 | * Data: Kp index (geomagnetic), solar flares, CME alerts |
| 9 | */ |
| 10 | |
| 11 | export async function GET() { |
| 12 | try { |
| 13 | const [kpRes, alertsRes, flareRes] = await Promise.allSettled([ |
| 14 | fetch('https://services.swpc.noaa.gov/json/planetary_k_index_1m.json', { |
| 15 | signal: AbortSignal.timeout(8000), |
| 16 | }).then(r => r.json()), |
| 17 | fetch('https://services.swpc.noaa.gov/json/alerts.json', { |
| 18 | signal: AbortSignal.timeout(8000), |
| 19 | }).then(r => r.json()), |
| 20 | fetch('https://services.swpc.noaa.gov/json/goes/primary/xray-flares-latest.json', { |
| 21 | signal: AbortSignal.timeout(8000), |
| 22 | }).then(r => r.json()), |
| 23 | ]); |
| 24 | |
| 25 | // Latest Kp index (geomagnetic storm indicator) |
| 26 | let kpIndex = 0; |
| 27 | let kpTimestamp = ''; |
| 28 | if (kpRes.status === 'fulfilled' && Array.isArray(kpRes.value) && kpRes.value.length > 0) { |
| 29 | const latest = kpRes.value[kpRes.value.length - 1]; |
| 30 | kpIndex = parseFloat(latest.kp_index || latest.Kp || 0); |
| 31 | kpTimestamp = latest.time_tag || ''; |
| 32 | } |
| 33 | |
| 34 | // Storm level from Kp |
| 35 | let stormLevel = 'Quiet'; |
| 36 | let stormColor = '#00E676'; |
| 37 | if (kpIndex >= 8) { stormLevel = 'Extreme (G5)'; stormColor = '#FF1744'; } |
| 38 | else if (kpIndex >= 7) { stormLevel = 'Severe (G4)'; stormColor = '#FF3D3D'; } |
| 39 | else if (kpIndex >= 6) { stormLevel = 'Strong (G3)'; stormColor = '#FF9500'; } |
| 40 | else if (kpIndex >= 5) { stormLevel = 'Moderate (G2)'; stormColor = '#FFD700'; } |
| 41 | else if (kpIndex >= 4) { stormLevel = 'Minor (G1)'; stormColor = '#FFD700'; } |
| 42 | else if (kpIndex >= 3) { stormLevel = 'Unsettled'; stormColor = '#D4AF37'; } |
| 43 | |
| 44 | // Recent alerts |
| 45 | const alerts: any[] = []; |
| 46 | if (alertsRes.status === 'fulfilled' && Array.isArray(alertsRes.value)) { |
| 47 | for (const alert of alertsRes.value.slice(0, 10)) { |
| 48 | alerts.push({ |
| 49 | id: alert.product_id || `alert-${Date.now()}`, |
| 50 | issue_datetime: alert.issue_datetime, |
| 51 | message: (alert.message || '').substring(0, 200), |
| 52 | }); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Recent solar flares |
| 57 | const flares: any[] = []; |
| 58 | if (flareRes.status === 'fulfilled' && Array.isArray(flareRes.value)) { |
| 59 | for (const flare of flareRes.value.slice(0, 5)) { |
| 60 | if (!flare.max_class) continue; |
| 61 | flares.push({ |
| 62 | class: flare.max_class, |
| 63 | begin: flare.begin_time, |
| 64 | peak: flare.max_time, |
| 65 | end: flare.end_time, |
| 66 | }); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return NextResponse.json({ |
| 71 | kp_index: kpIndex, |
| 72 | storm_level: stormLevel, |
| 73 | storm_color: stormColor, |
| 74 | kp_timestamp: kpTimestamp, |
| 75 | alerts, |
| 76 | solar_flares: flares, |
| 77 | timestamp: new Date().toISOString(), |
| 78 | }); |
| 79 | } catch (error) { |
| 80 | console.error('Space Weather API error:', error); |
| 81 | return NextResponse.json({ |
| 82 | kp_index: 0, storm_level: 'Unknown', storm_color: '#555', |
| 83 | alerts: [], solar_flares: [], error: 'Failed to fetch space weather data', |
| 84 | }, { status: 500 }); |
| 85 | } |
| 86 | } |
| 87 | |