Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/sync-missing-youtube-transcripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ on:
options:
- production
- dev
batch_size:
description: "Batch size (number of videos to process in this run)"
required: true
default: "50"
type: string

jobs:
sync-transcripts:
Expand Down Expand Up @@ -39,3 +44,4 @@ jobs:
SANITY_STUDIO_PROJECT_ID: ${{ vars.SANITY_STUDIO_PROJECT_ID }}
SANITY_STUDIO_DATASET: ${{ inputs.dataset }}
YOUTUBE_API_KEY: ${{ secrets.YOUTUBE_API_KEY }}
SYNC_BATCH_SIZE: ${{ inputs.batch_size }}
31 changes: 21 additions & 10 deletions apps/sanity/functions/sync-missing-youtube-transcripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,27 @@ export const handler = scheduledEventHandler(async ({ context }) => {
useCdn: false,
});

const batchSize = parseInt(process.env.SYNC_BATCH_SIZE || "50", 10);

console.log(
`[Sync Missing YouTube Transcripts] Querying documents with YouTube URLs missing transcripts in project ${projectId}, dataset ${dataset}...`,
);

const totalMissing = await client.fetch<number>(
`count(*[_type in ["post", "podcast", "course", "lesson", "short"] && (defined(youtube) || defined(listenLinks.youtube)) && !defined(transcript) && !(_id in path("drafts.**"))])`,
);

console.log(
`[Sync Missing YouTube Transcripts] Total documents currently missing transcripts in ${dataset}: ${totalMissing}`,
);

if (totalMissing === 0) {
console.log(
"[Sync Missing YouTube Transcripts] All videos currently have linked transcripts. Done.",
);
return;
}

const missingDocs = await client.fetch<
Array<{
_id: string;
Expand All @@ -271,25 +288,19 @@ export const handler = scheduledEventHandler(async ({ context }) => {
youtube: string;
}>
>(
`*[_type in ["post", "podcast"] && defined(youtube) && !defined(transcript) && !(_id in path("drafts.**"))][0...25]{
`*[_type in ["post", "podcast", "course", "lesson", "short"] && (defined(youtube) || defined(listenLinks.youtube)) && !defined(transcript) && !(_id in path("drafts.**"))][0...$batchSize]{
_id,
_type,
title,
youtube
"youtube": coalesce(youtube, listenLinks.youtube)
}`,
{ batchSize },
);

console.log(
`[Sync Missing YouTube Transcripts] Found ${missingDocs.length} documents missing transcripts.`,
`[Sync Missing YouTube Transcripts] Processing batch of ${missingDocs.length} documents (batch limit: ${batchSize})...`,
);

if (missingDocs.length === 0) {
console.log(
"[Sync Missing YouTube Transcripts] All videos currently have linked transcripts. Done.",
);
return;
}

let successCount = 0;
let failCount = 0;

Expand Down
Loading