60 lines
2.0 KiB
TypeScript
60 lines
2.0 KiB
TypeScript
import { getRedisClient } from "@/lib/redis";
|
|
import { sendDiscordNotification } from "@/lib/discord";
|
|
import { getLatestTiktokVideo, getTiktokProfile } from "@/lib/followers/tiktok";
|
|
|
|
async function checkTiktokFeed() {
|
|
try {
|
|
const latestVideo = await getLatestTiktokVideo();
|
|
if (!latestVideo) return;
|
|
|
|
const redis = await getRedisClient();
|
|
const redisKey = "erika:tiktok_last_video_id";
|
|
const lastVideoId = await redis.get(redisKey);
|
|
|
|
if (lastVideoId === latestVideo.id) {
|
|
return; // Already notified
|
|
}
|
|
|
|
let officialAuthorName = "TikTok";
|
|
let officialAvatarUrl: string | undefined = undefined;
|
|
try {
|
|
const profile = await getTiktokProfile();
|
|
if (profile.displayName) officialAuthorName = profile.displayName;
|
|
if (profile.avatarUrl) officialAvatarUrl = profile.avatarUrl;
|
|
} catch (e) {
|
|
console.error("[TikTok Feed] Failed to fetch profile:", e);
|
|
}
|
|
|
|
const message = `ลงคลิปใหม่แล้วววสสสสส ${latestVideo.shareUrl}`;
|
|
const embed = {
|
|
title: latestVideo.title || "คลิปใหม่จาก TikTok",
|
|
url: latestVideo.shareUrl || undefined,
|
|
color: 0xff0050,
|
|
author: {
|
|
name: officialAuthorName,
|
|
url: latestVideo.shareUrl || "https://www.tiktok.com/",
|
|
icon_url: officialAvatarUrl || undefined
|
|
},
|
|
image: latestVideo.coverImageUrl ? { url: latestVideo.coverImageUrl } : undefined
|
|
};
|
|
|
|
await sendDiscordNotification(message, embed);
|
|
await redis.set(redisKey, latestVideo.id);
|
|
} catch (error) {
|
|
console.error("[TikTok Feed] Error processing:", error);
|
|
}
|
|
}
|
|
|
|
export function startTiktokFeedInterval() {
|
|
if (!(global as any).tiktokFeedIntervalStarted) {
|
|
(global as any).tiktokFeedIntervalStarted = true;
|
|
console.log("[TikTok Feed] Starting 5-minute interval check in background...");
|
|
|
|
// Run immediately on start
|
|
checkTiktokFeed();
|
|
|
|
// Then every 5 minutes
|
|
setInterval(checkTiktokFeed, 5 * 60 * 1000);
|
|
}
|
|
}
|