Skip to content

Commit 2d9f693

Browse files
committed
fix(bot): make youtube upload notifications work
1 parent 175abe2 commit 2d9f693

3 files changed

Lines changed: 115 additions & 13 deletions

File tree

src/db/youtube.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ export async function youtubeUpdateVideoId(
133133
updateData.latestStreamId = videoId;
134134
updateData.latestStreamIdUpdated = updateTime;
135135
break;
136+
case PlaylistType.All:
137+
console.error(
138+
"All content type should not be used for updating video IDs",
139+
);
140+
141+
return { success: false };
142+
default:
143+
break;
136144
}
137145

138146
// Always update the "all" column regardless of the content type

src/utils/youtube/fetchLatestUploads.ts

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ import {
44
dbYouTubeTable,
55
} from "../../db/schema";
66
import { env } from "../../config";
7-
import { dbYouTubeGetAllChannelsToTrack } from "../../db/youtube";
7+
import {
8+
dbYouTubeGetAllChannelsToTrack,
9+
youtubeUpdateVideoId,
10+
} from "../../db/youtube";
811
import { discordGetAllGuildsTrackingChannel } from "../../db/discord";
912

1013
import getChannelDetails from "./getChannelDetails";
14+
import getSinglePlaylistAndReturnVideoData, {
15+
PlaylistType,
16+
} from "./getSinglePlaylistAndReturnVideoData";
1117

1218
export const updates = new Map<
1319
string,
@@ -30,10 +36,8 @@ export default async function fetchLatestUploads() {
3036
}
3137

3238
channels.data.forEach((channel) => {
33-
if (!channel.youtubeChannelId || !channel.latestAllId) {
34-
console.error(
35-
"Channel ID or latest video ID is missing in fetchLatestUploads",
36-
);
39+
if (!channel.youtubeChannelId) {
40+
console.error("Channel ID is missing in fetchLatestUploads");
3741

3842
return;
3943
}
@@ -69,12 +73,14 @@ export default async function fetchLatestUploads() {
6973

7074
const data = await res.json();
7175

76+
// TODO: Upload time (https://github.com/GalvinPython/feedr/issues/136)
7277
for (const playlist of data.items) {
7378
const channelId = playlist.snippet.channelId;
7479
const videoId =
7580
playlist.snippet.thumbnails.default.url.split("/")[4];
7681

77-
const requiresUpdate = channelDict[channelId] !== videoId;
82+
const requiresUpdate =
83+
channelDict[channelId].latestAllId !== videoId;
7884

7985
console.log(
8086
"Channel ID:",
@@ -86,7 +92,68 @@ export default async function fetchLatestUploads() {
8692
);
8793

8894
if (requiresUpdate) {
89-
if (!(await updateVideoId(channelId, videoId))) {
95+
const [longVideoId, shortVideoId, streamVideoId] =
96+
await Promise.all([
97+
getSinglePlaylistAndReturnVideoData(
98+
channelId,
99+
PlaylistType.Video,
100+
),
101+
getSinglePlaylistAndReturnVideoData(
102+
channelId,
103+
PlaylistType.Short,
104+
),
105+
getSinglePlaylistAndReturnVideoData(
106+
channelId,
107+
PlaylistType.Stream,
108+
),
109+
]);
110+
111+
if (!longVideoId && !shortVideoId && !streamVideoId) {
112+
console.error(
113+
"No video IDs found for channel in fetchLatestUploads",
114+
);
115+
continue;
116+
}
117+
118+
let contentType: PlaylistType | null = null;
119+
120+
const videoIdMap = {
121+
[PlaylistType.Video]: longVideoId,
122+
[PlaylistType.Short]: shortVideoId,
123+
[PlaylistType.Stream]: streamVideoId,
124+
};
125+
126+
contentType = Object.entries(videoIdMap).find(
127+
([, id]) => id,
128+
)?.[0] as PlaylistType | null;
129+
130+
if (contentType) {
131+
console.log(
132+
`Updating ${contentType} video ID for channel`,
133+
channelId,
134+
"to",
135+
videoIdMap[contentType as keyof typeof videoIdMap],
136+
);
137+
} else {
138+
console.error(
139+
"No valid video ID found for channel",
140+
channelId,
141+
"with video ID",
142+
videoId,
143+
);
144+
continue;
145+
}
146+
147+
const updateSuccess = await youtubeUpdateVideoId(
148+
channelId,
149+
videoId,
150+
contentType,
151+
152+
// Temporarily using current date for update time
153+
new Date(),
154+
);
155+
156+
if (!updateSuccess) {
90157
console.error(
91158
"Error updating video ID in fetchLatestUploads",
92159
);
@@ -110,9 +177,29 @@ export default async function fetchLatestUploads() {
110177

111178
const channelInfo = await getChannelDetails(channelId);
112179

180+
console.log(
181+
discordGuildsToUpdate.data.filter(
182+
(
183+
guild,
184+
): guild is typeof dbGuildYouTubeSubscriptionsTable.$inferSelect =>
185+
"youtubeChannelId" in guild &&
186+
"trackVideos" in guild &&
187+
"trackShorts" in guild &&
188+
"trackStreams" in guild,
189+
),
190+
);
191+
113192
updates.set(videoId, {
114193
channelInfo,
115-
discordGuildsToUpdate,
194+
discordGuildsToUpdate: discordGuildsToUpdate.data.filter(
195+
(
196+
guild,
197+
): guild is typeof dbGuildYouTubeSubscriptionsTable.$inferSelect =>
198+
"youtubeChannelId" in guild &&
199+
"trackVideos" in guild &&
200+
"trackShorts" in guild &&
201+
"trackStreams" in guild,
202+
),
116203
});
117204

118205
// console.log("Discord guilds to update:", discordGuildsToUpdate);

src/utils/youtube/sendLatestUploads.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default async function sendLatestUploads() {
1313
for (const guild of discordGuildsToUpdate) {
1414
try {
1515
const channelObj = await client.channels.fetch(
16-
guild.guild_channel_id,
16+
guild.notificationChannelId,
1717
);
1818

1919
if (
@@ -27,12 +27,19 @@ export default async function sendLatestUploads() {
2727
continue;
2828
}
2929

30+
console.log(
31+
"Sending message to channel:",
32+
channelObj.id,
33+
"for video ID:",
34+
videoId,
35+
);
36+
3037
await (channelObj as TextChannel).send({
3138
content:
32-
guild.guild_ping_role && channelInfo
33-
? `<@&${guild.guild_ping_role}> New video uploaded for ${channelInfo?.channelName}! https://www.youtube.com/watch?v=${videoId}`
34-
: guild.guild_ping_role
35-
? `<@&${guild.guild_ping_role}> New video uploaded! https://www.youtube.com/watch?v=${videoId}`
39+
guild.notificationRoleId && channelInfo
40+
? `<@&${guild.notificationRoleId}> New video uploaded for ${channelInfo?.channelName}! https://www.youtube.com/watch?v=${videoId}`
41+
: guild.notificationRoleId
42+
? `<@&${guild.notificationRoleId}> New video uploaded! https://www.youtube.com/watch?v=${videoId}`
3643
: channelInfo
3744
? `New video uploaded for ${channelInfo.channelName}! https://www.youtube.com/watch?v=${videoId}`
3845
: `New video uploaded! https://www.youtube.com/watch?v=${videoId}`,

0 commit comments

Comments
 (0)