Read the official standings and results from the CSI portal.
The Campionato page showed hardcoded demo data. It now reads the real 2025/26 season (Campionato Open Misto Eccellenza, project 767, team 3359, Girone B) from the Livescore CSI Bologna portal. The portal has no documented API: we call the same endpoints its own pages call over ajax, so parsing must degrade gracefully. A single server route fetches them, caches for 6 hours and serves the last good payload on failure; the page falls back to the previous data when nothing is available. No browser ever contacts the portal, keeping the request count independent of how many players open the app. Also fixes the header, which claimed "Girone C - CSI Milano". Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { createFileRoute } from "@tanstack/react-router";
|
||||
import {
|
||||
CSI_GIRONE,
|
||||
parseClassifica,
|
||||
partiteDaEventi,
|
||||
urlClassifica,
|
||||
urlPartite,
|
||||
type DatiCsi,
|
||||
} from "@/lib/csi-core";
|
||||
|
||||
const SCADENZA_MS = 6 * 60 * 60 * 1000;
|
||||
|
||||
// ponytail: cache in memoria del processo, si perde ai cold start e non è
|
||||
// condivisa tra istanze. Basta per una squadra; se il portale CSI diventa
|
||||
// lento o le richieste crescono, spostare i dati in una tabella Supabase
|
||||
// riempita da un job cron (stesso pattern di promemoria-palloni).
|
||||
let cache: DatiCsi | undefined;
|
||||
let scadenza = 0;
|
||||
|
||||
async function scarica(url: string): Promise<string> {
|
||||
const res = await fetch(url, {
|
||||
headers: { "User-Agent": "CrAPP/1.0 (+https://crapvolley.it)" },
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
});
|
||||
if (!res.ok) throw new Error(`CSI ${res.status} su ${url}`);
|
||||
return res.text();
|
||||
}
|
||||
|
||||
async function leggiCsi(): Promise<DatiCsi> {
|
||||
const [html, json] = await Promise.all([scarica(urlClassifica()), scarica(urlPartite())]);
|
||||
const classifica = parseClassifica(html);
|
||||
const partite = partiteDaEventi(JSON.parse(json));
|
||||
if (classifica.length === 0 && partite.length === 0) {
|
||||
throw new Error("CSI: risposta senza classifica né partite");
|
||||
}
|
||||
return { classifica, partite, girone: CSI_GIRONE, aggiornato: new Date().toISOString() };
|
||||
}
|
||||
|
||||
export const Route = createFileRoute("/api/public/csi")({
|
||||
server: {
|
||||
handlers: {
|
||||
GET: async () => {
|
||||
if (cache && Date.now() < scadenza) return Response.json(cache);
|
||||
try {
|
||||
cache = await leggiCsi();
|
||||
scadenza = Date.now() + SCADENZA_MS;
|
||||
} catch (error) {
|
||||
console.error("csi", error);
|
||||
// Meglio un dato vecchio che nessun dato: il portale CSI cambia raramente.
|
||||
if (!cache) return new Response("CSI non raggiungibile", { status: 503 });
|
||||
}
|
||||
return Response.json(cache);
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user