Skip to content

Commit bfb787a

Browse files
committed
fix yotube
1 parent 8249d7f commit bfb787a

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/subtitle/baseVideo.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export default class BaseVideo {
223223
sourceLang != targetLang &&
224224
this.setting["detectSubtitle"] == "dualsub"
225225
) {
226+
await this.waitRandom(300, 2000); //wait for avoid ban
226227
var sub2 = await this.requestSubtitleCached(
227228
request.url,
228229
targetLang
@@ -269,6 +270,10 @@ export default class BaseVideo {
269270
static async wait(time) {
270271
await new Promise((resolve) => setTimeout(resolve, time));
271272
}
273+
static async waitRandom(min, max) {
274+
const time = Math.random() * (max - min) + min;
275+
await this.wait(time);
276+
}
272277

273278

274279
static async waitUntil(fn,time) {

src/subtitle/youtube.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default class Youtube extends BaseVideo {
6060
await this.interceptCaption(); // start caption intercept
6161
this.loadCaption(); // turn on caption for embed video
6262
this.setPlayerCaption(lang, tlang); //turn on caption on specified lang
63+
await this.waitRandom(4000, 5000);
6364
this.reloadCaption(); //reset previous caption immediately
6465
}
6566

@@ -109,26 +110,34 @@ export default class Youtube extends BaseVideo {
109110
if (lang) {
110111
subUrl = await this.getTranslatedSubtitleUrl(subUrl, lang);
111112
}
113+
var json;
112114
try {
113115
var res = await fetch(this.getTrafficSafeUrl(subUrl));
116+
json = await res.json();
114117
} catch (error) {
115118
console.log(error);
116119
}
117120

118-
// if fail, change base url and try again
119-
if (res?.status != 200) {
120-
this.isSubtitleRequestFailed = !this.isSubtitleRequestFailed;
121-
res = await fetch(this.getTrafficSafeUrl(subUrl));
121+
try {
122+
// if fail, change base url and try again
123+
if (!json) {
124+
this.isSubtitleRequestFailed = !this.isSubtitleRequestFailed;
125+
const res = await fetch(this.getTrafficSafeUrl(subUrl));
126+
json = await res.json();
127+
}
128+
} catch (error) {
129+
console.error("Failed to fetch subtitle:", error);
122130
}
123-
return await res.json();
131+
return json;
124132
}
125133
static getTrafficSafeUrl(url) {
126-
return this.isSubtitleRequestFailed
127-
? url.replace(
128-
`${this.baseUrl}/api/timedtext`,
129-
"video.google.com/timedtext"
130-
)
131-
: url;
134+
if (this.isSubtitleRequestFailed) {
135+
const urlObj = new URL(url);
136+
urlObj.hostname = "video.google.com";
137+
urlObj.pathname = "/timedtext";
138+
return urlObj.toString();
139+
}
140+
return url;
132141
}
133142

134143
static async getTranslatedSubtitleUrl(subUrl, lang) {
@@ -156,6 +165,9 @@ export default class Youtube extends BaseVideo {
156165

157166
// concat sub=====================================
158167
static parseSubtitle(subtitle, lang) {
168+
if (!subtitle?.events) {
169+
return {events: [], pens: [{}], wireMagic: "pb3", wpWinPositions: [{}], wsWinStyles: [{}]};
170+
}
159171
var newEvents = [];
160172
for (var event of subtitle.events) {
161173
if (!event.segs || !event.dDurationMs) {

0 commit comments

Comments
 (0)