feat: add tiktok polling system with docker cron
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
export async function sendDiscordNotification(content: string, embed?: any) {
|
||||
const token = process.env.DISCORD_BOT_TOKEN;
|
||||
const channelId = process.env.DISCORD_CHANNEL_ID;
|
||||
|
||||
if (!token) {
|
||||
console.error("[Discord Notification] Missing DISCORD_BOT_TOKEN");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!channelId) {
|
||||
console.error("[Discord Notification] Missing DISCORD_CHANNEL_ID environment variable. Please add it to your .env file.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`https://discord.com/api/v10/channels/${channelId}/messages`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": `Bot ${token}`,
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({
|
||||
content,
|
||||
embeds: embed ? [embed] : undefined
|
||||
})
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
console.error("[Discord Notification] Failed to send message:", await res.text());
|
||||
} else {
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("[Discord Notification] Error sending message:", error);
|
||||
}
|
||||
}
|
||||
@@ -100,3 +100,47 @@ export async function getTiktokFollowerCount(): Promise<number | null> {
|
||||
const profile = await getTiktokProfile();
|
||||
return profile.followerCount;
|
||||
}
|
||||
|
||||
export interface TiktokVideo {
|
||||
id: string;
|
||||
title: string;
|
||||
coverImageUrl: string;
|
||||
shareUrl: string;
|
||||
createTime: number;
|
||||
}
|
||||
|
||||
export async function getLatestTiktokVideo(): Promise<TiktokVideo | null> {
|
||||
const profile = await getTiktokProfile();
|
||||
if (!profile.username) {
|
||||
console.error("[TikTok API] Could not get username from profile to fetch TikWM.");
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`https://www.tikwm.com/api/user/posts?unique_id=@${profile.username}&count=1`, {
|
||||
cache: "no-store",
|
||||
signal: AbortSignal.timeout(5000),
|
||||
});
|
||||
|
||||
if (!res.ok) return null;
|
||||
|
||||
const data = await res.json();
|
||||
const videos = data.data?.videos;
|
||||
|
||||
if (!videos || videos.length === 0) return null;
|
||||
|
||||
const v = videos[0];
|
||||
const shareUrl = `https://www.tiktok.com/@${profile.username}/video/${v.video_id}`;
|
||||
|
||||
return {
|
||||
id: v.video_id,
|
||||
title: v.title ?? "",
|
||||
coverImageUrl: v.cover ?? "",
|
||||
shareUrl: shareUrl,
|
||||
createTime: v.create_time ?? 0,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("[TikTok API] Error fetching latest video via TikWM:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user