Elffuss BraidHub

kikocis/osiris public

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