feat(home): tram arrival widget for Ул. Антонова-Овсеенко
All checks were successful
Deploy / deploy (push) Successful in 3m10s
All checks were successful
Deploy / deploy (push) Successful in 3m10s
Adds a live transit widget on the home screen showing upcoming trams
at both directions of the stop: toward Новочеркасская (stopID 16226)
and toward пр. Большевиков (stopID 16354).
- /api/transport proxies the СПб ORGP endpoint /stop/{id}/arriving
(DataTables POST format, JSON response with route number + minutes).
No auth required, free.
- TransportWidget renders two glassmorphism cards with route badges,
minutes-to-arrival, wheelchair indicator; imminent (<=2 min) arrivals
get a colored highlight. Filters to trams 23/27/39; refreshes every 30s.
- Route colors: 23 blue, 27 amber, 39 purple.
This commit is contained in:
67
app/api/transport/route.ts
Normal file
67
app/api/transport/route.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
export const dynamic = 'force-dynamic'
|
||||
import { NextResponse } from 'next/server'
|
||||
|
||||
const ORGP_BASE = 'https://transport.orgp.spb.ru'
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const { searchParams } = new URL(req.url)
|
||||
const stopId = searchParams.get('stopId')
|
||||
if (!stopId || !/^\d+$/.test(stopId)) {
|
||||
return NextResponse.json({ error: 'stopId required (digits)' }, { status: 400 })
|
||||
}
|
||||
|
||||
const body = new URLSearchParams({
|
||||
sEcho: '1',
|
||||
iColumns: '5',
|
||||
sColumns: 'index,routeNumber,timeToArrive,parkNumber,wheelchair',
|
||||
iDisplayStart: '0',
|
||||
iDisplayLength: '-1',
|
||||
sNames: 'index,routeNumber,timeToArrive,parkNumber,wheelchair',
|
||||
})
|
||||
|
||||
try {
|
||||
const upstream = await fetch(
|
||||
`${ORGP_BASE}/Portal/transport/stop/${encodeURIComponent(stopId)}/arriving`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
||||
'Accept': 'application/json, text/javascript, */*; q=0.01',
|
||||
},
|
||||
body: body.toString(),
|
||||
cache: 'no-store',
|
||||
// @ts-ignore next-internal option for timeout
|
||||
next: { revalidate: 0 },
|
||||
}
|
||||
)
|
||||
|
||||
if (!upstream.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: `upstream_${upstream.status}`, arrivals: [] },
|
||||
{ status: 502 }
|
||||
)
|
||||
}
|
||||
|
||||
const data = await upstream.json()
|
||||
const arrivals = Array.isArray(data?.aaData)
|
||||
? data.aaData.map((row: any[]) => ({
|
||||
route: String(row[1] ?? ''),
|
||||
minutes: Number(row[2] ?? 0),
|
||||
park: String(row[3] ?? ''),
|
||||
wheelchair: Boolean(row[4]),
|
||||
}))
|
||||
: []
|
||||
|
||||
return NextResponse.json({
|
||||
stopId,
|
||||
arrivals,
|
||||
fetchedAt: new Date().toISOString(),
|
||||
})
|
||||
} catch (e: any) {
|
||||
return NextResponse.json(
|
||||
{ error: e?.message || 'fetch_failed', arrivals: [] },
|
||||
{ status: 502 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user