123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- console.log('bundle2!')
- printable = (platform) => {
- return platform === "WEB";
- }
- parseCodecs = (format) => {
- const mimeType = format['mimeType']
- if (!mimeType) {
- return {};
- }
- const regex = /(?<mimetype>[^/]+\/[^;]+)(?:;\s*codecs="?(?<codecs>[^"]+))?/;
- const match = mimeType.match(regex);
- if (!match) {
- return {};
- }
- const codecs = match.groups.codecs;
- if (!codecs) {
- return {};
- }
- const splitCodecs = codecs.trim().replace(/,$/, '').split(',').map(str => str.trim()).filter(Boolean);
- let vcodec = null;
- let acodec = null;
- for (const fullCodec of splitCodecs) {
- const codec = fullCodec.split('.')[0];
- if (['avc1', 'avc2', 'avc3', 'avc4', 'vp9', 'vp8', 'hev1', 'hev2', 'h263', 'h264', 'mp4v', 'hvc1', 'av01', 'theora'].includes(codec)) {
- if (!vcodec) {
- vcodec = fullCodec;
- }
- } else if (['mp4a', 'opus', 'vorbis', 'mp3', 'aac', 'ac-3', 'ec-3', 'eac3', 'dtsc', 'dtse', 'dtsh', 'dtsl'].includes(codec)) {
- if (!acodec) {
- acodec = fullCodec;
- }
- } else {
- console.log(`WARNING: Unknown codec ${fullCodec}`);
- }
- }
- if (!vcodec && !acodec) {
- if (splitCodecs.length === 2) {
- return {
- vcodec: splitCodecs[0],
- acodec: splitCodecs[1]
- };
- }
- } else {
- return {
- vcodec: vcodec,
- acodec: acodec
- };
- }
- return {};
- }
- parseSetCookie = (headers) => {
- if (!headers) {
- return ""
- }
- const setCookie = headers['Set-Cookie']
- if (!setCookie) {
- return ""
- }
- console.log(`setCookie: ${setCookie}`)
- let result = 'PREF=hl=en&tz=UTC; SOCS=CAI; GPS=1; ';
- const needCookieNames = ['YSC', 'VISITOR_INFO1_LIVE', 'VISITOR_PRIVACY_METADATA'];
- for (const i in needCookieNames) {
- const cookieName = needCookieNames[i];
- const regexp = new RegExp(`${cookieName}=([^;,]+)`)
- const match = setCookie.match(regexp)
- if (match && match.length === 2) {
- const cookieValue = match[1]
- if (i != needCookieNames.length - 1) {
- result += `${cookieName}=${cookieValue}; `
- } else {
- result += `${cookieName}=${cookieValue}`
- }
- }
- }
- console.log(`current cookie: ${result}`)
- return result;
- }
- request = async (method, url, data = null, headers = {}, platform) => {
- if (platform === "WEB") {
- url = url.replace("https://www.youtube.com/", "http://127.0.0.1:80/");
- url = url.replace("https://music.youtube.com/", "http://127.0.0.1:80/");
- }
- console.log(`request url:${url}`)
- console.log(`request data:${data}`)
- console.log(`request method:${method}`)
- console.log(`request headers:${JSON.stringify((headers))}`)
- if (platform === "WEB") {
- const res = await fetch(url, {
- 'mode': 'cors',
- 'method': method,
- 'headers': headers,
- 'body': data
- })
- const resData = await res.text()
- return Promise.resolve({
- 'data': resData,
- 'headers': res.headers
- });
- }
- return new Promise((resolve, reject) => {
- AF.request(url, method, data, headers, (data, headers, err) => {
- if (err) {
- reject(err);
- } else {
- console.log(`response headers: ${headers}`);
- resolve({
- 'data': data,
- 'headers': JSON.parse(headers)
- });
- }
- });
- })
- }
- getStringBetween = (string, needleStart, needleEnd, offsetStart = 0, offsetEnd = 0) => {
- const x = string.indexOf(needleStart);
- const y = needleEnd ? string.indexOf(needleEnd, x) : string.length;
- return string.substring(x + needleStart.length + offsetEnd, y + offsetStart);
- }
- findFunction = (jsCode, regexp, platform) => {
- const match = jsCode.match(regexp)
- if (!match && match.length <= 1) {
- return null;
- }
- let result = "";
- const dependencyMatches = match[0].match(/([$a-zA-Z0-9]+\.[$a-zA-Z0-9]+)/g)
- const existDependencies = [];
- if (dependencyMatches && dependencyMatches.length >= 1) {
- for (let currentMatch of dependencyMatches) {
- const varName = currentMatch.split('.')[0];
- if (existDependencies.includes(varName)) {
- continue
- }
- if (!/^[$A-Z]+$/.test(varName)) {
- continue
- }
- let reg = "var (\$)?" + varName + "={(.|\\n)*?};"
- const varNameMatch = jsCode.match(new RegExp(reg), 'ig');
- if (varNameMatch && varNameMatch.length >= 1) {
- result += varNameMatch[0] + "\n";
- }
- existDependencies.push(varName);
- }
- }
- result += `\n${match[0]}`;
- if (printable(platform)) {
- console.log(`findFunction result: ` + result);
- }
- return eval(result);
- };
- const cache = {};
- fetchBaseJSContent = async (baseJsUrl, platform) => {
- const cacheKey = `jsContent:${baseJsUrl}`;
- if (cache[cacheKey]) {
- console.log(`baseContent from cache: ${baseJsUrl}`);
- return cache[cacheKey];
- }
- console.log(`extract baseUrl: ${baseJsUrl}`);
- const baseContentResp = await request('GET', baseJsUrl, null, {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Safari/537.36',
- }, platform);
- const {data, _} = baseContentResp;
- cache[cacheKey] = data;
- return data;
- }
- extractJSSignatureFunction = async (baseJsUrl, platform) => {
- const cacheKey = `jsSign:${baseJsUrl}`
- if (cache[cacheKey]) {
- console.log(`jsSign from cache: ${baseJsUrl}`);
- return cache[cacheKey];
- }
- const baseJsContent = await fetchBaseJSContent(baseJsUrl, platform);
- const result = findFunction(baseJsContent, /([a-zA-Z0-9]+)=function\([a-zA-Z0-9]+\)\{a=a\.split\(""\).*};/, platform);
- cache[cacheKey] = result
- return result
- }
- extractNJSFunction = async (baseJsUrl, platform) => {
- const cacheKey = `jsN:${baseJsUrl}`
- if (cache[cacheKey]) {
- console.log(`jsN from cache: ${baseJsUrl}`);
- return cache[cacheKey];
- }
- const baseJsContent = await fetchBaseJSContent(baseJsUrl, platform);
- const result = findFunction(baseJsContent, /([a-zA-Z0-9]+)=function\([a-zA-Z0-9]+\)\{var b=a\.split\(""\)[\s\S]*?};/, platform);
- cache[cacheKey] = result
- return result
- }
- signUrl = async (signatureCipher, baseJsUrl, platform) => {
- const searchParams = {}
- for (const item of signatureCipher.split('&')) {
- const [key, value] = item.split('=');
- searchParams[decodeURIComponent(key)] = decodeURIComponent(value);
- }
- const [url, signature, sp] = [searchParams['url'], searchParams['s'], searchParams['sp']];
- const decipher = await extractJSSignatureFunction(baseJsUrl, platform);
- if (!decipher) {
- return null;
- }
- if (printable(platform)) {
- console.log(`signatureCipher=${signatureCipher}, url=${url}, signature=${signature}, sp=${sp}`)
- }
- let newUrl = `${url}&${sp}=${decipher(signature)}`;
- function replaceUrlParam(url, paramName, paramValue) {
- let pattern = new RegExp(`([?&])${paramName}=.*?(&|$)`, 'i');
- let newUrl = url.replace(pattern, `$1${paramName}=${paramValue}$2`);
- if (newUrl === url && url.indexOf('?') === -1) {
- newUrl += `?${paramName}=${paramValue}`;
- } else if (newUrl === url) {
- newUrl += `&${paramName}=${paramValue}`;
- }
- return newUrl;
- }
- for (const item of url.split('&')) {
- const [key, value] = item.split('=');
- searchParams[decodeURIComponent(key)] = decodeURIComponent(value);
- }
- const nFunction = await extractNJSFunction(baseJsUrl, platform);
- const n = searchParams['n']
- if (n && nFunction) {
- const newN = nFunction(n);
- return replaceUrlParam(newUrl, 'n', newN);
- }
- return newUrl;
- }
- detail = async (url, platform) => {
- try {
- const htmlResp = await request('GET', `${url}&bpctr=9999999999&has_verified=1`, null, {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*
|