Files
wm-projekt/scripts/fetch-crests.ts
2026-06-24 22:25:08 -05:00

84 lines
2.3 KiB
TypeScript

/**
* Einmal-Skript: Lädt alle Team-Crests von football-data.org herunter
* und speichert sie unter public/crests/{teamId}.svg.
*
* Ausführung: npx tsx --env-file=.env scripts/fetch-crests.ts
*/
import * as fs from "node:fs";
import * as path from "node:path";
const CREST_BASE = "https://crests.football-data.org";
const OUT_DIR = path.resolve(import.meta.dirname, "..", "public", "crests");
const FD_BASE = "https://api.football-data.org/v4";
const FD_COMP = "WC";
function fdHeaders(): Record<string, string> {
const token = process.env.FOOTBALL_DATA_TOKEN;
if (!token) {
console.error("[fehler] FOOTBALL_DATA_TOKEN nicht gesetzt. Bitte .env prüfen.");
process.exit(1);
}
return { "X-Auth-Token": token };
}
interface FdTeam {
id: number;
name: string;
tla?: string | null;
crest?: string | null;
}
async function main() {
console.log("[fetch-crests] Rufe Team-Daten von football-data.org ab…");
const res = await fetch(`${FD_BASE}/competitions/${FD_COMP}/teams`, {
headers: fdHeaders(),
});
if (!res.ok) {
console.error(`[fehler] football-data API: ${res.status} ${res.statusText}`);
process.exit(1);
}
const data = (await res.json()) as { teams: FdTeam[] };
const teams = data.teams ?? [];
console.log(`[fetch-crests] ${teams.length} Teams gefunden.`);
fs.mkdirSync(OUT_DIR, { recursive: true });
let ok = 0;
let failed = 0;
const failures: string[] = [];
for (const t of teams) {
const id = String(t.id);
const url = t.crest ?? `${CREST_BASE}/${id}.svg`;
const outPath = path.join(OUT_DIR, `${id}.svg`);
try {
const imgRes = await fetch(url);
if (!imgRes.ok) {
failed++;
failures.push(`${t.name} (id=${id}): HTTP ${imgRes.status}`);
continue;
}
const buf = Buffer.from(await imgRes.arrayBuffer());
fs.writeFileSync(outPath, buf);
ok++;
} catch (err) {
failed++;
failures.push(`${t.name} (id=${id}): ${err instanceof Error ? err.message : String(err)}`);
}
}
console.log(`\n[fetch-crests] Fertig: ${ok} geladen, ${failed} fehlgeschlagen.`);
if (failures.length > 0) {
console.log("[fetch-crests] Fehlgeschlagene:");
for (const f of failures) console.log(` - ${f}`);
}
}
main().catch((err) => {
console.error("[fehler]", err);
process.exit(1);
});