Elffuss BraidHub

kikocis/osiris public

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