Newer
Older
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
const TYPE_MOVIES = 4;
const PREFIX_TYPE = 16;
const PREFIX_LENGTH = 18;
const PREFIX_ORDER = 8;
const PREFIX_DATE = 8;
const PREFIX_DURATION = 24;
function sortToByte(sort) {
switch(sort) {
case Type.Order.Chronological:
return SORT_DATE;
case SORT_RATING_STRING:
return SORT_RATING;
case SORT_VIEWS_STRING:
return SORT_VIEWS;
default:
throw new ScriptException("Unknown sort");
}
}
function searchQueryToSP(sort, type, filters) {
if(!type)
type = TYPE_VIDEO;
let filter_date = (filters?.date && filters.date.length > 0) ? filters.date[0] : null;
let filter_duration = (filters?.duration && filters.duration.length > 0) ? filters.duration[0] : null;
let filter_features = filters?.features ?? [];
const sortByte = sort ? sortToByte(sort) : null;//SORT_RELEVANCE;
let arrLength = 0;
let filterLength = 0;
if(sortByte)
arrLength += 2;
if(type) {
filterLength += 2;
arrLength += 2;
}
if(filter_date) {
filterLength += 2;
arrLength += 2;
}
if(filter_duration) {
filterLength += 2;
arrLength += 2;
}
if(filter_features.length > 0) {
for(let i = 0; i < filter_features.length; i++) {
arrLength += 2;
filterLength += 2;
if(filter_features[i] > 128) {
arrLength += 1;
filterLength += 1;
}
}
}
if(filterLength > 0)
arrLength += 2;
const array = new Uint8Array(arrLength);
let index = 0;
if(sortByte) {
array[index] = PREFIX_ORDER;
array[index + 1] = sortByte;
index += 2;
}
if(filterLength > 0) {
array[index] = PREFIX_LENGTH;
array[index + 1] = filterLength;
index += 2;
}
if(filter_date) {
array[index] = PREFIX_DATE;
array[index + 1] = filter_date;
index += 2;
}
if(filter_duration) {
array[index] = PREFIX_DURATION;
array[index + 1] = filter_duration;
index += 2;
}
if(type) {
array[index] = PREFIX_TYPE;
array[index + 1] = type;
index += 2;
}
for(let i = 0; i < filter_features.length; i++) {
array[index] = filter_features[i];
array[index + 1] = 1;
index += 2;
if(filter_features[i] > 128) {
array[index] = 1;
index += 1;
}
}
return utility.toBase64(array);
}
//#endregion
//#region Utility
const htmlEncodedCharacters = {
"amp": "&",
"lt": "<",
"gt": ">",
"quot": "\"",
"apos": "'"
}
function decodeHtml(text) {
return text.replace(/(?:&|&)#([0-9]*);/gm, function(match, dec) {
return String.fromCharCode(dec);
}).replace(/&([a-z]*);(#.*?;)?/gm, function(match, c){
if(htmlEncodedCharacters[c])
return htmlEncodedCharacters[c];
return c;
});
}
function monthNameToNumber(month) {
if(!month)
return -1;
month = month.toLowerCase();
//Either partial or full month name
if(month.startsWith("jan")) return 1;
if(month.startsWith("feb")) return 2;
if(month.startsWith("mar")) return 3;
if(month.startsWith("apr")) return 4;
if(month.startsWith("may")) return 5;
if(month.startsWith("jun")) return 6;
if(month.startsWith("jul")) return 7;
if(month.startsWith("aug")) return 8;
if(month.startsWith("sep")) return 9;
if(month.startsWith("oct")) return 10;
if(month.startsWith("nov")) return 11;
if(month.startsWith("dec")) return 12;
return -1;
}
const ytLangMap = {
"ar": Language.ARABIC,
"es": Language.SPANISH,
"fr": Language.FRENCH,
"hi": Language.HINDI,
"id": Language.INDONESIAN,
"ko": Language.KOREAN,
"pt-BR": Language.PORTBRAZIL,
"ru": Language.RUSSIAN,
"th": Language.THAI,
"tr": Language.TURKISH,
"vi": Language.VIETNAMESE,
"en": Language.ENGLISH,
"en-US": Language.ENGLISH
};
function ytLangIdToLanguage(id) {
if(!id)
return Language.UNKNOWN;
const langParts = id?.split(".");
let langPart = (langParts && langParts.length > 0) ? langParts[0] : "";
return ytLangMap[langPart]; //Backwards compat
if(langPart.indexOf("-") > 0)
langPart = langPart.split("-")[0].trim();
if(ytLangMap[langPart])
return ytLangMap[langPart]; //Backwards compat
if(langPart && langPart.length > 0)
return langPart.trim();
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
return Language.UNKNOWN;
}
function findRenderer(obj, rendererName) {
if(!obj)
return null;
const keys = Object.keys(obj);
if(!keys || keys.length == 0)
return null;
const objName = keys[0];
const renderer = obj[objName];
if(objName == rendererName)
return renderer;
if(renderer.contents) {
for(let content of renderer.contents) {
const result = findRenderer(content, rendererName);
if(result)
return result;
}
}
if(renderer.content)
return findRenderer(renderer.content, rendererName);
return null;
}
function switchKey(obj, handlers) {
const objName = Object.keys(obj)[0];
if(!objName) {
if(handlers["null"])
return handlers["null"];
return null;
}
if(handlers[objName])
return handlers[objName](obj[objName]);
if(handlers["default"])
return handlers["default"](objName);
return null;
}
//#endregion
function validateContinuation(reqcb, useAuth = false) {
const clientContext = getClientContext(useAuth);
const result = reqcb();
const append = result?.onResponseReceivedCommands ?? result?.onResponseReceivedActions;
if(append && append.length > 0 && append[0].appendContinuationItemsAction) {
const appendResults = append[0].appendContinuationItemsAction.continuationItems;
if(!appendResults) {
if(IS_TESTING)
console.log("Continuation found without items?", result);
return [];
}
else
return appendResults;
}
else if(!clientContext.INNERTUBE_CONTEXT.client.visitorData && result.responseContext?.visitorData) {
log("[validateContinuation] No visitor data set, found visitor data in response, retrying");
clientContext.INNERTUBE_CONTEXT.client.visitorData = result.responseContext.visitorData;
//Retry with visitorData
const reResult = reqcb();
log("[validateContinuation] retry result");
if(append && append.length > 0 && append[0].appendContinuationItemsAction) {
const appendResults = append[0].appendContinuationItemsAction.continuationItems;
if(!appendResults) {
if(IS_TESTING)
console.log("Continuation found without items?", result);
return [];
}
else
return appendResults;
}
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
else
return [];
}
else
return [];
}
//#region Cipher/Decryption
var _cipherDecode = {
};
var _nDecrypt = {
};
var _sts = {
};
const REGEX_CIPHERS = [
new RegExp("(?:\\b|[^a-zA-Z0-9$])([a-zA-Z0-9$]{2,})\\s*=\\s*function\\(\\s*a\\s*\\)\\s*\\{\\s*a\\s*=\\s*a\\.split\\(\\s*\"\"\\s*\\)"),
new RegExp("\\bm=([a-zA-Z0-9$]{2,})\\(decodeURIComponent\\(h\\.s\\)\\)"),
new RegExp("\\bc&&\\(c=([a-zA-Z0-9$]{2,})\\(decodeURIComponent\\(c\\)\\)"),
new RegExp("([\\w$]+)\\s*=\\s*function\\((\\w+)\\)\\{\\s*\\2=\\s*\\2\\.split\\(\"\"\\)\\s*;"),
new RegExp("\\b([\\w$]{2,})\\s*=\\s*function\\((\\w+)\\)\\{\\s*\\2=\\s*\\2\\.split\\(\"\"\\)\\s*;"),
new RegExp("\\bc\\s*&&\\s*d\\.set\\([^,]+\\s*,\\s*(:encodeURIComponent\\s*\\()([a-zA-Z0-9$]+)\\(")
];
const REGEX_DECRYPT_N = /\.get\(\"n\"\)\)&&\([a-zA-Z0-9$_]=([a-zA-Z0-9$_]+)(?:\[(\d+)])?\([a-zA-Z0-9$_]\)/;
const REGEX_DECRYPT_N2 = /[a-zA-Z0-9$_]+=String\.fromCharCode\(110\),[a-zA-Z0-9$_]+=[a-zA-Z0-9$_]+\.get\([a-zA-Z0-9$_]+\)\)&&\([a-zA-Z0-9$_]=([a-zA-Z0-9$_]+)(?:\[(\d+)])?\([a-zA-Z0-9$_]\)/;
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
const REGEX_PARAM_N = new RegExp("[?&]n=([^&]*)");
const STS_REGEX = new RegExp("signatureTimestamp[=:](\\d+)");
source.decryptUrlTest = function(encrypted) {
prepareCipher();
let url = decryptUrlN(encrypted.url, true);
if(!url)
url = decryptUrl(encrypted.cipher, true);
if(!url)
url = decryptUrl(encrypted.signatureCipher, true);
return url;
}
source.decryptUrlTestN = function(n) {
prepareCipher();
let url = "https://whatever.com/asdgdsag?a=b&n=" + n + "&u=asd"
return decryptUrlN(url, true);
}
function decryptUrl(encrypted, jsUrl, doLogging) {
if(!encrypted) return null;
const query = parseQueryString(encrypted);
const baseUrl = query.url;
const sigKey = query.sp;
const sigValue = decodeCipher(decodeURIComponent(query.s), jsUrl);
let decryptedUrl = decodeURIComponent(baseUrl) + "&" + sigKey + "=" + sigValue;
if(doLogging) {
log("SigKey: " + sigKey);
log("SigValue: " + sigValue);
log("Decrypted: " + decryptedUrl);
}
return decryptUrlN(decryptedUrl, jsUrl, doLogging);
}
function decryptUrlN(url, jsUrl, doLogging) {
const nParamMatch = REGEX_PARAM_N.exec(url);
if(nParamMatch) {
const encryptedN = nParamMatch[1];
const decryptedN = decryptN(encryptedN, jsUrl);
if(doLogging) {
log("Encrypt URL:" + url);
log("NParam Found: " + encryptedN + " (length:" + encryptedN.length + ")");
log("NParam Decrypted: " + decryptedN + " (size:" + decryptedN.length + ")");
log("Decrypted URL:" + url.replace(encryptedN, decryptedN));
}
url = url.replace(encryptedN, decryptedN);
}
else if(doLogging)
log("No NParam found in (" + url + ")");
return url;
}
function decodeCipher(cipher, jsUrl) {
if(!_cipherDecode[jsUrl])
throw new ScriptException("Cipher decoder was not available [" + jsUrl + "]");
return _cipherDecode[jsUrl](cipher);
}
function decryptN(encryptedN, jsUrl) {
if(!_nDecrypt[jsUrl])
throw new ScriptException("N Decryptor was not available [" + jsUrl + "]");
return _nDecrypt[jsUrl](encryptedN);
}
function testCipher(hash) {
const jsUrl = CIPHER_TEST_PREFIX + hash + CIPHER_TEST_SUFFIX;
try{
const result = prepareCipher(jsUrl);
clearCipher(jsUrl);
return {
success: result,
exception: ""
};
}
catch(ex) {
return {
success: false,
exception: ex
};
}
}
source.testCipher = testCipher;
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
function testCiphers() {
let testResults = [];
for(hash of CIPHER_TEST_HASHES) {
const jsUrl = CIPHER_TEST_PREFIX + hash + CIPHER_TEST_SUFFIX;
try{
if(prepareCipher(jsUrl))
testResults.push("CipherTest [" + hash + "]: PASSED");
else
testResults.push("CipherTest [" + hash + "]: FAIL");
}
catch(ex) {
testResults.push(["CipherTest [" + hash + "]: FAIL", ex]);
}
clearCipher(jsUrl);
}
for(result of testResults) {
if(result.constructor === Array)
console.log(result[0], result[1]);
else
console.log(result);
}
}
source.testCiphers = testCiphers;
function prepareCipher(jsUrl) {
if(_cipherDecode[jsUrl])
return false;//_cipherDecode[jsUrl];
log("New JS Url found: [" + jsUrl + "], fetching new js (total: " + (Object.keys(_cipherDecode).length + 1) + ")");
try{
const playerCodeResp = http.GET(URL_BASE + jsUrl, {});
if(!playerCodeResp.isOk) {
if(bridge.devSubmit) bridge.devSubmit("prepareCipher - Failed to get player js", jsUrl);
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
console.log("Javascript Url: " + URL_BASE + jsUrl);
const playerCode = playerCodeResp.body;
const cipherFunctionCode = getCipherFunctionCode(playerCode, jsUrl);
console.log("DecodeCipher Function: " + cipherFunctionCode);
_cipherDecode[jsUrl] = eval(cipherFunctionCode);
const decryptFunctionCode = getNDecryptorFunctionCode(playerCode, jsUrl);
console.log("DecryptN Function: " + decryptFunctionCode);
_nDecrypt[jsUrl] = eval(decryptFunctionCode);
const stsMatch = playerCode.match(STS_REGEX);
console.log("stsMatch: " + stsMatch);
if (stsMatch !== null && stsMatch.length > 1) {
const sts = stsMatch[1];
_sts[jsUrl] = sts;
console.log("sts: " + sts);
}
return true;//_cipherDecode[jsUrl];
}
catch(ex) {
clearCipher(jsUrl);
if(bridge.devSubmit) bridge.devSubmit("prepareCipher - Failed to get Cipher due to: " + ex, jsUrl);
throw new ScriptException("Failed to get Cipher due to: " + ex);
}
}
source.prepareCipher = prepareCipher;
function clearCipher(jsUrl) {
if(_cipherDecode[jsUrl])
_cipherDecode[jsUrl] = undefined;
if(_nDecrypt[jsUrl])
_nDecrypt[jsUrl] = undefined;
}
function getNDecryptorFunctionCode(code, jsUrl) {
if(_nDecrypt[jsUrl])
return _nDecrypt[jsUrl];
let nDecryptFunctionArrNameMatch = REGEX_DECRYPT_N.exec(code);
if(!nDecryptFunctionArrNameMatch) {
console.log("NDecryptor failed, trying fallback");
nDecryptFunctionArrNameMatch = REGEX_DECRYPT_N2.exec(code);
}
if(!nDecryptFunctionArrNameMatch) {
if(bridge.devSubmit) bridge.devSubmit("getNDecryptorFunctionCode - Failed to find n decryptor (name)", jsUrl);
throw new ScriptException("Failed to find n decryptor (name)\n" + jsUrl);
const nDecryptFunctionArrName = nDecryptFunctionArrNameMatch[1];
const nDecryptFunctionArrIndex = parseInt(nDecryptFunctionArrNameMatch[2]);
const nDecryptFunctionNameMatch = code.match(escapeRegex(nDecryptFunctionArrName) + "\\s*=\\s*\\[([$a-zA-Z0-9,\\(,\\)\\.]+?)]");
if(!nDecryptFunctionNameMatch) {
if(bridge.devSubmit) bridge.devSubmit("getNDecryptorFunctionCode - Failed to find n decryptor (array)", jsUrl);
Kelvin
committed
throw new ScriptException("Failed to find n decryptor (array)\n" + jsUrl);
if(nDecryptArray.length <= nDecryptFunctionArrIndex) {
if(bridge.devSubmit) bridge.devSubmit("getNDecryptorFunctionCode - Failed to find n decryptor (index)", jsUrl);
Kelvin
committed
throw new ScriptException("Failed to find n decryptor (index)\n" + jsUrl);
const nDecryptFunctionName = nDecryptArray[nDecryptFunctionArrIndex]
const nDecryptFunctionCodeMatch1 = code.match(escapeRegex(nDecryptFunctionName) + "=function\\(a\\)\\{[\\s\\S]*?join\\(\\\"\\\"\\)};");
const nDecryptFunctionCodeMatch2 = code.match(escapeRegex(nDecryptFunctionName) + "=function\\(a\\)\\{[\\s\\S]*?join\\.call\\([a-zA-Z$_]+,\\\"\\\"\\)};")
let nDecryptFunctionCodeMatch = undefined;
if(nDecryptFunctionCodeMatch1 && !nDecryptFunctionCodeMatch2)
nDecryptFunctionCodeMatch = nDecryptFunctionCodeMatch1;
else if(!nDecryptFunctionCodeMatch1 && nDecryptFunctionCodeMatch2)
nDecryptFunctionCodeMatch = nDecryptFunctionCodeMatch2;
else if(nDecryptFunctionCodeMatch1 && nDecryptFunctionCodeMatch2 && nDecryptFunctionCodeMatch1.length > 0 && nDecryptFunctionCodeMatch2.length > 0) {
if(nDecryptFunctionCodeMatch1[0].length < nDecryptFunctionCodeMatch2[0].length)
nDecryptFunctionCodeMatch = nDecryptFunctionCodeMatch1;
else
nDecryptFunctionCodeMatch = nDecryptFunctionCodeMatch2;
}
if(!nDecryptFunctionCodeMatch) {
if(bridge.devSubmit) bridge.devSubmit("getNDecryptorFunctionCode - Failed to find n decryptor (code)", jsUrl, code);
Kelvin
committed
throw new ScriptException("Failed to find n decryptor (code)\n" + jsUrl);
return "(function(){" +
"var " + nDecryptFunctionCodeMatch[0] + "\n" +
"return function decryptN(nEncrypted){ return " + nDecryptFunctionName + "(nEncrypted); } \n" +
"})()";
}
function getCipherFunctionCode(playerCode, jsUrl) {
if(_cipherDecode[jsUrl])
return _cipherDecode[jsUrl];
let cipherFunctionName = null;
for(let i = 0; i < REGEX_CIPHERS.length; i++) {
const match = playerCode.match(REGEX_CIPHERS[i]);
if(match) {
cipherFunctionName = match[1];
break;
}
}
if(!cipherFunctionName) {
if(bridge.devSubmit) bridge.devSubmit("getCipherFunctionCode - Failed to find cipher (name)", jsUrl);
Kelvin
committed
throw new ScriptException("Failed to find cipher (name)\n" + jsUrl);
const cipherFunctionCodeMatch = playerCode.match("(" + escapeRegex(cipherFunctionName) + "=function\\([a-zA-Z0-9_]+\\)\\{.+?\\})");
if(!cipherFunctionCodeMatch) {
if(IS_TESTING)
console.log("Failed to find cipher function in: ", playerCode);
if(bridge.devSubmit) bridge.devSubmit("getCipherFunctionCode - Failed to find cipher (function)", jsUrl);
Kelvin
committed
throw new ScriptException("Failed to find cipher (function)\n" + jsUrl);
}
const cipherFunctionCode = cipherFunctionCodeMatch[1];
const cipherFunctionCodeVar = "var " + cipherFunctionCode;
const helperObjNameMatch = cipherFunctionCode.match(";([A-Za-z0-9_\\$]{2,3})\\...\\(");
if(!helperObjNameMatch) {
if(IS_TESTING)
console.log("Failed to find helper name in: ", playerCode);
if(bridge.devSubmit) bridge.devSubmit("getCipherFunctionCode - Failed to find helper (name)", jsUrl);
Kelvin
committed
throw new ScriptException("Failed to find helper (name)\n" + jsUrl);
}
if(IS_TESTING)
console.log("Cipher Code: ", cipherFunctionCode);
const helperObjName = helperObjNameMatch[1];
const helperObjMatch = playerCode.match("(var " + escapeRegex(helperObjName) + "=\\{[\\s\\S]*?\\};)");
if(!helperObjMatch) {
if(IS_TESTING)
console.log("Failed to find helper method [" + helperObjName + "] in: ", playerCode);
if(bridge.devSubmit) bridge.devSubmit("getCipherFunctionCode - Failed to find helper (methods)", jsUrl);
Kelvin
committed
throw new ScriptException("Failed to extract helper (methods)\n" + jsUrl);
}
const helperObj = helperObjMatch[1];
const functionCode = "return function decodeCipher(str){ return " + cipherFunctionName + "(str); }";
return "(function(){" + helperObj + "\n" +
cipherFunctionCodeVar + "\n" +
functionCode + "})()";
}
function escapeRegex(str) {
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
}
function decodeHexEncodedString(str) {
return str.replace(/\\x([0-9A-Fa-f]{2})/g, function() {
return String.fromCharCode(parseInt(arguments[1], 16));
});
}
function parseQueryString(query) {
if(query.indexOf("?") >= 0)
query = query.substring(query.indexOf("?") + 1);
const parts = query.split("&");
const results = {};
for(let i = 0; i < parts.length; i++) {
const part = parts[i];
const valueIndex = part.indexOf("=");
if(valueIndex == -1)
results[part] = true;
else
results[part.substring(0, valueIndex)] = part.substring(valueIndex + 1);
}
return results;
}
//#endregion
//#region Others
const RANDOM_CHARACTER_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
function randomString(length) {
let str = "";
for(let i = 0; i < length; i++)
str += RANDOM_CHARACTER_SET[Math.floor(Math.random() * RANDOM_CHARACTER_SET.length)]
return str;
}
function randomInt(start, end) {
return Math.floor(random() * (end + start) - end);
}
//#endregion
console.log("LOADED");