Elffuss BraidHub

kikocis/osiris public

master · 2.5 KiBRawPermalink
1const ASFINAG_WEBCAMS_URL = 'https://odo.asfinag.at/odo/rest/sec/resource/001/json/webcams?language=atDE';
2const ASFINAG_CACHE_TTL_MS = 60 * 60 * 1000;
3
4const ASFINAG_HEADERS = {
5 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0',
6 'Accept': 'application/json',
7 'Accept-Language': 'en,en-US;q=0.9,de;q=0.8',
8 'Referer': 'https://www.asfinag.at/',
9 'Content-Type': 'application/json; charset=utf-8',
10 'Authorization': 'Basic bWFwX3dpZGdldDp0ZWdkaXc=',
11 'Origin': 'https://www.asfinag.at',
12};
13
14interface AsfinagWebcam {
15 wcs_id?: string;
16 wgs84_lat?: number;
17 wgs84_lon?: number;
18 position_txt?: string;
19 direction_txt?: string;
20 url_campic?: string;
21}
22
23export interface CctvCamera {
24 id: string;
25 lat: number;
26 lng: number;
27 name: string;
28 city: string;
29 country: string;
30 feed_url: string;
31 source: string;
32}
33
34let cachedCameras: CctvCamera[] | null = null;
35let cacheExpiresAt = 0;
36let pendingFetch: Promise<CctvCamera[]> | null = null;
37
38function toAsfinagCamera(cam: AsfinagWebcam): CctvCamera | null {
39 if (!cam.wcs_id || !cam.wgs84_lat || !cam.wgs84_lon || !cam.url_campic) {
40 return null;
41 }
42
43 // Skip Hungarian road authority (Utinform) cameras — feeds are unavailable
44 if (cam.wcs_id.startsWith('Utinform')) {
45 return null;
46 }
47
48 return {
49 id: `asfinag-${cam.wcs_id}`,
50 lat: cam.wgs84_lat,
51 lng: cam.wgs84_lon,
52 name: cam.position_txt || cam.direction_txt || 'ASFINAG Webcam',
53 city: 'Austria',
54 country: 'Austria',
55 feed_url: cam.url_campic,
56 source: 'ASFINAG',
57 };
58}
59
60async function fetchFreshAsfinagCameras(): Promise<CctvCamera[]> {
61 try {
62 const res = await fetch(ASFINAG_WEBCAMS_URL, {
63 signal: AbortSignal.timeout(12000),
64 headers: ASFINAG_HEADERS,
65 });
66
67 if (!res.ok) return [];
68
69 const data = await res.json();
70 if (!Array.isArray(data)) return [];
71
72 return data
73 .map(toAsfinagCamera)
74 .filter((cam): cam is CctvCamera => cam !== null);
75 } catch {
76 return [];
77 }
78}
79
80export async function fetchAsfinagCameras(): Promise<CctvCamera[]> {
81 const now = Date.now();
82 if (cachedCameras && now < cacheExpiresAt) {
83 return cachedCameras;
84 }
85
86 if (!pendingFetch) {
87 pendingFetch = fetchFreshAsfinagCameras()
88 .then((cameras) => {
89 if (cameras.length > 0) {
90 cachedCameras = cameras;
91 cacheExpiresAt = Date.now() + ASFINAG_CACHE_TTL_MS;
92 }
93
94 return cachedCameras ?? cameras;
95 })
96 .finally(() => {
97 pendingFetch = null;
98 });
99 }
100
101 return pendingFetch;
102}