Elffuss BraidHub

kikocis/osiris public

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