initial commit
This commit is contained in:
24
scripts/probe-api.mjs
Normal file
24
scripts/probe-api.mjs
Normal file
@@ -0,0 +1,24 @@
|
||||
const key = process.env.RIDEBUS_API_KEY;
|
||||
if (!key) { console.log('RideBus API key is not configured.'); process.exit(1); }
|
||||
const base = (process.env.RIDEBUS_API_BASE_URL || 'https://api.ridebus.by/').replace(/\/+$/, '') + '/';
|
||||
const cityId = process.env.BOARD_CITY_ID || '1';
|
||||
async function get(path) {
|
||||
const response = await fetch(new URL(path, base), {
|
||||
headers: { 'X-API-Key': key, 'X-City-Id': cityId, Accept: 'application/json' },
|
||||
signal: AbortSignal.timeout(12000), redirect: 'error',
|
||||
});
|
||||
if (!response.ok) throw new Error(`API returned HTTP ${response.status}`);
|
||||
return response.json();
|
||||
}
|
||||
try {
|
||||
const stops = await get('api/v1/stops');
|
||||
const matches = stops.filter(stop => /швейн/i.test(stop.name));
|
||||
console.log(JSON.stringify(matches.map(({ id, name, description, routes_count }) => ({ id, name, description, routes_count })), null, 2));
|
||||
for (const stop of matches) {
|
||||
const [details, eta] = await Promise.all([get(`api/v1/stops/${stop.id}`), get(`api/v1/eta/stop/${stop.id}`)]);
|
||||
console.log(JSON.stringify({ stopId: stop.id, routes: details.routes.map(({ id, route_number, name }) => ({ id, route_number, name })), upcomingCount: eta.routes.reduce((count, route) => count + (route.arrivals?.length || 0), 0) }, null, 2));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error.cause?.code || error.message);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
Reference in New Issue
Block a user