Newer
Older
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
this.continuation = itemSection.continuation;
}
nextPage() {
this.context.page = this.context.page + 1;
if(this.continuation) {
const continueItems = validateContinuation(()=>
requestSearchContinuation(this.continuation.token));
if(continueItems.length > 0) {
const fakeSectionList = {
contents: continueItems
};
const newItemSection = extractSectionListRenderer_Sections(fakeSectionList, this.context);
if(newItemSection.channels)
return new SearchItemSectionPlaylistPager(newItemSection);
}
}
this.hasMore = false;
this.results = [];
return this;
}
}
//#endregion
//#region Requests
function getAuthContextHeaders(useMobile = false, contentType = null) {
const clientContext = getClientContext(true);
const result = {
"Accept-Language": "en-US",
"x-goog-authuser": "0",
"x-goog-pageid": clientContext.DELEGATED_SESSION_ID,
"x-origin": useMobile ? URL_BASE_M : URL_BASE,
"x-youtube-client-name": useMobile ? "2" : "1",
"User-Agent": useMobile ? USER_AGENT_TABLET : USER_AGENT_WINDOWS
};
if(contentType)
result["Content-Type"] = contentType;
return result;
}
function requestGuide(pageId) {
if(!pageId)
throw new ScriptException("No page id found, invalid authentication?");
const clientContext = getClientContext(true);
const body = {
context: clientContextAuth.INNERTUBE_CONTEXT
};
const url = URL_GUIDE + "?key=" + clientContext.INNERTUBE_API_KEY + "&prettyPrint=false"
const res = http.POST(url, JSON.stringify(body), getAuthContextHeaders(false, "application/json"), true);
if (res.code != 200) {
bridge.log("Failed to retrieve subscriptions page.");
return [];
}
const data = JSON.parse(res.body);
return data;
}
Kelvin
committed
function requestNext(body, useAuth = false, useMobile = false) {
const clientContext = getClientContext(useAuth);
if(!clientContext || !clientContext.INNERTUBE_CONTEXT || !clientContext.INNERTUBE_API_KEY)
throw new ScriptException("Missing client context");
body.context = clientContext.INNERTUBE_CONTEXT;
Kelvin
committed
const baseUrl = (useMobile) ? URL_NEXT_MOBILE : URL_NEXT;
const url = baseUrl + "?key=" + clientContext.INNERTUBE_API_KEY + "&prettyPrint=false";
let headers = (!bridge.isLoggedIn() && useAuth) ? {} : getAuthContextHeaders(useMobile);
headers["Content-Type"] = "application/json";
if(useMobile) {
headers["User-Agent"] = USER_AGENT_TABLET;
}
if(useAuth) {
headers["x-goog-authuser"] = clientContext.SESSION_INDEX ?? "0";
}
const resp = http.POST(url, JSON.stringify(body), headers, useAuth);
if(!resp.isOk) {
log("Fail Url: " + url + "\nFail Body:\n" + JSON.stringify(body));
throw new ScriptException("Failed to next [" + resp.code + "]");
}
return JSON.parse(resp.body);
}
function requestBrowse(body, useMobile = false, useAuth = false, attempt = 0) {
const clientContext = getClientContext(useAuth);
if(!clientContext || !clientContext.INNERTUBE_CONTEXT || !clientContext.INNERTUBE_API_KEY)
throw new ScriptException("Missing client context");
body.context = clientContext.INNERTUBE_CONTEXT;
let headers = !bridge.isLoggedIn() ? {} : getAuthContextHeaders(useMobile);
if(useMobile)
headers["User-Agent"] = USER_AGENT_TABLET;
headers["Content-Type"] = "application/json";
const baseUrl = !useMobile ? URL_BROWSE : URL_BROWSE_MOBILE;
const url = baseUrl + "?key=" + clientContext.INNERTUBE_API_KEY + "&prettyPrint=false";
const resp = http.POST(url, JSON.stringify(body), headers, useAuth);
if(!resp.isOk) {
if((resp.code == 408 || resp.code == 500) && attempt < 1) {
return requestBrowse(body, useMobile, useAuth, attempt + 1);
}
log("Fail Url: " + url + "\nFail Body:\n" + JSON.stringify(body));
if(resp.code != 500 || !bridge.isLoggedIn())
throw new ScriptException("Failed to browse [" + resp.code + "]");
else {
throw new ScriptLoginRequiredException("Failed to browse [" + resp.code + "]\nLogin might have expired, try logging in again");
}
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
}
return JSON.parse(resp.body);
}
function requestSearch(query, useAuth = false, params = null) {
const clientContext = getClientContext(useAuth);
if(!clientContext || !clientContext.INNERTUBE_CONTEXT || !clientContext.INNERTUBE_API_KEY)
throw new ScriptException("Missing client context");
const body = {
context: clientContext.INNERTUBE_CONTEXT,
query: query
};
if(params)
body.params = params;
const resp = http.POST(URL_SEARCH + "?key=" + clientContext.INNERTUBE_API_KEY + "&prettyPrint=false",
JSON.stringify(body), {
"User-Agent": USER_AGENT_WINDOWS,
"Content-Type": "application/json"
}, useAuth);
if(!resp.isOk) throw new ScriptException("Failed to search [" + resp.code + "]");
return JSON.parse(resp.body);
}
function requestSearchContinuation(continuation, useAuth = false) {
const clientContext = getClientContext(useAuth);
if(!clientContext || !clientContext.INNERTUBE_CONTEXT || !clientContext.INNERTUBE_API_KEY)
throw new ScriptException("Missing client context");
const body = {
context: clientContext.INNERTUBE_CONTEXT,
continuation: continuation
};
const resp = http.POST(URL_SEARCH + "?key=" + clientContext.INNERTUBE_API_KEY + "&prettyPrint=false",
JSON.stringify(body), {
"Content-Type": "application/json"
}, useAuth);
if(!resp.isOk) throw new ScriptException("Failed to search [" + resp.code + "]");
return JSON.parse(resp.body);
}
function getRequestHeaders(additionalHeaders) {
const headers = additionalHeaders ?? {};
return Object.assign(headers, {"Accept-Language": "en-US"});
}
const resp = http.GET(url, getRequestHeaders(headers), useAuth);
if(resp.isOk)
return resp.body;
else throw new ScriptException("Failed to request page [" + resp.code + "]");
}
function requestInitialData(url, useMobile = false, useAuth = false) {
let headers = {"Accept-Language": "en-US", "Cookie": "PREF=hl=en&gl=US" };
if(useMobile)
headers["User-Agent"] = USER_AGENT_TABLET;
let html = resp.body;
if(html.indexOf("<form action=\"https://consent.youtube.com/save\"") > 0) {
log("Consent form required");
const consentData = "gl=US&m=0&app=0&pc=yt&continue=" + encodeURIComponent(url) + "&x=6&bl=boq_identityfrontenduiserver_20231017.04_p0&hl=en&src=1&cm=2&set_eom=true";
const respConsent = http.POST("https://consent.youtube.com/save", consentData,
{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
"Accept-Language": "en-US",
"Content-Type": "application/x-www-form-urlencoded"
}, useAuth);
throwIfCaptcha(respConsent);
if(respConsent.isOk) {
const body = respConsent.body;
if(respConsent.body.indexOf("<form action=\"https://consent.youtube.com/save\"") > 0)
throw new CriticalException("Failed to refuse Google consent [" + respConsent.code + "]")
else
html = respConsent.body;
}
else throw new CriticalException("Failed to refuse Google consent [" + resp.code + "]");
}
const initialData = getInitialData(html);
return initialData;
}
else throw new ScriptException("Failed to request page [" + resp.code + "]\n" + url + "\n");
}
function requestClientConfig(useMobile = false, useAuth = false) {
let headers = {
}
if(useMobile)
headers["User-Agent"] = USER_AGENT_TABLET;
const resp = http.GET(!useMobile ? URL_CONTEXT : URL_CONTEXT_M, headers, useAuth);
if(!resp.isOk) throw new ScriptException("Failed to request context requestClientConfig");
const body = {
videoId: videoId,
cpn: "" + randomString(16),
contentCheckOk: "true",
racyCheckOn: "true",
context: {
client: {
"clientName": "IOS",
"clientVersion": IOS_APP_VERSION,//"17.31.4",^M
"deviceModel": IOS_DEVICE_VERSION,//"iPhone14,5",^M
"osVersion": IOS_OS_VERSION_DETAILED,//"15.6.0.19G71",^M
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
"hl": langDisplay,
"gl": langRegion,
},
user: {
"lockedSafetyMode": false
}
}
};
const headers = {
"Content-Type": "application/json",
"User-Agent": USER_AGENT_IOS,
"X-Goog-Api-Format-Version": "2"
};
const token = randomString(12);
const clientContext = getClientContext(false);
const url = URL_PLAYER +
"?key=" + clientContext.INNERTUBE_API_KEY +
"&prettyPrint=false" +
"&t=" + token +
"&id=" + videoId
if(batch) {
batch.POST(url, JSON.stringify(body), headers, false);
}
else {
const resp = http.POST(url, JSON.stringify(body), headers, false);
return resp;
}
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
}
function requestAndroidStreamingData(videoId) {
const body = {
videoId: videoId,
cpn: "" + randomString(16),
contentCheckOk: "true",
racyCheckOn: "true",
context: {
client: {
"clientName": "ANDROID",
"clientVersion": "17.31.35",
"platform": "MOBILE",
"osName": "Android",
"osVersion": "12",
"androidSdkVersion": 31,
"hl": langDisplay,
"gl": langRegion,
"params": "8AEB"
},
user: {
"lockedSafetyMode": false
}
}
};
const headers = {
"Content-Type": "application/json",
"User-Agent": USER_AGENT_ANDROID,
"X-Goog-Api-Format-Version": "2"
};
const token = randomString(12);
const clientContext = getClientContext(false);
const url = URL_PLAYER +
"?key=" + clientContext.INNERTUBE_API_KEY +
"&prettyPrint=false" +
"&t=" + token +
"&id=" + videoId
const resp = http.POST(url, JSON.stringify(body), headers, false);
if(resp.isOk)
return JSON.parse(resp.body);
else
return null;
}
Kelvin
committed
function requestTvHtml5EmbedStreamingData(videoId, sts, withLogin = false) {
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
const body = {
videoId: videoId,
cpn: "" + randomString(16),
contentCheckOk: "true",
racyCheckOn: "true",
playbackContext: {
contentPlaybackContext: {
signatureTimestamp: sts,
referer: "https://www.youtube.com/watch?v=" + videoId
}
},
context: {
client: {
"clientName": "TVHTML5_SIMPLY_EMBEDDED_PLAYER",
"clientVersion": "2.0",
"clientScreen": "EMBED",
"platform": "TV",
"hl": langDisplay,
"gl": langRegion
},
thirdParty: {
"embedUrl": "https://www.youtube.com/watch?v=" + videoId,
},
user: {
"lockedSafetyMode": false
}
}
};
const headers = {
"Content-Type": "application/json",
"User-Agent": USER_AGENT_TVHTML5_EMBED,
"X-Goog-Api-Format-Version": "2"
};
const token = randomString(12);
const clientContext = getClientContext(false);
const url = URL_PLAYER +
"?key=" + clientContext.INNERTUBE_API_KEY +
"&prettyPrint=false" +
"&t=" + token +
"&id=" + videoId
Kelvin
committed
const resp = http.POST(url, JSON.stringify(body), headers, !!withLogin);
if(resp.isOk)
return JSON.parse(resp.body);
else
return null;
}
//#endregion
//#region Page Extraction
function getInitialData(html, useAuth = false) {
const clientContext = getClientContext(useAuth);
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
//TODO: Fix regex instead of this temporary workaround.
/*
const startIndex = html.indexOf("var ytInitialData = ");
const endIndex = html.indexOf(";</script>", startIndex);
if(startIndex > 0 && endIndex > 0) {
const raw = html.substring(startIndex + 20, endIndex);
const initialDataRaw = raw.startsWith("'") && raw.endsWith("'") ?
decodeHexEncodedString(raw.substring(1, raw.length - 1))
//TODO: Find proper decoding strat
.replaceAll("\\\\\"", "\\\"") :
raw;
let initialData = null;
try{
initialData = JSON.parse(initialDataRaw);
}
catch(ex) {
console.log("Failed to parse initial data: ", initialDataRaw);
throw ex;
}
if(clientContext?.INNERTUBE_CONTEXT && !clientContext.INNERTUBE_CONTEXT.client.visitorData &&
initialData.responseContext?.webResponseContextExtensionData?.ytConfigData?.visitorData) {
clientContext.INNERTUBE_CONTEXT.client.visitorData = initialData.responseContext?.webResponseContextExtensionData?.ytConfigData?.visitorData
log("Found new visitor (auth) data: " + clientContext.INNERTUBE_CONTEXT.client.visitorData);
}
return initialData;
}*/
const initialDataRaw = match[1].startsWith("'") && match[1].endsWith("'") ?
decodeHexEncodedString(match[1].substring(1, match[1].length - 1))
//TODO: Find proper decoding strat
.replaceAll("\\\\\"", "\\\"") :
match[1];
try{
initialData = JSON.parse(initialDataRaw);
}
catch(ex) {
console.log("Failed to parse initial data: ", initialDataRaw);
throw ex;
}
if(clientContext?.INNERTUBE_CONTEXT && !clientContext.INNERTUBE_CONTEXT.client.visitorData &&
initialData.responseContext?.webResponseContextExtensionData?.ytConfigData?.visitorData) {
clientContext.INNERTUBE_CONTEXT.client.visitorData = initialData.responseContext?.webResponseContextExtensionData?.ytConfigData?.visitorData
log("Found new visitor (auth) data: " + clientContext.INNERTUBE_CONTEXT.client.visitorData);
}
return initialData;
}
//if(initialData == null)
// log(html);
let match = html.match(REGEX_INITIAL_PLAYER_DATA);
let initialDataRaw = match[1];
try {
return JSON.parse(initialDataRaw);
}
catch(ex) {
//Fallback approach
match = html.match(REGEX_INITIAL_PLAYER_DATA_FALLBACK);
if(match) {
initialDataRaw = match[1];
return JSON.parse(initialDataRaw);
}
}
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
}
return null;
}
function getClientConfig(html) {
const matches = html.matchAll(REGEX_YTCFG);
let match = null;
for(let m of matches) {
if(m && m.length >= 2 && m[1].indexOf("INNERTUBE_CONTEXT") > 0) {
match = m;
}
}
if(!match) throw new ScriptException("Context structure not found");
return JSON.parse(match[1]);
}
//#endregion
//#region Top-Level Extraction
/**
* Extract Subscription channels from a submenu obtained from subscriptionsPage
* @returns {PlatformAuthorLink[]} Channels
*/
function extractChannelListSubMenuAvatarRenderer_AuthorLink(renderer) {
const thumbnail = renderer?.thumbnail?.thumbnails && renderer.thumbnail.thumbnails.length > 0 ?
renderer.thumbnail.thumbnails[renderer.thumbnail.thumbnails.length - 1] :
null;
const name = renderer?.accessibility?.accessibilityData?.label ?
renderer.accessibility.accessibilityData.label.trim() :
"";
const url = renderer?.navigationEndpoint?.browseEndpoint?.canonicalBaseUrl ?
URL_BASE + renderer.navigationEndpoint.browseEndpoint.canonicalBaseUrl :
null;
if(!url || !name)
return null;
else
return new PlatformAuthorLink(new PlatformID(PLATFORM, null, config?.id, PLATFORM_CLAIMTYPE), name, url, thumbnail);
}
/**
* Extract Subscription channels from a submenu obtained from subscriptionsPage
* @returns {String[]} Urls
*/
function extractChannelListSubMenuAvatarRenderer_URL(renderer) {
const canonicalUrl = renderer?.navigationEndpoint?.browseEndpoint?.canonicalBaseUrl ?
URL_BASE + renderer.navigationEndpoint.browseEndpoint.canonicalBaseUrl :
null;
const idUrl = renderer?.navigationEndpoint?.browseEndpoint?.browseId ?
URL_BASE + "/channel/" + renderer.navigationEndpoint.browseEndpoint.browseId :
null;
const url = idUrl ?? canonicalUrl;
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
if(!url)
return null;
else
return url;
}
/**
* Extract Subscription channels from a sections[] obtained from guide()
* @returns {PlatformAuthorLink[]} Channels
*/
function extractGuide_Channels(data) {
let sections = data.items ?? [];
let channels = [];
for(let section of sections) {
switchKey(section, {
guideSubscriptionsSectionRenderer(renderer) {
for(let item of renderer.items) {
switchKey(item, {
guideEntryRenderer(guideEntryRenderer) {
channels.push(extractGuideEntry_AuthorLink(guideEntryRenderer));
},
guideCollapsibleEntryRenderer(collapseRenderer) {
if(collapseRenderer.expandableItems?.length > 0) {
for(let item of collapseRenderer.expandableItems) {
switchKey(item, {
guideEntryRenderer(guideEntryRenderer) {
channels.push(extractGuideEntry_AuthorLink(guideEntryRenderer));
}
})
}
}
}
});
}
}
});
}
return channels;
}
function extractGuideEntry_AuthorLink(guideEntryRenderer) {
const thumbnail = guideEntryRenderer.thumbnail?.thumbnails?.length > 0 ?
guideEntryRenderer.thumbnail.thumbnails[0].url : null;
const name = guideEntryRenderer.formattedTitle?.simpleText ??
guideEntryRenderer.accessibility?.accessibilityData?.label;
const url = guideEntryRenderer.navigationEndpoint?.browseEndpoint?.canonicalBaseUrl ?
URL_BASE + guideEntryRenderer.navigationEndpoint?.browseEndpoint?.canonicalBaseUrl : null;
return new PlatformAuthorLink(new PlatformID(PLATFORM, null, config.id, PLATFORM_CLAIMTYPE), name, url, thumbnail);
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
}
/**
* Extract all video results and shelves from a search page's initial data
* @param data Root-data from search()
* @param contextData Any context values used to fill out data for resulting objects
* @returns Object containing videos and shelves
*/
function extractSearch_SearchResults(data, contextData) {
let searchContents = data.contents?.twoColumnSearchResultsRenderer?.primaryContents?.sectionListRenderer ??
data.contents?.sectionListRenderer;
if(searchContents) {
const results = extractSectionListRenderer_Sections(searchContents, contextData);
return results;
}
return {};
}
/**
* Extracts a PlatformChannel from a channel page's initial data
* @param initialData Initial data from a ChannelPage
* @returns {PlatformChannel}
*/
function extractChannel_PlatformChannel(initialData, sourceUrl = null) {
const errorAlerts = initialData?.alerts?.filter(x=>x.alertRenderer?.type == "ERROR") ?? [];
if(errorAlerts.length > 0){
throw new UnavailableException(extractText_String(errorAlerts[0].alertRenderer.text));
}
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
if(initialData?.header?.c4TabbedHeaderRenderer) {
const headerRenderer = initialData?.header?.c4TabbedHeaderRenderer;
if(IS_TESTING)
console.log("Initial Data", initialData);
const thumbnailTargetWidth = 200;
const thumbnails = headerRenderer.avatar?.thumbnails;
const thumbnail = (thumbnails && thumbnails.length > 0) ? thumbnails.sort((a,b)=>Math.abs(a.width - thumbnailTargetWidth) - Math.abs(b.width - thumbnailTargetWidth))[0] : { url: "" };
const banners = headerRenderer.banner?.thumbnails;
const bannerTargetWidth = 1080;
const banner = (banners && banners.length > 0) ? banners.sort((a,b)=>Math.abs(a.width - bannerTargetWidth) - Math.abs(b.width - bannerTargetWidth))[0] : { url: "" };
const idUrl = headerRenderer?.navigationEndpoint?.browseEndpoint?.browseId ?
URL_BASE + "/channel/" + headerRenderer.navigationEndpoint.browseEndpoint.browseId :
null;
const canonicalUrl = headerRenderer?.navigationEndpoint?.browseEndpoint?.canonicalBaseUrl ?
URL_BASE + headerRenderer.navigationEndpoint.browseEndpoint.canonicalBaseUrl :
null;
return new PlatformChannel({
id: new PlatformID(PLATFORM, headerRenderer.channelId, config.id, PLATFORM_CLAIMTYPE),
name: headerRenderer.title ?? "",
thumbnail: thumbnail.url,
banner: banner.url,
subscribers: Math.max(0, extractHumanNumber_Integer(extractText_String(headerRenderer.subscriberCountText))),
description: "",
url: idUrl,
urlAlternatives: [idUrl, canonicalUrl],
links: {}
});
}
else if(initialData?.header?.pageHeaderRenderer) {
log("New channel model");
const headerRenderer = initialData?.header?.pageHeaderRenderer;
if(IS_TESTING)
console.log("Initial Data (New Model)", initialData);
const thumbnailTargetWidth = 200;
const thumbnails = headerRenderer?.content?.pageHeaderViewModel?.image?.decoratedAvatarViewModel?.avatar?.avatarViewModel?.image?.sources;
const thumbnail = (thumbnails && thumbnails.length > 0) ? thumbnails.sort((a,b)=>Math.abs(a.width - thumbnailTargetWidth) - Math.abs(b.width - thumbnailTargetWidth))[0] : { url: "" };
const banners = headerRenderer?.content?.pageHeaderViewModel?.banner?.imageBannerViewModel?.image?.sources;
const bannerTargetWidth = 1080;
const banner = (banners && banners.length > 0) ? banners.sort((a,b)=>Math.abs(a.width - bannerTargetWidth) - Math.abs(b.width - bannerTargetWidth))[0] : { url: "" };
const id = initialData?.metadata?.channelMetadataRenderer?.externalId;
if(!id) {
log("ID not found in new channel viewmodel:" + JSON.stringify(id, null, " "));
if(bridge.devSubmit) bridge.devSubmit("extractChannel_PlatformChannel - ID Not found in new channel view model", JSON.stringify(initialData));
throw new ScriptException("ID Not found in new channel view model");
}
const idUrl = id ?
URL_BASE + "/channel/" + id:
null;
const canonicalUrl = initialData?.metadata?.channelMetadataRenderer?.vanityChannelUrl ?
initialData?.metadata?.channelMetadataRenderer?.vanityChannelUrl :
null;
let subCount = 0;
const metadataRows = headerRenderer?.content?.pageHeaderViewModel?.metadata?.contentMetadataViewModel?.metadataRows;
for(let row of metadataRows) {
const subsStr = row.metadataParts.find(x=>x.text?.content?.indexOf("subscribers") > 0)?.text?.content;
if(!subsStr)
continue;
const subsNum = extractHumanNumber_Integer(extractText_String(subsStr));
if(!isNaN(subsNum) && subsNum > 0) {
subCount = subsNum;
break;
}
}
return new PlatformChannel({
id: new PlatformID(PLATFORM, id, config.id, PLATFORM_CLAIMTYPE),
name: initialData?.metadata?.channelMetadataRenderer?.title ?? "",
thumbnail: thumbnail.url,
banner: banner.url,
subscribers: Math.max(0, subCount),
description: initialData?.metadata?.channelMetadataRenderer?.description,
url: idUrl,
urlAlternatives: [idUrl, canonicalUrl].filter(x=>x != null),
links: {}
});
}
else {
log("Missing header: (" + sourceUrl + ")\n" + JSON.stringify(initialData, null, " "));
if(bridge.devSubmit) bridge.devSubmit("extractChannel_PlatformChannel - No header for " + sourceUrl, JSON.stringify(initialData));
throw new ScriptException("No header for " + sourceUrl);
}
}
/**
* Extracts multiple tabs from a page that contains a tab rendering
* @param initialData Initial data from a page with a TwoColumnBrowseResultsRenderer
* @param contextData Any context values used to fill out data for resulting objects
* @returns
*/
function extractPage_Tabs(initialData, contextData) {
const content = initialData.contents;
if(!content) {
if(bridge.devSubmit) bridge.devSubmit("extractPage_Tabs - Missing contents", JSON.stringify(initialData));
throw new ScriptException("Missing contents");
}
return switchKey(content, {
twoColumnBrowseResultsRenderer(renderer) {
return extractTwoColumnBrowseResultsRenderer_Tabs(renderer, contextData);
},
singleColumnBrowseResultsRenderer(renderer) {
return extractSingleColumnBrowseResultsRenderer_Tabs(renderer, contextData);
},
default(name) {
if(bridge.devSubmit) bridge.devSubmit("extractPage_Tabs - Unknown renderer type: " + name, JSON.stringify(content));
throw new ScriptException("Unknown renderer type: " + name);
}
});
}
//#endregion
//#region Layout Extractors
function extractVideoPage_VideoDetails(initialData, initialPlayerData, contextData, jsUrl, useLogin, useAbr) {
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
const contents = initialData.contents;
const contentsContainer = contents.twoColumnWatchNextResults?.results?.results ??
null;
if(!contentsContainer || !contentsContainer.contents || !initialPlayerData.videoDetails) return null;
if (IS_TESTING) {
console.log("initialData: ", initialData);
console.log("playerData:", initialPlayerData);
console.log("streamingData:", initialPlayerData?.streamingData);
}
const videoDetails = initialPlayerData.videoDetails;
const nonce = randomString(16);
const hlsSource = (initialPlayerData?.streamingData?.hlsManifestUrl) ?
new HLSSource({
url: initialPlayerData?.streamingData?.hlsManifestUrl
}) : null;
const dashSource = (initialPlayerData?.streamingData?.dashManifestUrl) ?
new DashSource({
url: initialPlayerData?.streamingData?.dashManifestUrl
}) : null;
const abrStreamingUrl = (initialPlayerData.streamingData.serverAbrStreamingUrl) ?
decryptUrlN(initialPlayerData.streamingData.serverAbrStreamingUrl, jsUrl, false) : undefined;
useAbr = abrStreamingUrl && (!!useAbr || USE_ABR_VIDEOS);
const video = {
id: new PlatformID(PLATFORM, videoDetails.videoId, config.id),
name: videoDetails.title,
thumbnails: new Thumbnails(videoDetails.thumbnail?.thumbnails.map(x=>new Thumbnail(escapeUnicode(x.url), x.height)) ?? []),
author: new PlatformAuthorLink(new PlatformID(PLATFORM, videoDetails.channelId, config.id, PLATFORM_CLAIMTYPE), videoDetails.author, URL_BASE + "/channel/" + videoDetails.channelId, null, null),
duration: parseInt(videoDetails.lengthSeconds),
viewCount: parseInt(videoDetails.viewCount),
url: contextData.url,
isLive: videoDetails?.isLive ?? false,
description: videoDetails.shortDescription,
hls: (videoDetails?.isLive ?? false) ? hlsSource : null,
dash: (videoDetails?.isLive ?? false) ? dashSource : null,
live: (videoDetails?.isLive ?? false) ? (hlsSource ?? dashSource) : null,
video:
((!useAbr) ?
extractAdaptiveFormats_VideoDescriptor(initialPlayerData?.streamingData?.adaptiveFormats, jsUrl, contextData, "") :
extractABR_VideoDescriptor(initialPlayerData, jsUrl)
)
?? new VideoSourceDescriptor([]),
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
subtitles: initialPlayerData
.captions
?.playerCaptionsTracklistRenderer
?.captionTracks
?.map(x=>{
let kind = x.baseUrl.match(REGEX_URL_KIND);
if(kind)
kind = kind[1];
if(!kind || kind == "asr") {
return {
name: extractText_String(x.name),
url: x.baseUrl,
format: "text/vtt",
getSubtitles() {
const subResp = http.GET(x.baseUrl, {});
if(!subResp.isOk)
return "";
const asr = subResp.body;
let lines = asr.match(REGEX_ASR);
const newSubs = [];
let skipped = 0;
for(let i = 0; i < lines.length; i++) {
const line = lines[i];
const lineParsed = /<text .*?start="(.*?)" .*?dur="(.*?)".*?>(.*?)<\/text>/gms.exec(line);
const start = parseFloat(lineParsed[1]);
const dur = parseFloat(lineParsed[2]);
let end = start + dur;
const nextLine = (i + 1 < lines.length) ? lines[i + 1] : null;
if(nextLine) {
const lineParsedNext = /<text .*?start="(.*?)" .*?dur="(.*?)".*?>(.*?)<\/text>/gms.exec(nextLine);
const startNext = parseFloat(lineParsedNext[1]);
const durNext = parseFloat(lineParsedNext[2]);
const endNext = startNext + durNext;
if(startNext && startNext < end)
end = startNext;
}
newSubs.push((i - skipped + 1) + "\n" +
toSRTTime(start, true) + " --> " + toSRTTime(end, true) + "\n" +
text + "\n");
}
console.log(newSubs);
return "WEBVTT\n\n" + newSubs.join('\n');
}
};
}
else if(kind == "vtt") {
return {
name: extractText_String(x.name),
url: x.baseUrl,
format: "text/vtt",
};
}
else return null;
})?.filter(x=>x != null) ?? []
};
//Adds HLS stream if any other format is not yet available, mostly relevant for recently ended livestreams.
if(video.video.videoSources !== null && video.video.videoSources.length == 0 && initialPlayerData?.streamingData?.hlsManifestUrl)
video.video.videoSources.push(new HLSSource({url: initialPlayerData.streamingData.hlsManifestUrl}));
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
//Add additional/better details
for(let i = 0; i < contentsContainer.contents.length; i++) {
const content = contentsContainer.contents[i];
switchKey(content, {
videoPrimaryInfoRenderer(renderer) {
//if(renderer.title?.runs)
// video.name = extractString_Runs(renderer.title.runs);
if(renderer.viewCount?.videoViewCountRenderer?.viewCount?.simpleText)
video.viewCount = extractFirstNumber_Integer(renderer.viewCount?.videoViewCountRenderer?.viewCount.simpleText)
else if(renderer.viewCount?.videoViewCountRenderer?.viewCount?.runs) {
video.viewCount = parseInt(extractFirstNumber_Integer(extractRuns_String(renderer.viewCount?.videoViewCountRenderer?.viewCount?.runs)));
}
if(renderer.viewCount?.videoViewCountRenderer?.isLive || renderer.viewCount?.videoViewCountRenderer?.viewCount?.isLive)
video.isLive = true;
else
video.isLive = false;
if(renderer.videoActions?.menuRenderer?.topLevelButtons)
renderer.videoActions.menuRenderer.topLevelButtons.forEach((button)=>{
switchKey(button, {
segmentedLikeDislikeButtonRenderer(renderer) {
const likeButtonRenderer = renderer?.likeButton?.toggleButtonRenderer;
if(likeButtonRenderer) {
const likeTextData = likeButtonRenderer.defaultText;
if(likeTextData){
if(likeTextData.accessibility?.accessibilityData?.label)
video.rating = new RatingLikes(extractFirstNumber_Integer(likeTextData.accessibility.accessibilityData.label));
else if(likeTextData.simpleText)
video.rating = new RatingLikes(extractHumanNumber_Integer(likeTextData.simpleText));
}
}
},
segmentedLikeDislikeButtonViewModel(renderer) {
if(IS_TESTING)
console.log("Found new likes model:", renderer);
let likeButtonViewModel = renderer?.likeButtonViewModel;
if(likeButtonViewModel.likeButtonViewModel) //Youtube double nested, not sure if a bug on their end which may be removed
likeButtonViewModel = likeButtonViewModel.likeButtonViewModel;
let toggleButtonViewModel = likeButtonViewModel?.toggleButtonViewModel;
if(toggleButtonViewModel.toggleButtonViewModel) //Youtube double nested, not sure if a bug on their end which may be removed
toggleButtonViewModel = toggleButtonViewModel.toggleButtonViewModel;
const buttonViewModel = toggleButtonViewModel?.defaultButtonViewModel?.buttonViewModel;
if(buttonViewModel?.title) {
let num = parseInt(buttonViewModel.title);
if(!isNaN(num))
video.rating = new RatingLikes(num);
num = extractHumanNumber_Integer(buttonViewModel.title);
if(!isNaN(num) && num >= 0)
video.rating = new RatingLikes(num);
else if(buttonViewModel.title?.toLowerCase() == "like")
video.rating = new RatingLikes(0);
else {
if(bridge.devSubmit) bridge.devSubmit("extractVideoPage_VideoDetails - Found unknown likes model", JSON.stringify(buttonViewModel));
throw new ScriptException("Found unknown likes model, please report to dev:\n" + JSON.stringify(buttonViewModel.title));
}
else
log("UNKNOWN LIKES MODEL:\n" + JSON.stringify(renderer, null, " "));
}
});
});
if(!video.datetime || video.datetime <= 0) {
let date = 0;
if (date <= 0 && renderer.relativeDateText?.simpleText)
date = extractAgoText_Timestamp(renderer.relativeDateText.simpleText);
if(date <= 0 && renderer.dateText?.simpleText)
date = extractDate_Timestamp(renderer.dateText.simpleText);
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
video.datetime = date;
}
},
videoSecondaryInfoRenderer(renderer) {
if(renderer.owner.videoOwnerRenderer)
video.author = extractVideoOwnerRenderer_AuthorLink(renderer.owner.videoOwnerRenderer);
if(renderer.description?.runs)
video.description = extractRuns_Html(renderer.description.runs);
},
itemSectionRenderer() {
//Comments
}
});
}
const scheduledTime = initialPlayerData?.playabilityStatus?.liveStreamability?.liveStreamabilityRenderer?.offlineSlate?.liveStreamOfflineSlateRenderer?.scheduledStartTime;
if(scheduledTime && !isNaN(scheduledTime))
video.datetime = parseInt(scheduledTime);
const result = new PlatformVideoDetails(video);
if(!useLogin){
result.getComments = function() {
return extractTwoColumnWatchNextResultContents_CommentsPager(contextData.url, contentsContainer?.contents, useLogin)
};
}
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
function extractABR_VideoDescriptor(initialPlayerData, jsUrl) {
const abrStreamingUrl = (initialPlayerData.streamingData.serverAbrStreamingUrl) ?
decryptUrlN(initialPlayerData.streamingData.serverAbrStreamingUrl, jsUrl, false) : undefined;
if(!abrStreamingUrl)
return undefined;
return new UnMuxVideoSourceDescriptor(
(initialPlayerData.streamingData.adaptiveFormats
.filter(x => x.mimeType.startsWith("video/webm"))
.map(y => {
const codecs = y.mimeType.substring(y.mimeType.indexOf('codecs=\"') + 8).slice(0, -1);
const container = y.mimeType.substring(0, y.mimeType.indexOf(';'));
if (codecs.startsWith("av01"))
return null; //AV01 is unsupported.
const duration = parseInt(parseInt(y.approxDurationMs) / 1000) ?? 0;
if (isNaN(duration))
return null;
return new YTABRVideoSource({
name: "UMP " + y.height + "p" + (y.fps ? y.fps : "") + " " + container,
url: abrStreamingUrl,
width: y.width,
height: y.height,
duration: (!isNaN(duration)) ? duration : 0,
container: y.mimeType.substring(0, y.mimeType.indexOf(';')),
codec: codecs,
bitrate: y.bitrate,
}, abrStreamingUrl, y, initialPlayerData.playerConfig.mediaCommonConfig.mediaUstreamerRequestConfig.videoPlaybackUstreamerConfig);
})).filter(x => x != null),
//Audio
(initialPlayerData.streamingData.adaptiveFormats
.filter(x => x.mimeType.startsWith("audio/webm"))
.map(y => {
const codecs = y.mimeType.substring(y.mimeType.indexOf('codecs=\"') + 8).slice(0, -1);
const container = y.mimeType.substring(0, y.mimeType.indexOf(';'));
if (codecs.startsWith("av01"))
return null; //AV01 is unsupported.
const duration = parseInt(parseInt(y.approxDurationMs) / 1000) ?? 0;
if (isNaN(duration))
return null;
return new YTABRAudioSource({
name: "UMP " + (y.audioTrack?.displayName ? y.audioTrack.displayName : codecs),
url: abrStreamingUrl,
width: y.width,
height: y.height,
duration: (!isNaN(duration)) ? duration : 0,
container: y.mimeType.substring(0, y.mimeType.indexOf(';')),
codec: codecs,
bitrate: y.bitrate,
audioChannels: y.audioChannels,
language: ytLangIdToLanguage(y.audioTrack?.id)
}, abrStreamingUrl, y, initialPlayerData.playerConfig.mediaCommonConfig.mediaUstreamerRequestConfig.videoPlaybackUstreamerConfig);
})).filter(x => x != null)
);
}
function extractAdaptiveFormats_VideoDescriptor(adaptiveSources, jsUrl, contextData, prefix) {
const nonce = randomString(16);
return adaptiveSources ? new UnMuxVideoSourceDescriptor(
adaptiveSources.filter(x=>x.mimeType.startsWith("video/") && (x.url || x.cipher || x.signatureCipher)).map(y=>{
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
const codecs = y.mimeType.substring(y.mimeType.indexOf('codecs=\"') + 8).slice(0, -1);
const container = y.mimeType.substring(0, y.mimeType.indexOf(';'));
if(codecs.startsWith("av01"))
return null; //AV01 is unsupported.
const logItag = y.itag == 134;
if(logItag) {
//log(videoDetails.title + " || Format " + container + " - " + y.itag + " - " + y.width);
log("Source Parameters:\n" + JSON.stringify({
url: y.url,
cipher: y.cipher,
signatureCipher: y.signatureCipher
}, null, " "));
}
let url = decryptUrlN(y.url, jsUrl, logItag) ?? decryptUrl(y.cipher, jsUrl, logItag) ?? decryptUrl(y.signatureCipher, jsUrl, logItag);
if(url.indexOf("&cpn=") < 0)
url = url + "&cpn=" + nonce;
const duration = parseInt(parseInt(y.approxDurationMs) / 1000) ?? 0;
if(isNaN(duration))
return null;
if(!y.initRange?.end || !y.indexRange?.end)
return null;
return new YTVideoSource({
name: prefix + y.height + "p" + (y.fps ? y.fps : "") + " " + container,
url: url,
width: y.width,
height: y.height,
duration: (!isNaN(duration)) ? duration : 0,
container: y.mimeType.substring(0, y.mimeType.indexOf(';')),
codec: codecs,
bitrate: y.bitrate,
itagId: y.itag,
initStart: parseInt(y.initRange?.start),
initEnd: parseInt(y.initRange?.end),
indexStart: parseInt(y.indexRange?.start),
indexEnd: parseInt(y.indexRange?.end)
adaptiveSources.filter(x=>x.mimeType.startsWith("audio/") && (x.url || x.cipher || x.signatureCipher)).map(y=>{
const codecs = y.mimeType.substring(y.mimeType.indexOf('codecs=\"') + 8).slice(0, -1);
const container = y.mimeType.substring(0, y.mimeType.indexOf(';'));
let url = decryptUrlN(y.url, jsUrl) ?? decryptUrl(y.cipher, jsUrl) ?? decryptUrl(y.signatureCipher, jsUrl);