reduce traffic
This commit is contained in:
78
app/api/live/route.ts
Normal file
78
app/api/live/route.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { fetchMatchesAndTeamsFifa, fetchFifaScores, applyFifaScores, attachFifaGoals } from "@/lib/feeds";
|
||||
import type { Match, Team } from "@/lib/types";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const RECENT_MS = 60 * 60 * 1000;
|
||||
const MATCH_DURATION = 2.5 * 60 * 60 * 1000;
|
||||
|
||||
function isRecentlyFinished(m: Match): boolean {
|
||||
if (m.status !== "FINISHED") return false;
|
||||
const kickoff = new Date(m.utcDate).getTime();
|
||||
if (isNaN(kickoff)) return false;
|
||||
return kickoff + MATCH_DURATION > Date.now() - RECENT_MS;
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const locale = request.nextUrl.searchParams.get("locale") || "de";
|
||||
try {
|
||||
const { matches: rawMatches, teams } = await fetchMatchesAndTeamsFifa(locale);
|
||||
|
||||
const relevant = rawMatches.filter(m =>
|
||||
m.status === "LIVE" || m.status === "IN_PLAY" || m.status === "PAUSED" || isRecentlyFinished(m),
|
||||
);
|
||||
|
||||
if (relevant.length === 0) {
|
||||
return NextResponse.json({
|
||||
updatedAt: new Date().toISOString(),
|
||||
liveCount: 0,
|
||||
recentCount: 0,
|
||||
teams: [] as Team[],
|
||||
matches: [] as Match[],
|
||||
});
|
||||
}
|
||||
|
||||
let fifaData: Awaited<ReturnType<typeof fetchFifaScores>>;
|
||||
try {
|
||||
fifaData = await fetchFifaScores(locale);
|
||||
} catch {
|
||||
return NextResponse.json({
|
||||
updatedAt: new Date().toISOString(),
|
||||
liveCount: relevant.filter(m => m.status !== "FINISHED").length,
|
||||
recentCount: relevant.filter(m => m.status === "FINISHED").length,
|
||||
teams,
|
||||
matches: relevant,
|
||||
goalsFailed: true,
|
||||
});
|
||||
}
|
||||
|
||||
let matches = applyFifaScores(relevant, teams, fifaData);
|
||||
let goalsFailed = false;
|
||||
|
||||
try {
|
||||
matches = await attachFifaGoals(matches, fifaData, locale);
|
||||
} catch {
|
||||
goalsFailed = true;
|
||||
}
|
||||
|
||||
const liveCount = matches.filter(m => m.status === "LIVE" || m.status === "IN_PLAY" || m.status === "PAUSED").length;
|
||||
const recentCount = matches.filter(m => m.status === "FINISHED" && isRecentlyFinished(m)).length;
|
||||
|
||||
return NextResponse.json({
|
||||
updatedAt: new Date().toISOString(),
|
||||
liveCount,
|
||||
recentCount,
|
||||
teams,
|
||||
matches,
|
||||
goalsFailed,
|
||||
});
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : "unbekannter Fehler";
|
||||
return NextResponse.json(
|
||||
{ error: "Live-Feed nicht erreichbar", detail: message },
|
||||
{ status: 502 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { fetchMatchesAndTeamsFifa, fetchOdds, attachOdds, attachKOOdds, fetchFifaScores, applyFifaScores, attachFifaGoals } from "@/lib/feeds";
|
||||
import { fetchMatchesAndTeamsFifa, fetchOdds, attachOdds, attachKOOdds, attachFifaLiveData } from "@/lib/feeds";
|
||||
import { computeGroupTables, computeThirdPlaceTable } from "@/lib/standings";
|
||||
import { qualifiedThirdGroups, resolveAnnexC } from "@/lib/bracket";
|
||||
import { securelyQualifiedThirdTeams } from "@/lib/third-place-security";
|
||||
@@ -27,17 +27,11 @@ export async function GET(request: NextRequest) {
|
||||
matches = attachKOOdds(matches, teams, odds);
|
||||
}
|
||||
|
||||
// FIFA-Live-Scores als häufiger gecachtes Overlay
|
||||
// FIFA-Live-Scores als häufiger gecachtes Overlay (Pipeline: Scores → Goals)
|
||||
try {
|
||||
const fifaData = await fetchFifaScores(locale);
|
||||
matches = applyFifaScores(matches, teams, fifaData);
|
||||
try {
|
||||
matches = await attachFifaGoals(matches, fifaData, locale);
|
||||
} catch (err) {
|
||||
console.warn("[fifa] Goals fehlgeschlagen:", err instanceof Error ? err.message : err);
|
||||
}
|
||||
matches = await attachFifaLiveData(matches, teams, locale);
|
||||
} catch (err) {
|
||||
console.warn("[fifa] Scores fehlgeschlagen:", err instanceof Error ? err.message : err);
|
||||
console.warn("[fifa] Live-Overlay fehlgeschlagen:", err instanceof Error ? err.message : err);
|
||||
}
|
||||
|
||||
const groupTables = computeGroupTables(teams, matches);
|
||||
|
||||
Reference in New Issue
Block a user