@@ -259,10 +259,27 @@ export const handler = scheduledEventHandler(async ({ context }) => {
259259 useCdn : false ,
260260 } ) ;
261261
262+ const batchSize = parseInt ( process . env . SYNC_BATCH_SIZE || "50" , 10 ) ;
263+
262264 console . log (
263265 `[Sync Missing YouTube Transcripts] Querying documents with YouTube URLs missing transcripts in project ${ projectId } , dataset ${ dataset } ...` ,
264266 ) ;
265267
268+ const totalMissing = await client . fetch < number > (
269+ `count(*[_type in ["post", "podcast", "course", "lesson", "short"] && (defined(youtube) || defined(listenLinks.youtube)) && !defined(transcript) && !(_id in path("drafts.**"))])` ,
270+ ) ;
271+
272+ console . log (
273+ `[Sync Missing YouTube Transcripts] Total documents currently missing transcripts in ${ dataset } : ${ totalMissing } ` ,
274+ ) ;
275+
276+ if ( totalMissing === 0 ) {
277+ console . log (
278+ "[Sync Missing YouTube Transcripts] All videos currently have linked transcripts. Done." ,
279+ ) ;
280+ return ;
281+ }
282+
266283 const missingDocs = await client . fetch <
267284 Array < {
268285 _id : string ;
@@ -271,25 +288,19 @@ export const handler = scheduledEventHandler(async ({ context }) => {
271288 youtube : string ;
272289 } >
273290 > (
274- `*[_type in ["post", "podcast"] && defined(youtube) && !defined(transcript) && !(_id in path("drafts.**"))][0...25 ]{
291+ `*[_type in ["post", "podcast", "course", "lesson", "short" ] && ( defined(youtube) || defined(listenLinks.youtube)) && !defined(transcript) && !(_id in path("drafts.**"))][0...$batchSize ]{
275292 _id,
276293 _type,
277294 title,
278- youtube
295+ " youtube": coalesce(youtube, listenLinks.youtube)
279296 }` ,
297+ { batchSize } ,
280298 ) ;
281299
282300 console . log (
283- `[Sync Missing YouTube Transcripts] Found ${ missingDocs . length } documents missing transcripts .` ,
301+ `[Sync Missing YouTube Transcripts] Processing batch of ${ missingDocs . length } documents (batch limit: ${ batchSize } ).. .` ,
284302 ) ;
285303
286- if ( missingDocs . length === 0 ) {
287- console . log (
288- "[Sync Missing YouTube Transcripts] All videos currently have linked transcripts. Done." ,
289- ) ;
290- return ;
291- }
292-
293304 let successCount = 0 ;
294305 let failCount = 0 ;
295306
0 commit comments