-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetchLatestUploads.ts
More file actions
281 lines (239 loc) · 9.92 KB
/
fetchLatestUploads.ts
File metadata and controls
281 lines (239 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { Platform } from "../../types/types.d";
import {
dbGuildYouTubeSubscriptionsTable,
dbYouTubeTable,
} from "../../db/schema";
import { env } from "../../config";
import {
dbYouTubeGetAllChannelsToTrack,
youtubeUpdateVideoId,
} from "../../db/youtube";
import { discordGetAllGuildsTrackingChannel } from "../../db/discord";
import getChannelDetails from "./getChannelDetails";
import getSinglePlaylistAndReturnVideoData, {
PlaylistType,
} from "./getSinglePlaylistAndReturnVideoData";
/**
* Parse a full ISO 8601 duration string into total seconds.
* Supports formats like "PT1H2M3S", "P1DT2H3M4S", "PT30S", etc.
* Note: YouTube typically returns PT-prefixed durations, but very long
* streams/videos could include a day component (P1DT...).
*/
function parseISO8601Duration(duration: string): number {
const match = duration.match(
/^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/,
);
if (!match) return 0;
const days = parseInt(match[1] || "0", 10);
const hours = parseInt(match[2] || "0", 10);
const minutes = parseInt(match[3] || "0", 10);
const seconds = parseInt(match[4] || "0", 10);
return days * 86400 + hours * 3600 + minutes * 60 + seconds;
}
/**
* Fetch the duration (in seconds) of a video using the YouTube Videos API.
* Returns 0 if the duration cannot be determined.
*/
async function fetchVideoDuration(videoId: string): Promise<number> {
const res = await fetch(
`https://youtube.googleapis.com/youtube/v3/videos?part=contentDetails&id=${videoId}&key=${env.youtubeApiKey}`,
);
if (!res.ok) {
console.error(
"Error fetching video duration:",
res.statusText,
);
return 0;
}
// TODO: Type the response from YouTube API for better type safety
const data = await res.json();
if (!data.items || data.items.length === 0) return 0;
const durationFirstItem = data.items[0];
if (!durationFirstItem.contentDetails || !durationFirstItem.contentDetails.duration) {
console.error("Duration not found in video details for video ID:", videoId,);
return 0;
}
const duration = durationFirstItem?.contentDetails?.duration;
if (typeof duration !== "string") return 0;
return parseISO8601Duration(duration);
}
export const updates = new Map<
string,
{
channelInfo: Awaited<ReturnType<typeof getChannelDetails>>;
discordGuildsToUpdate: (typeof dbGuildYouTubeSubscriptionsTable.$inferSelect)[];
}
>();
export default async function fetchLatestUploads() {
console.log("Fetching latest uploads...");
const channels = await dbYouTubeGetAllChannelsToTrack();
const channelDict: Record<string, typeof dbYouTubeTable.$inferSelect> = {};
if (!channels || !channels.success || channels.data.length === 0) {
console.log("No channels to track.");
return;
}
channels.data.forEach((channel) => {
if (!channel.youtubeChannelId) {
console.error("Channel ID is missing in fetchLatestUploads");
return;
}
channelDict[channel.youtubeChannelId] = channel;
});
const chunkSize = 50;
const channelIds = Object.keys(channelDict).map((channelId) =>
channelId.replace(/^UC/, "UU"),
);
const chunks: string[][] = [];
for (let i = 0; i < channelIds.length; i += chunkSize) {
const chunk = channelIds.slice(i, i + chunkSize);
chunks.push(chunk);
}
for (const chunk of chunks) {
const chunkJoined = chunk.join(",");
const res = await fetch(
`https://youtube.googleapis.com/youtube/v3/playlists?part=snippet&id=${chunkJoined}&key=${env.youtubeApiKey}`,
);
if (!res.ok) {
console.error(
"Error fetching latest uploads in fetchLatestUploads:",
res.statusText,
);
return;
}
const data = await res.json();
// TODO: Upload time (https://github.com/GalvinPython/feedr/issues/136)
for (const playlist of data.items) {
const channelId = playlist.snippet.channelId;
const videoId =
playlist.snippet.thumbnails.default.url.split("/")[4];
if (!channelDict[channelId]) {
console.error(
"Channel ID not found in channelDict:",
channelId,
);
continue;
}
const requiresUpdate =
channelDict[channelId].latestAllId !== videoId;
if (requiresUpdate) {
console.log(
"Channel ID:",
channelId,
"Video ID:",
videoId,
"Requires update?",
requiresUpdate,
);
// Use duration-based detection to reduce API quota usage
// and avoid UULF which is currently lagging.
// YouTube Shorts are currently limited to 3 minutes (180s).
// Videos at exactly 180s could still be shorts, so we use
// a strict greater-than check.
const durationSeconds = await fetchVideoDuration(videoId);
const SHORTS_DURATION = 180;
let contentType: PlaylistType | null = null;
if (durationSeconds > SHORTS_DURATION) {
// Over the shorts limit: cannot be a short, check only if it's a stream
const streamVideoId = await getSinglePlaylistAndReturnVideoData(
channelId,
PlaylistType.Stream,
);
if (videoId === streamVideoId.videoId) {
contentType = PlaylistType.Stream;
} else {
// Not a stream and over 3 min; must be a regular video
contentType = PlaylistType.Video;
}
} else {
// Under 3 minutes: could be a short or a video, check UUSH and UULV
const [shortVideoId, streamVideoId] = await Promise.all([
getSinglePlaylistAndReturnVideoData(
channelId,
PlaylistType.Short,
),
getSinglePlaylistAndReturnVideoData(
channelId,
PlaylistType.Stream,
),
]);
if (videoId === shortVideoId.videoId) {
contentType = PlaylistType.Short;
} else if (videoId === streamVideoId.videoId) {
contentType = PlaylistType.Stream;
} else {
// Not in shorts or streams playlist → regular video
contentType = PlaylistType.Video;
}
}
console.log("Determined content type:", contentType, `(duration: ${durationSeconds}s)`);
if (contentType) {
console.log(
`Updating ${contentType} video ID for channel`,
channelId,
"to",
videoId,
);
} else {
console.error(
"No valid video ID found for channel",
channelId,
"with video ID",
videoId,
);
continue;
}
const updateSuccess = await youtubeUpdateVideoId(
channelId,
videoId,
contentType,
// Temporarily using current date for update time
new Date(),
);
if (!updateSuccess.success) {
console.error(
"Error updating video ID in fetchLatestUploads",
);
return;
}
const discordGuildsToUpdate =
await discordGetAllGuildsTrackingChannel(
Platform.YouTube,
channelId,
);
if (!discordGuildsToUpdate) {
console.error(
"Error getting discord guilds to update in fetchLatestUploads",
);
return;
}
const channelInfo = await getChannelDetails(channelId);
console.info(`Filtered guilds for channel ID ${channelId}:`, {
count: discordGuildsToUpdate.data.filter(
(
guild,
): guild is typeof dbGuildYouTubeSubscriptionsTable.$inferSelect =>
"youtubeChannelId" in guild &&
"trackVideos" in guild &&
"trackShorts" in guild &&
"trackStreams" in guild,
).length,
});
updates.set(videoId, {
channelInfo,
discordGuildsToUpdate: discordGuildsToUpdate.data.filter(
(
guild,
): guild is typeof dbGuildYouTubeSubscriptionsTable.$inferSelect =>
"youtubeChannelId" in guild &&
((contentType === PlaylistType.Video &&
guild.trackVideos) ||
(contentType === PlaylistType.Short &&
guild.trackShorts) ||
(contentType === PlaylistType.Stream &&
guild.trackStreams)),
),
});
}
}
}
}