Skip to content

Commit 1fd8644

Browse files
committed
fix: ajust te getFotmated loader
1 parent 88ef4d1 commit 1fd8644

4 files changed

Lines changed: 59 additions & 77 deletions

File tree

google-youtube/loaders/videos/captions/get.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ export default async function get(
4040

4141
try {
4242
if (!ctx.tokens?.access_token) {
43-
throw new Error("Access token not available");
43+
throw new Error(
44+
"Authentication required. Please authenticate with YouTube first.",
45+
);
4446
}
4547

4648
const accessToken = ctx.tokens.access_token;
@@ -66,6 +68,12 @@ export default async function get(
6668
});
6769

6870
if (!response.ok) {
71+
if (response.status === 401) {
72+
throw new Error(
73+
"Authentication token expired or invalid. Please refresh your YouTube authentication.",
74+
);
75+
}
76+
6977
if (response.status === 400 && tfmt !== "vtt") {
7078
const fallbackParams = new URLSearchParams();
7179
fallbackParams.append("tfmt", "vtt");
@@ -87,20 +95,49 @@ export default async function get(
8795
});
8896

8997
if (!fallbackResponse.ok) {
98+
let errorDetails = "";
99+
try {
100+
const errorJson = await fallbackResponse.json();
101+
errorDetails = JSON.stringify(errorJson);
102+
} catch {
103+
errorDetails = `Status code: ${fallbackResponse.status}`;
104+
}
105+
90106
throw new Error(
91-
`Failed to fetch caption: ${fallbackResponse.status}`,
107+
`Failed to fetch caption with fallback format: ${errorDetails}`,
92108
);
93109
}
94110

95111
return await fallbackResponse.text();
96112
}
97113

98-
throw new Error(`Failed to fetch caption: ${response.status}`);
114+
let errorDetails = "";
115+
try {
116+
const errorJson = await response.json();
117+
errorDetails = JSON.stringify(errorJson);
118+
} catch {
119+
errorDetails = `Status code: ${response.status}`;
120+
}
121+
122+
throw new Error(`Failed to fetch caption: ${errorDetails}`);
99123
}
100124

101125
return await response.text();
102-
} catch (error) {
103-
throw ctx.errorHandler.toHttpError(
126+
} catch (error: unknown) {
127+
if (
128+
error instanceof Error &&
129+
(error.message.includes("Authentication required") ||
130+
error.message.includes("token expired") ||
131+
error.message.includes("invalid"))
132+
) {
133+
ctx.errorHandler.toHttpError(
134+
error,
135+
error.message,
136+
401,
137+
);
138+
}
139+
140+
ctx.errorHandler.toHttpError(
104141
error,
105142
`Error fetching caption with ID ${id}`,
106143
);

google-youtube/loaders/videos/captions.ts renamed to google-youtube/loaders/videos/captions/getFormated.ts

Lines changed: 13 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

55
export 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

135107
function 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

google-youtube/loaders/videos/captions/list.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export interface Props {
1212
* @name LIST_CAPTIONS
1313
* @title List Video Captions
1414
* @description Lists all available caption tracks for a specific video.
15-
* @internal true
1615
*/
1716
export default async function list(
1817
props: Props,

google-youtube/manifest.gen.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import * as $$$5 from "./loaders/livestreams/list.ts";
2929
import * as $$$6 from "./loaders/livestreams/streams.ts";
3030
import * as $$$7 from "./loaders/oauth/start.ts";
3131
import * as $$$8 from "./loaders/oauth/whoami.ts";
32-
import * as $$$9 from "./loaders/videos/captions.ts";
33-
import * as $$$10 from "./loaders/videos/captions/get.ts";
32+
import * as $$$9 from "./loaders/videos/captions/get.ts";
33+
import * as $$$10 from "./loaders/videos/captions/getFormated.ts";
3434
import * as $$$11 from "./loaders/videos/captions/list.ts";
3535
import * as $$$12 from "./loaders/videos/categories.ts";
3636
import * as $$$13 from "./loaders/videos/channelVideos.ts";
@@ -49,8 +49,8 @@ const manifest = {
4949
"google-youtube/loaders/livestreams/streams.ts": $$$6,
5050
"google-youtube/loaders/oauth/start.ts": $$$7,
5151
"google-youtube/loaders/oauth/whoami.ts": $$$8,
52-
"google-youtube/loaders/videos/captions.ts": $$$9,
53-
"google-youtube/loaders/videos/captions/get.ts": $$$10,
52+
"google-youtube/loaders/videos/captions/get.ts": $$$9,
53+
"google-youtube/loaders/videos/captions/getFormated.ts": $$$10,
5454
"google-youtube/loaders/videos/captions/list.ts": $$$11,
5555
"google-youtube/loaders/videos/categories.ts": $$$12,
5656
"google-youtube/loaders/videos/channelVideos.ts": $$$13,

0 commit comments

Comments
 (0)