20 lines
680 B
TypeScript
20 lines
680 B
TypeScript
import "server-only";
|
|
import { getYoutubeFollowerConfig } from "@/lib/config/server";
|
|
|
|
export async function getYoutubeSubscriberCount(): Promise<number | null> {
|
|
try {
|
|
const { apiKey, channelId } = getYoutubeFollowerConfig();
|
|
const res = await fetch(
|
|
`https://www.googleapis.com/youtube/v3/channels?part=statistics&id=${encodeURIComponent(channelId)}&key=${encodeURIComponent(apiKey)}`,
|
|
{ cache: "no-store", signal: AbortSignal.timeout(5000) }
|
|
);
|
|
if (!res.ok) return null;
|
|
|
|
const data = await res.json();
|
|
const count = data.items?.[0]?.statistics?.subscriberCount;
|
|
return count ? Number(count) : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|