1- import { AppContext } from "../../mod.ts" ;
2- import { YouTubeCaptionListResponse } from "../../utils/types.ts" ;
3- import { COMMON_ERROR_MESSAGES } from "../../utils/constant.ts" ;
1+ import { AppContext } from "../../../ mod.ts" ;
2+ import { YouTubeCaptionListResponse } from "../../../ utils/types.ts" ;
3+ import { COMMON_ERROR_MESSAGES } from "../../../ utils/constant.ts" ;
44
55export interface Props {
66 /**
@@ -21,18 +21,6 @@ export interface Props {
2121 */
2222 format ?: "srt" | "sbv" | "vtt" ;
2323
24- /**
25- * @title Translation Language
26- * @description Language code for translation
27- */
28- translationLanguage ?: string ;
29-
30- /**
31- * @title Preferred Language
32- * @description Preferred language code to fetch caption directly
33- */
34- preferredLanguage ?: string ;
35-
3624 /**
3725 * @title Auto Load Caption
3826 * @description If true, automatically loads the first available caption
@@ -69,8 +57,6 @@ const loader = async (
6957 videoId,
7058 captionId,
7159 format = "srt" ,
72- translationLanguage,
73- preferredLanguage,
7460 autoLoadCaption = true ,
7561 } = props ;
7662
@@ -82,27 +68,14 @@ const loader = async (
8268 }
8369
8470 try {
85- const captionsListResponse = await ctx . client [ "GET /captions" ] (
86- {
87- part : "snippet" ,
88- videoId,
89- } ,
90- {
91- headers : ctx . tokens ?. access_token
92- ? { Authorization : `Bearer ${ ctx . tokens . access_token } ` }
93- : { } ,
94- } ,
95- ) ;
96-
97- if ( ! captionsListResponse . ok ) {
98- ctx . errorHandler . toHttpError (
99- captionsListResponse ,
100- `Failed to fetch captions list: ${ captionsListResponse . statusText } ` ,
71+ const captionsListResponse = await ctx . invoke [ "google-youtube" ] . loaders
72+ . videos . captions . list (
73+ {
74+ videoId,
75+ } ,
10176 ) ;
102- }
10377
104- const captionsList = await captionsListResponse
105- . json ( ) as YouTubeCaptionListResponse ;
78+ const captionsList = captionsListResponse ;
10679
10780 const response : CaptionResponse = {
10881 available : captionsList . items ?. length > 0
@@ -111,15 +84,14 @@ const loader = async (
11184 } ;
11285
11386 const targetCaptionId = captionId ||
114- findCaptionToLoad ( response . available , preferredLanguage , autoLoadCaption ) ;
87+ findCaptionToLoad ( response . available , autoLoadCaption ) ;
11588
11689 if ( targetCaptionId ) {
11790 await loadCaption (
11891 ctx ,
11992 response ,
12093 targetCaptionId ,
12194 format ,
122- translationLanguage ,
12395 ) ;
12496 }
12597
@@ -134,18 +106,10 @@ const loader = async (
134106
135107function findCaptionToLoad (
136108 available : YouTubeCaptionListResponse ,
137- preferredLanguage ?: string ,
138109 autoLoadCaption = true ,
139110) : string | undefined {
140111 if ( ! autoLoadCaption || ! available . items ?. length ) return undefined ;
141112
142- if ( preferredLanguage ) {
143- const preferredCaption = available . items . find (
144- ( item ) => item . snippet . language . startsWith ( preferredLanguage ) ,
145- ) ;
146- if ( preferredCaption ) return preferredCaption . id ;
147- }
148-
149113 return available . items [ 0 ] ?. id ;
150114}
151115
@@ -154,31 +118,13 @@ async function loadCaption(
154118 response : CaptionResponse ,
155119 captionId : string ,
156120 format : "srt" | "sbv" | "vtt" ,
157- translationLanguage : string | undefined ,
158121) : Promise < void > {
159122 try {
160- const captionResponse = await ctx . client [ "GET /captions/:id" ] (
161- {
123+ const captionText = await ctx . invoke [ "google-youtube" ] . loaders . videos
124+ . captions . get ( {
162125 id : captionId ,
163- tfmt : format ,
164- tlang : translationLanguage ,
165- } ,
166- {
167- headers : ctx . tokens ?. access_token
168- ? { Authorization : `Bearer ${ ctx . tokens . access_token } ` }
169- : { } ,
170- } ,
171- ) ;
172-
173- if ( ! captionResponse . ok ) {
174- ctx . errorHandler . toHttpError (
175- captionResponse ,
176- `Failed to fetch caption text: ${ captionResponse . statusText } ` ,
177- ) ;
178- return ;
179- }
126+ } ) ;
180127
181- const captionText = await captionResponse . text ( ) ;
182128 response . loadedCaptionId = captionId ;
183129 response . format = format ;
184130
0 commit comments