54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { getYoutubeSubscriberCount } from "./youtube";
|
|
import { getRobloxFollowerCount } from "./roblox";
|
|
import { getDiscordMemberCount } from "./discord";
|
|
|
|
export interface FollowerCounts {
|
|
youtube: number | null;
|
|
roblox: number | null;
|
|
discord: number | null;
|
|
tiktok: number | null;
|
|
}
|
|
|
|
export interface FollowerInfo {
|
|
count: number;
|
|
label: string;
|
|
}
|
|
|
|
export async function getFollowerCounts(): Promise<FollowerCounts> {
|
|
const [youtube, roblox, discord] = await Promise.all([
|
|
getYoutubeSubscriberCount(),
|
|
getRobloxFollowerCount(),
|
|
getDiscordMemberCount(),
|
|
]);
|
|
|
|
const tiktokEnv = process.env.TIKTOK_FOLLOWERS;
|
|
const tiktok = tiktokEnv ? Number(tiktokEnv) : null;
|
|
|
|
return { youtube, roblox, discord, tiktok };
|
|
}
|
|
|
|
const followerLabels: Record<keyof FollowerCounts, string> = {
|
|
youtube: "ผู้ติดตาม",
|
|
roblox: "ผู้ติดตาม",
|
|
discord: "สมาชิก",
|
|
tiktok: "ผู้ติดตาม",
|
|
};
|
|
|
|
export function getCountForLink(name: string, counts: FollowerCounts): FollowerInfo | null {
|
|
const key = name.toLowerCase();
|
|
for (const [platform, label] of Object.entries(followerLabels)) {
|
|
const count = counts[platform as keyof FollowerCounts];
|
|
if (key.includes(platform) && count !== null) {
|
|
return { count, label };
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function formatCount(n: number): string {
|
|
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
|
|
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K`;
|
|
return n.toString();
|
|
}
|
|
|