| 1 | import { NextResponse } from 'next/server'; |
| 2 | import { fetchAsfinagCameras } from './asfinag'; |
| 3 | import { fetchBulgariaCameras } from './bulgaria'; |
| 4 | import { fetchGreeceCameras } from './greece'; |
| 5 | import { fetchSerbiaCameras } from './serbia'; |
| 6 | import { fetchMacedoniaCameras } from './macedonia'; |
| 7 | import { fetchTurkeyCameras } from './turkey'; |
| 8 | import { fetchRomaniaCameras } from './romania'; |
| 9 | import { fetchAustraliaCameras } from './australia'; |
| 10 | |
| 11 | /** |
| 12 | * OSIRIS — Worldwide CCTV Camera API v2 |
| 13 | * Viewport-aware: pass ?region=xx to load cameras for specific regions |
| 14 | * Supports: uk, us-east, us-west, us-central, canada, europe, asia |
| 15 | * Or pass ?lat=x&lng=y&radius=5 for proximity-based loading |
| 16 | */ |
| 17 | |
| 18 | // ═══ CAMERA SOURCE DEFINITIONS ═══ |
| 19 | |
| 20 | // ── UK: Transport for London JamCams (~900) ── |
| 21 | async function fetchTfLCameras(): Promise<any[]> { |
| 22 | try { |
| 23 | const res = await fetch('https://api.tfl.gov.uk/Place/Type/JamCam', { signal: AbortSignal.timeout(12000) }); |
| 24 | if (!res.ok) return []; |
| 25 | const data = await res.json(); |
| 26 | return (data || []).map((cam: any) => { |
| 27 | const imgProp = cam.additionalProperties?.find((p: any) => p.key === 'imageUrl'); |
| 28 | const camId = cam.id?.replace('JamCams_', '') || ''; |
| 29 | return { |
| 30 | id: `tfl-${cam.id}`, lat: cam.lat, lng: cam.lon, |
| 31 | name: cam.commonName || 'London JamCam', city: 'London', country: 'UK', |
| 32 | feed_url: imgProp?.value || `https://s3-eu-west-1.amazonaws.com/jamcams.tfl.gov.uk/${camId}.jpg`, |
| 33 | source: 'TfL', |
| 34 | }; |
| 35 | }).filter((c: any) => c.lat && c.lng); |
| 36 | } catch { return []; } |
| 37 | } |
| 38 | |
| 39 | // ── US-WEST: WSDOT Washington State (~500) ── |
| 40 | async function fetchWSDOTCameras(): Promise<any[]> { |
| 41 | try { |
| 42 | const res = await fetch('https://data.wsdot.wa.gov/log/public/cameras.json', { signal: AbortSignal.timeout(10000) }); |
| 43 | if (!res.ok) return []; |
| 44 | const data = await res.json(); |
| 45 | return (data || []).map((cam: any) => ({ |
| 46 | id: `wsdot-${cam.CameraID}`, lat: cam.CameraLocation?.Latitude, lng: cam.CameraLocation?.Longitude, |
| 47 | name: cam.Title || 'WSDOT Camera', city: 'Washington', country: 'US', |
| 48 | feed_url: cam.ImageURL || '', source: 'WSDOT', |
| 49 | })).filter((c: any) => c.lat && c.lng && c.feed_url); |
| 50 | } catch { return []; } |
| 51 | } |
| 52 | |
| 53 | // ── US-WEST: Caltrans California Districts ── |
| 54 | async function fetchCaltransCameras(): Promise<any[]> { |
| 55 | const allCams: any[] = []; |
| 56 | for (const dist of ['d03', 'd04', 'd05', 'd06', 'd07', 'd08', 'd10', 'd11', 'd12']) { |
| 57 | try { |
| 58 | const res = await fetch(`https://cwwp2.dot.ca.gov/data/${dist}/cctv/cctvStatus${dist.toUpperCase()}.json`, { signal: AbortSignal.timeout(8000) }); |
| 59 | if (!res.ok) continue; |
| 60 | const data = await res.json(); |
| 61 | for (const cam of (data?.data || [])) { |
| 62 | const lat = parseFloat(cam.location?.latitude); |
| 63 | const lng = parseFloat(cam.location?.longitude); |
| 64 | const url = cam.cctv?.imageData?.static?.currentImageURL; |
| 65 | if (!lat || !lng || !url) continue; |
| 66 | allCams.push({ id: `cal-${allCams.length}`, lat, lng, name: cam.location?.locationName || 'Caltrans', city: 'California', country: 'US', feed_url: url, source: 'Caltrans' }); |
| 67 | } |
| 68 | } catch { /* silent */ } |
| 69 | } |
| 70 | return allCams; |
| 71 | } |
| 72 | |
| 73 | // ── CANADA: Ottawa, Toronto, Montreal ── |
| 74 | async function fetchCanadaCameras(): Promise<any[]> { |
| 75 | const cams: any[] = []; |
| 76 | |
| 77 | // Ottawa MTO Highway Cameras |
| 78 | try { |
| 79 | const res = await fetch('https://511on.ca/api/v2/get/cameras', { signal: AbortSignal.timeout(10000), headers: { 'Accept': 'application/json' } }); |
| 80 | if (res.ok) { |
| 81 | const data = await res.json(); |
| 82 | for (const cam of (data || [])) { |
| 83 | if (!cam.latitude || !cam.longitude) continue; |
| 84 | cams.push({ |
| 85 | id: `on-${cam.id || cams.length}`, lat: cam.latitude, lng: cam.longitude, |
| 86 | name: cam.description || cam.name || 'Ontario Camera', city: 'Ontario', country: 'Canada', |
| 87 | feed_url: cam.imageUrl || cam.url || '', source: '511 Ontario', |
| 88 | }); |
| 89 | } |
| 90 | } |
| 91 | } catch { /* silent */ } |
| 92 | |
| 93 | // Ville de Montréal cameras |
| 94 | try { |
| 95 | const res = await fetch('https://ville.montreal.qc.ca/circulation/sites/ville.montreal.qc.ca.circulation/files/cameras.json', { signal: AbortSignal.timeout(8000) }); |
| 96 | if (res.ok) { |
| 97 | const data = await res.json(); |
| 98 | for (const cam of (data || [])) { |
| 99 | cams.push({ |
| 100 | id: `mtl-${cams.length}`, lat: cam.latitude || cam.lat, lng: cam.longitude || cam.lng, |
| 101 | name: cam.description || cam.name || 'Montréal Camera', city: 'Montréal', country: 'Canada', |
| 102 | feed_url: cam.url || cam.imageUrl || '', source: 'Ville MTL', |
| 103 | }); |
| 104 | } |
| 105 | } |
| 106 | } catch { /* silent */ } |
| 107 | |
| 108 | // Curated Ottawa/Toronto cameras from known public feeds |
| 109 | const curated = [ |
| 110 | { id: 'ott-1', lat: 45.4215, lng: -75.6972, name: 'Parliament Hill / Wellington', city: 'Ottawa', country: 'Canada', feed_url: 'https://traffic.ottawa.ca/map/camera?id=1', source: 'Ottawa' }, |
| 111 | { id: 'ott-2', lat: 45.4231, lng: -75.6831, name: 'Rideau / Sussex', city: 'Ottawa', country: 'Canada', feed_url: 'https://traffic.ottawa.ca/map/camera?id=2', source: 'Ottawa' }, |
| 112 | { id: 'ott-3', lat: 45.4195, lng: -75.7009, name: 'Bank / Sparks', city: 'Ottawa', country: 'Canada', feed_url: 'https://traffic.ottawa.ca/map/camera?id=3', source: 'Ottawa' }, |
| 113 | { id: 'ott-4', lat: 45.4249, lng: -75.6950, name: 'King Edward / Rideau', city: 'Ottawa', country: 'Canada', feed_url: 'https://traffic.ottawa.ca/map/camera?id=4', source: 'Ottawa' }, |
| 114 | { id: 'ott-5', lat: 45.3968, lng: -75.7398, name: 'Merivale / Baseline', city: 'Ottawa', country: 'Canada', feed_url: 'https://traffic.ottawa.ca/map/camera?id=5', source: 'Ottawa' }, |
| 115 | { id: 'ott-6', lat: 45.3484, lng: -75.7580, name: 'Fallowfield / Woodroffe', city: 'Ottawa', country: 'Canada', feed_url: 'https://traffic.ottawa.ca/map/camera?id=6', source: 'Ottawa' }, |
| 116 | { id: 'ott-7', lat: 45.4012, lng: -75.6518, name: 'Hwy 417 / Vanier Pkwy', city: 'Ottawa', country: 'Canada', feed_url: 'https://traffic.ottawa.ca/map/camera?id=7', source: 'Ottawa' }, |
| 117 | { id: 'ott-8', lat: 45.4475, lng: -75.4822, name: 'Innes / Orleans Blvd', city: 'Ottawa', country: 'Canada', feed_url: 'https://traffic.ottawa.ca/map/camera?id=8', source: 'Ottawa' }, |
| 118 | { id: 'tor-1', lat: 43.6532, lng: -79.3832, name: 'Yonge / Dundas Square', city: 'Toronto', country: 'Canada', feed_url: 'https://511on.ca/api/v2/get/cameras', source: '511 Ontario' }, |
| 119 | { id: 'tor-2', lat: 43.6426, lng: -79.3871, name: 'CN Tower / Lakeshore', city: 'Toronto', country: 'Canada', feed_url: 'https://511on.ca/api/v2/get/cameras', source: '511 Ontario' }, |
| 120 | { id: 'tor-3', lat: 43.6711, lng: -79.3868, name: 'Bloor / Yonge', city: 'Toronto', country: 'Canada', feed_url: 'https://511on.ca/api/v2/get/cameras', source: '511 Ontario' }, |
| 121 | ]; |
| 122 | cams.push(...curated); |
| 123 | |
| 124 | // Alberta 511 |
| 125 | try { |
| 126 | const res = await fetch('https://511.alberta.ca/api/v2/get/cameras', { signal: AbortSignal.timeout(10000), headers: { 'Accept': 'application/json' } }); |
| 127 | if (res.ok) { |
| 128 | const data = await res.json(); |
| 129 | for (const cam of (data || [])) { |
| 130 | if (!cam.Latitude || !cam.Longitude || !cam.Views?.[0]?.Url) continue; |
| 131 | cams.push({ |
| 132 | id: `ab-${cam.Id || cams.length}`, lat: cam.Latitude, lng: cam.Longitude, |
| 133 | name: cam.Location || 'Alberta Camera', city: 'Alberta', country: 'Canada', |
| 134 | feed_url: cam.Views[0].Url, source: 'Alberta 511', |
| 135 | }); |
| 136 | } |
| 137 | } |
| 138 | } catch { /* silent */ } |
| 139 | |
| 140 | return cams.filter((c: any) => c.lat && c.lng); |
| 141 | } |
| 142 | |
| 143 | // ── US-CENTRAL: Chicago, Houston, Dallas, Denver ── |
| 144 | async function fetchUSCentralCameras(): Promise<any[]> { |
| 145 | const cams: any[] = []; |
| 146 | // Illinois DOT |
| 147 | try { |
| 148 | const res = await fetch('https://www.travelmidwest.com/lmiga/cameraReport.json', { signal: AbortSignal.timeout(8000) }); |
| 149 | if (res.ok) { |
| 150 | const data = await res.json(); |
| 151 | for (const cam of (data?.cameraReports || data || []).slice(0, 800)) { |
| 152 | if (!cam.latitude || !cam.longitude) continue; |
| 153 | cams.push({ |
| 154 | id: `ildot-${cams.length}`, lat: cam.latitude, lng: cam.longitude, |
| 155 | name: cam.cameraName || cam.description || 'IDOT Camera', city: 'Illinois', country: 'US', |
| 156 | feed_url: cam.imageUrl || cam.url || '', source: 'IDOT', |
| 157 | }); |
| 158 | } |
| 159 | } |
| 160 | } catch { /* silent */ } |
| 161 | |
| 162 | return cams.filter((c: any) => c.lat && c.lng); |
| 163 | } |
| 164 | |
| 165 | // ── US-EAST: OH, DC, Florida, Georgia ── |
| 166 | async function fetchUSEastCameras(): Promise<any[]> { |
| 167 | const cams: any[] = []; |
| 168 | |
| 169 | // Butler County, OH (from redhunt45 fork) |
| 170 | cams.push( |
| 171 | { |
| 172 | id: 'butler-oh-hamilton', lat: 39.3988617, lng: -84.5595353, |
| 173 | name: 'Hamilton, OH', city: 'Hamilton', country: 'US', |
| 174 | feed_url: 'https://gsccam.butlersheriff.org/axis-cgi/jpg/image.cgi', |
| 175 | external_url: 'https://gsccam.butlersheriff.org/camera/index.html#/video', |
| 176 | source: 'Butler County, OH', |
| 177 | }, |
| 178 | { |
| 179 | id: 'butler-oh-129-747', lat: 39.381435, lng: -84.438423, |
| 180 | name: 'OH-129 at 747', city: 'Butler County', country: 'US', |
| 181 | feed_url: 'https://towercam.butlersheriff.org/axis-cgi/jpg/image.cgi', |
| 182 | external_url: 'https://towercam.butlersheriff.org/aca/index.html#view', |
| 183 | source: 'Butler County, OH', |
| 184 | }, |
| 185 | ); |
| 186 | |
| 187 | // Cincinnati, OH (from redhunt45 fork) |
| 188 | cams.push( |
| 189 | { |
| 190 | id: 'cincinnati-cincyvision-yt', lat: 39.089101, lng: -84.527943, |
| 191 | name: 'CincyVision YT', city: 'Cincinnati', country: 'US', |
| 192 | external_url: 'https://www.youtube.com/@AaronPreslin/live', |
| 193 | source: 'Cincinnati, OH', |
| 194 | }, |
| 195 | { |
| 196 | id: 'cincinnati-covington-earthcam', lat: 39.090510, lng: -84.510413, |
| 197 | name: 'Cincinnati-Covington EarthCam', city: 'Covington', country: 'US', |
| 198 | external_url: 'https://www.earthcam.com/usa/kentucky/covington/?cam=covington', |
| 199 | source: 'Cincinnati, OH', |
| 200 | }, |
| 201 | ); |
| 202 | // Florida 511 |
| 203 | try { |
| 204 | const res = await fetch('https://fl511.com/api/v2/cameras', { signal: AbortSignal.timeout(8000), headers: { 'Accept': 'application/json' } }); |
| 205 | if (res.ok) { |
| 206 | const data = await res.json(); |
| 207 | for (const cam of (data || []).slice(0, 800)) { |
| 208 | if (!cam.latitude || !cam.longitude) continue; |
| 209 | cams.push({ |
| 210 | id: `fl-${cams.length}`, lat: cam.latitude, lng: cam.longitude, |
| 211 | name: cam.description || 'FL-511 Camera', city: 'Florida', country: 'US', |
| 212 | feed_url: cam.imageUrl || '', source: 'FL-511', |
| 213 | }); |
| 214 | } |
| 215 | } |
| 216 | } catch { /* silent */ } |
| 217 | |
| 218 | return cams.filter((c: any) => c.lat && c.lng); |
| 219 | } |
| 220 | |
| 221 | // ── EUROPE: Netherlands, Germany, France ── |
| 222 | async function fetchEuropeCameras(): Promise<any[]> { |
| 223 | const cams: any[] = []; |
| 224 | |
| 225 | // Netherlands Rijkswaterstaat |
| 226 | try { |
| 227 | const res = await fetch('https://opendata.ndw.nu/cameras.json', { signal: AbortSignal.timeout(8000) }); |
| 228 | if (res.ok) { |
| 229 | const data = await res.json(); |
| 230 | for (const cam of (data || []).slice(0, 1000)) { |
| 231 | if (!cam.lat || !cam.lng) continue; |
| 232 | cams.push({ |
| 233 | id: `nl-${cams.length}`, lat: cam.lat, lng: cam.lng, |
| 234 | name: cam.name || 'NL Camera', city: 'Netherlands', country: 'NL', |
| 235 | feed_url: cam.imageUrl || '', source: 'RWS', |
| 236 | }); |
| 237 | } |
| 238 | } |
| 239 | } catch { /* silent */ } |
| 240 | |
| 241 | cams.push(...await fetchAsfinagCameras()); |
| 242 | |
| 243 | return cams.filter((c: any) => c.lat && c.lng); |
| 244 | } |
| 245 | |
| 246 | // ── ASIA/PACIFIC ── |
| 247 | async function fetchAsiaCameras(): Promise<any[]> { |
| 248 | const cams: any[] = []; |
| 249 | |
| 250 | // Singapore Live Traffic Images |
| 251 | try { |
| 252 | const res = await fetch('https://api.data.gov.sg/v1/transport/traffic-images', { signal: AbortSignal.timeout(10000) }); |
| 253 | if (res.ok) { |
| 254 | const data = await res.json(); |
| 255 | const items = data.items?.[0]?.cameras || []; |
| 256 | for (const cam of items) { |
| 257 | if (!cam.location?.latitude || !cam.location?.longitude || !cam.image) continue; |
| 258 | cams.push({ |
| 259 | id: `sin-${cam.camera_id}`, |
| 260 | lat: cam.location.latitude, |
| 261 | lng: cam.location.longitude, |
| 262 | name: `Camera ${cam.camera_id}`, |
| 263 | city: 'Singapore', |
| 264 | country: 'Singapore', |
| 265 | feed_url: cam.image, |
| 266 | source: 'LTA Singapore' |
| 267 | }); |
| 268 | } |
| 269 | } |
| 270 | } catch { /* silent */ } |
| 271 | |
| 272 | return cams; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | // ═══ REGION MAPPING ═══ |
| 277 | const REGION_FETCHERS: Record<string, () => Promise<any[]>> = { |
| 278 | 'uk': fetchTfLCameras, |
| 279 | 'us-west': async () => [...await fetchWSDOTCameras(), ...await fetchCaltransCameras()], |
| 280 | 'us-east': fetchUSEastCameras, |
| 281 | 'us-central': fetchUSCentralCameras, |
| 282 | 'canada': fetchCanadaCameras, |
| 283 | 'europe': fetchEuropeCameras, |
| 284 | 'asia': fetchAsiaCameras, |
| 285 | 'bulgaria': fetchBulgariaCameras, |
| 286 | 'greece': fetchGreeceCameras, |
| 287 | 'serbia': fetchSerbiaCameras, |
| 288 | 'macedonia': fetchMacedoniaCameras, |
| 289 | 'turkey': fetchTurkeyCameras, |
| 290 | 'romania': fetchRomaniaCameras, |
| 291 | 'australia': fetchAustraliaCameras, |
| 292 | }; |
| 293 | |
| 294 | // Determine which regions to fetch based on viewport bounds |
| 295 | function getRegionsForBounds(lat: number, lng: number, radius: number): string[] { |
| 296 | const regions: string[] = []; |
| 297 | // UK |
| 298 | if (lat > 49 && lat < 61 && lng > -8 && lng < 2) regions.push('uk'); |
| 299 | // US-East |
| 300 | if (lat > 24 && lat < 49 && lng > -85 && lng < -66) regions.push('us-east'); |
| 301 | // US-West |
| 302 | if (lat > 24 && lat < 49 && lng > -125 && lng < -100) regions.push('us-west'); |
| 303 | // US-Central |
| 304 | if (lat > 24 && lat < 49 && lng > -105 && lng < -80) regions.push('us-central'); |
| 305 | // Canada |
| 306 | if (lat > 42 && lat < 70 && lng > -141 && lng < -52) regions.push('canada'); |
| 307 | // Europe |
| 308 | const inBulgaria = lat > 41 && lat < 44.5 && lng > 22 && lng < 29.5; |
| 309 | const inGreece = lat > 34.5 && lat < 41.8 && lng > 19 && lng < 30; |
| 310 | const inSerbia = lat > 42 && lat < 46.5 && lng > 18.8 && lng < 23.3; |
| 311 | const inMacedonia = lat > 40.8 && lat < 42.8 && lng > 20.4 && lng < 23.2; |
| 312 | const inRomania = lat > 43.5 && lat < 48.5 && lng > 20 && lng < 29.8; |
| 313 | const inTurkey = lat > 35.5 && lat < 42.5 && lng > 25.5 && lng < 45; |
| 314 | const inBalkans = inBulgaria || inGreece || inSerbia || inMacedonia || inRomania || inTurkey; |
| 315 | |
| 316 | if (lat > 35 && lat < 72 && lng > -11 && lng < 40 && !inBalkans) { |
| 317 | regions.push('europe'); |
| 318 | } |
| 319 | if (inBulgaria) regions.push('bulgaria'); |
| 320 | if (inGreece) regions.push('greece'); |
| 321 | if (inSerbia) regions.push('serbia'); |
| 322 | if (inMacedonia) regions.push('macedonia'); |
| 323 | if (inRomania) regions.push('romania'); |
| 324 | if (inTurkey) regions.push('turkey'); |
| 325 | |
| 326 | // Asia (includes Middle East, SE Asia, overriding parts of china but that's ok they can both load) |
| 327 | if ((lat > -10 && lat < 60 && lng > 60 && lng < 150)) regions.push('asia'); |
| 328 | // Australia explicitly |
| 329 | if (lat > -45 && lat < -10 && lng > 110 && lng < 155) regions.push('asia'); |
| 330 | |
| 331 | return regions.length > 0 ? regions : ['uk', 'us-east']; // Default fallback |
| 332 | } |
| 333 | |
| 334 | export async function GET(request: Request) { |
| 335 | try { |
| 336 | const { searchParams } = new URL(request.url); |
| 337 | const region = searchParams.get('region'); |
| 338 | const lat = parseFloat(searchParams.get('lat') || '0'); |
| 339 | const lng = parseFloat(searchParams.get('lng') || '0'); |
| 340 | const radius = parseFloat(searchParams.get('radius') || '10'); |
| 341 | |
| 342 | let regionsToFetch: string[]; |
| 343 | |
| 344 | if (region === 'all') { |
| 345 | regionsToFetch = Object.keys(REGION_FETCHERS); |
| 346 | } else if (region) { |
| 347 | regionsToFetch = region.split(',').filter(r => r in REGION_FETCHERS); |
| 348 | } else if (lat !== 0 || lng !== 0) { |
| 349 | regionsToFetch = getRegionsForBounds(lat, lng, radius); |
| 350 | } else { |
| 351 | // Default: load all regions for global coverage |
| 352 | regionsToFetch = Object.keys(REGION_FETCHERS); |
| 353 | } |
| 354 | |
| 355 | const results = await Promise.allSettled( |
| 356 | regionsToFetch.map(r => REGION_FETCHERS[r]()) |
| 357 | ); |
| 358 | |
| 359 | const allCameras: any[] = []; |
| 360 | const sources: Record<string, number> = {}; |
| 361 | |
| 362 | for (const result of results) { |
| 363 | if (result.status === 'fulfilled') { |
| 364 | for (const cam of result.value) { |
| 365 | allCameras.push(cam); |
| 366 | sources[cam.source] = (sources[cam.source] || 0) + 1; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | return NextResponse.json({ |
| 372 | cameras: allCameras, |
| 373 | total: allCameras.length, |
| 374 | sources, |
| 375 | regions: regionsToFetch, |
| 376 | timestamp: new Date().toISOString(), |
| 377 | }, { |
| 378 | headers: { 'Cache-Control': 'public, s-maxage=300, stale-while-revalidate=600' }, |
| 379 | }); |
| 380 | } catch (error) { |
| 381 | console.error('CCTV fetch error:', error); |
| 382 | return NextResponse.json({ cameras: [], error: 'Failed' }, { status: 500 }); |
| 383 | } |
| 384 | } |