fix(acr) : reuse live API
This commit is contained in:
+17
-28
@@ -1,45 +1,34 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
import type { YoutubeLiveInfo } from "../live/route";
|
||||||
|
|
||||||
type APISearchResponse = {
|
export async function GET(request: Request) {
|
||||||
items?: {
|
const response = await fetch(new URL("/api/live", request.url), {
|
||||||
id?: {
|
cache: "no-store",
|
||||||
videoId?: string;
|
});
|
||||||
};
|
|
||||||
}[];
|
|
||||||
};
|
|
||||||
|
|
||||||
const channelId = "UCC80OcUEuutVW37MC190JJg";
|
|
||||||
|
|
||||||
export async function GET() {
|
|
||||||
const apiKey = process.env.YOUTUBE_API_KEY;
|
|
||||||
|
|
||||||
if (!apiKey) {
|
|
||||||
return new NextResponse("YOUTUBE_API_KEY is not configured.", {
|
|
||||||
status: 500,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch(
|
|
||||||
`https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=${channelId}&eventType=live&type=video&maxResults=1&key=${apiKey}`,
|
|
||||||
{ cache: "no-store" },
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
return new NextResponse("Failed to search for live stream.", {
|
return new NextResponse("Failed to get live stream.", {
|
||||||
status: 502,
|
status: 502,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const data: APISearchResponse = await response.json();
|
const liveInfo: YoutubeLiveInfo = await response.json();
|
||||||
const videoId = data.items?.[0]?.id?.videoId;
|
|
||||||
|
|
||||||
if (!videoId) {
|
if (liveInfo === "none") {
|
||||||
return new NextResponse("No live stream found for this channel.", {
|
return new NextResponse("No live stream found for this channel.", {
|
||||||
status: 404,
|
status: 404,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const videoId = new URL(liveInfo.url).searchParams.get("v");
|
||||||
|
|
||||||
|
if (!videoId) {
|
||||||
|
return new NextResponse("Live stream video id was not found.", {
|
||||||
|
status: 502,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return NextResponse.redirect(
|
return NextResponse.redirect(
|
||||||
`https://www.youtube.com/live_chat?is_popout=${videoId}`,
|
`https://studio.youtube.com/live_chat?is_popout=1&v=${videoId}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user