|
|
|
@@ -1,35 +1,20 @@
|
|
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
import { getLatestTiktokVideo, getTiktokProfile } from "@/lib/followers/tiktok";
|
|
|
|
|
import { getRedisClient } from "@/lib/redis";
|
|
|
|
|
import { sendDiscordNotification } from "@/lib/discord";
|
|
|
|
|
import { getLatestTiktokVideo, getTiktokProfile } from "@/lib/followers/tiktok";
|
|
|
|
|
|
|
|
|
|
export async function GET(req: Request) {
|
|
|
|
|
async function checkTiktokFeed() {
|
|
|
|
|
try {
|
|
|
|
|
const { searchParams } = new URL(req.url);
|
|
|
|
|
|
|
|
|
|
// 1. Verify cron secret
|
|
|
|
|
const cronSecret = process.env.CRON_SECRET;
|
|
|
|
|
if (!cronSecret || searchParams.get("secret") !== cronSecret) {
|
|
|
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Fetch the latest video from TikTok API
|
|
|
|
|
const latestVideo = await getLatestTiktokVideo();
|
|
|
|
|
if (!latestVideo) {
|
|
|
|
|
return NextResponse.json({ message: "No videos found or failed to fetch." }, { status: 200 });
|
|
|
|
|
}
|
|
|
|
|
if (!latestVideo) return;
|
|
|
|
|
|
|
|
|
|
// 3. Check Redis to see if we've already notified about this video
|
|
|
|
|
const redis = await getRedisClient();
|
|
|
|
|
const redisKey = "erika:tiktok_last_video_id";
|
|
|
|
|
const lastVideoId = await redis.get(redisKey);
|
|
|
|
|
|
|
|
|
|
if (lastVideoId === latestVideo.id) {
|
|
|
|
|
// Already notified
|
|
|
|
|
return NextResponse.json({ message: "No new videos.", id: latestVideo.id }, { status: 200 });
|
|
|
|
|
return; // Already notified
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. We have a new video! Prepare the notification
|
|
|
|
|
let officialAuthorName = "TikTok";
|
|
|
|
|
let officialAvatarUrl: string | undefined = undefined;
|
|
|
|
|
try {
|
|
|
|
@@ -37,7 +22,7 @@ export async function GET(req: Request) {
|
|
|
|
|
if (profile.displayName) officialAuthorName = profile.displayName;
|
|
|
|
|
if (profile.avatarUrl) officialAvatarUrl = profile.avatarUrl;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("[TikTok Cron] Failed to fetch profile:", e);
|
|
|
|
|
console.error("[TikTok Feed] Failed to fetch profile:", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const message = `ลงคลิปใหม่แล้วววสสสสส ${latestVideo.shareUrl}`;
|
|
|
|
@@ -53,16 +38,22 @@ export async function GET(req: Request) {
|
|
|
|
|
image: latestVideo.coverImageUrl ? { url: latestVideo.coverImageUrl } : undefined
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 5. Send Discord notification
|
|
|
|
|
await sendDiscordNotification(message, embed);
|
|
|
|
|
|
|
|
|
|
// 6. Update Redis with the new video ID
|
|
|
|
|
// We don't set an expiry because we want to remember it forever (until the next video)
|
|
|
|
|
await redis.set(redisKey, latestVideo.id);
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({ success: true, message: "New video notified!", id: latestVideo.id }, { status: 200 });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("[TikTok Cron] Error processing request:", error);
|
|
|
|
|
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|