20 lines
646 B
TypeScript
20 lines
646 B
TypeScript
export async function getYoutubeSubscriberCount(): Promise<number | null> {
|
|
const apiKey = process.env.YOUTUBE_API_KEY;
|
|
const channelId = process.env.YOUTUBE_CHANNEL_ID;
|
|
if (!apiKey || !channelId) return null;
|
|
|
|
try {
|
|
const res = await fetch(
|
|
`https://www.googleapis.com/youtube/v3/channels?part=statistics&id=${encodeURIComponent(channelId)}&key=${encodeURIComponent(apiKey)}`,
|
|
{ next: { revalidate: 3600 } }
|
|
);
|
|
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;
|
|
}
|
|
}
|