Skip to content

Commit

Permalink
refactor: remove unnecessary parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
skick1234 committed Jun 10, 2024
1 parent f80269a commit 590ec5d
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 40 deletions.
Empty file modified .eslintignore
100644 → 100755
Empty file.
1 change: 0 additions & 1 deletion .eslintrc.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
"array-bracket-spacing": "error",
"block-spacing": "error",
"brace-style": ["error", "1tbs", { "allowSingleLine": true }],
"capitalized-comments": ["error", "always", { "ignoreConsecutiveComments": true }],
"comma-dangle": ["error", "always-multiline"],
"comma-spacing": "error",
"comma-style": "error",
Expand Down
Empty file modified .github/FUNDING.yml
100644 → 100755
Empty file.
Empty file modified .github/stale.yml
100644 → 100755
Empty file.
7 changes: 2 additions & 5 deletions .github/workflows/publish_npm.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ jobs:
uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
registry-url: 'https://registry.npmjs.org'

- name: Install dependencies
run: npm i

- name: Publish
run: npm publish
env:
Expand Down
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .prettierrc
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Binary file added bun.lockb
Binary file not shown.
80 changes: 55 additions & 25 deletions lib/main.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@ const UTIL = require('./util.js');
const BASE_SEARCH_URL = 'https://www.youtube.com/results';
const BASE_API_URL = 'https://www.youtube.com/youtubei/v1/search';
const CACHE = new Map([
['apiKey', 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'],
['clientVersion', '2.20231121.08.00'],
['playlistParams', 'EgIQAw%253D%253D'],
// ['apiKey', 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'],
['clientVersion', '2.20240606.06.00'],
['playlistParams', 'EgIQAw%3D%3D'],
]);

// Save api key and client version for safeSearch
const saveCache = (parsed, opts) => {
if (parsed.apiKey) CACHE.set('apiKey', parsed.apiKey);
else if (CACHE.has('apiKey')) parsed.apiKey = CACHE.get('apiKey');
if (parsed.context) CACHE.set('clientVersion', parsed.context.client.clientVersion);
else if (CACHE.has('clientVersion')) parsed.context = UTIL.buildPostContext(CACHE.get('clientVersion'), opts);
// if (parsed.apiKey) CACHE.set('apiKey', parsed.apiKey);
// else if (CACHE.has('apiKey')) parsed.apiKey = CACHE.get('apiKey');
if (parsed.context && parsed.context.client && parsed.context.client.clientVersion) {
CACHE.set('clientVersion', parsed.context.client.clientVersion);
} else if (CACHE.has('clientVersion')) {
parsed.context = UTIL.buildPostContext(CACHE.get('clientVersion'), opts);
}
const plParams =
UTIL.getPlaylistParams(parsed) ||
UTIL.betweenFromRight(parsed.body, `"params":"`, '"}},"tooltip":"Search for Playlist"');
if (plParams) CACHE.set('playlistParams', plParams);
};

// eslint-disable-next-line complexity
const main = (module.exports = async (searchString, options, rt = 3) => {
UTIL.checkForUpdates();
if (rt === 2) {
CACHE.delete('apiKey');
// CACHE.delete('apiKey');
CACHE.delete('clientVersion');
CACHE.delete('playlistParams');
}
Expand All @@ -31,21 +37,26 @@ const main = (module.exports = async (searchString, options, rt = 3) => {
const opts = UTIL.checkArgs(searchString, options);

let parsed = {};
if (!opts.safeSearch || !CACHE.has('apiKey') || !CACHE.has('clientVersion') || !CACHE.has('playlistParams')) {
const body = await request(BASE_SEARCH_URL, Object.assign({}, opts.requestOptions, { query: opts.query })).then(r =>
r.body.text(),
);
if (
!opts.safeSearch ||
!CACHE.has('clientVersion') ||
!CACHE.has('playlistParams')
// || !CACHE.has('apiKey')
) {
const res = await request(BASE_SEARCH_URL, Object.assign({}, opts.requestOptions, { query: opts.query }));
const body = await res.body.text();
parsed = UTIL.parseBody(body, opts);
const plParams = UTIL.betweenFromRight(body, `"params":"`, '"}},"tooltip":"Search for Playlist"');
if (plParams) CACHE.set('playlistParams', plParams);
saveCache(parsed, opts);
}
if (opts.type === 'playlist') {
const params = CACHE.get('playlistParams');
parsed.json = await UTIL.doPost(
BASE_API_URL,
Object.assign({}, opts.requestOptions, {
query: { key: parsed.apiKey, prettyPrint: false },
query: {
// key: parsed.apiKey,
prettyPrint: false,
},
}),
{
context: parsed.context,
Expand All @@ -56,11 +67,13 @@ const main = (module.exports = async (searchString, options, rt = 3) => {
if (!parsed.json) throw new Error('Cannot searching for Playlist!');
} else if (opts.safeSearch || !parsed.json) {
try {
if (!parsed.apiKey || !parsed.context.client.clientVersion) throw new Error('Missing api key');
parsed.json = await UTIL.doPost(
BASE_API_URL,
Object.assign({}, opts.requestOptions, {
query: { key: parsed.apiKey, prettyPrint: false },
query: {
// key: parsed.apiKey,
prettyPrint: false,
},
}),
{
context: parsed.context,
Expand All @@ -74,9 +87,7 @@ const main = (module.exports = async (searchString, options, rt = 3) => {

if (!parsed.json) return main(searchString, options, rt - 1);

const resp = {
query: opts.search,
};
const resp = { query: opts.search };

try {
// Parse items
Expand All @@ -102,24 +113,38 @@ const main = (module.exports = async (searchString, options, rt = 3) => {
// We're already on last page or hit the limit
if (!token || opts.limit < 1) return resp;
// Recursively fetch more items
const nestedResp = await parsePage2(parsed.apiKey, token, parsed.context, opts);
const nestedResp = await parsePage2(
// parsed.apiKey,
token,
parsed.context,
opts,
);

// Merge the responses
resp.items.push(...nestedResp);
return resp;
} catch (e) {
parsed.query = searchString;
parsed.requestOptions = opts.requestOptions;
parsed.error = UTIL.errorToObject(e);
UTIL.logger(parsed);
throw e;
}
});

const parsePage2 = async (apiKey, token, context, opts) => {
const parsePage2 = async (
// apiKey,
token,
context,
opts,
) => {
const json = await UTIL.doPost(
BASE_API_URL,
Object.assign({}, opts.requestOptions, {
query: { key: apiKey, prettyPrint: false },
query: {
// key: apiKey,
prettyPrint: false,
},
}),
{ context, continuation: token },
);
Expand All @@ -146,7 +171,12 @@ const parsePage2 = async (apiKey, token, context, opts) => {
if (!nextToken || opts.limit < 1) return parsedItems;

// Recursively fetch more items
const nestedResp = await parsePage2(apiKey, nextToken, context, opts);
const nestedResp = await parsePage2(
// apiKey,
nextToken,
context,
opts,
);
parsedItems.push(...nestedResp);
return parsedItems;
} catch (e) {
Expand Down
Empty file modified lib/parseItem.js
100644 → 100755
Empty file.
35 changes: 31 additions & 4 deletions lib/util.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const DEFAULT_CONTEXT = {
clientVersion: '<important information>',
},
user: {},
request: {},
};
const CONSENT_COOKIE = 'SOCS=CAI';

Expand All @@ -29,14 +28,31 @@ const tryParseBetween = (body, left, right, addEndCurly = false) => {
}
};

const getClientVersion = json => {
try {
const { serviceTrackingParams } = json.responseContext;
for (const service of serviceTrackingParams) {
if (!Array.isArray(service.params)) continue;
const param = service.params.find(p => p.key === 'cver');
if (!param || !param.value) continue;
return param.value;
}
} catch (e) {
// noop
}
return null;
};

exports.parseBody = (body, options = {}) => {
const json =
tryParseBetween(body, 'var ytInitialData = ', '};', true) ||
tryParseBetween(body, 'window["ytInitialData"] = ', '};', true) ||
tryParseBetween(body, 'var ytInitialData = ', ';</script>') ||
tryParseBetween(body, 'window["ytInitialData"] = ', ';</script>');
const apiKey = between(body, 'INNERTUBE_API_KEY":"', '"') || between(body, 'innertubeApiKey":"', '"');
const apiKey = undefined;
// between(body, 'INNERTUBE_API_KEY":"', '"') || between(body, 'innertubeApiKey":"', '"');
const clientVersion =
getClientVersion(json) ||
between(body, 'INNERTUBE_CONTEXT_CLIENT_VERSION":"', '"') ||
between(body, 'innertube_context_client_version":"', '"');
const context = buildPostContext(clientVersion, options);
Expand All @@ -63,10 +79,11 @@ const parseText = (exports.parseText = txt =>
exports.parseIntegerFromText = x => (typeof x === 'string' ? Number(x) : Number(parseText(x).replace(/\D+/g, '')));

// Request Utility
exports.doPost = (url, opts, payload) => {
exports.doPost = async (url, opts, payload) => {
if (!opts) opts = {};
const reqOpts = Object.assign({}, opts, { method: 'POST', body: JSON.stringify(payload) });
return request(url, reqOpts).then(r => r.body.json());
const r = await request(url, reqOpts);
return r.body.json();
};

// Guarantee that all arguments are valid
Expand Down Expand Up @@ -307,3 +324,13 @@ exports.errorToObject = err => {
});
return obj;
};

exports.getPlaylistParams = parsed => {
try {
return parsed.json.header.searchHeaderRenderer.searchFilterButton.buttonRenderer.command.openPopupAction.popup
.searchFilterOptionsDialogRenderer.groups[1].searchFilterGroupRenderer.filters[2].searchFilterRenderer
.navigationEndpoint.searchEndpoint.params;
} catch (e) {
return null;
}
};
10 changes: 5 additions & 5 deletions package.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
"lint:fix": "eslint --fix ./"
},
"dependencies": {
"undici": "6.0.1"
"undici": "^6.18.2"
},
"devDependencies": {
"eslint": "^8.56.0",
"prettier": "^3.1.1"
"eslint": "^8.57.0",
"prettier": "^3.3.1"
},
"engines": {
"node": ">=18.0"
"node": ">=14.0"
},
"homepage": "https://github.com/distubejs/ytsr#readme",
"funding": "https://github.com/distubejs/ytsr?sponsor=1"
"funding": "https://github.com/distubejs/ytsr?sponsor"
}
Empty file modified typings/index.d.ts
100644 → 100755
Empty file.

0 comments on commit 590ec5d

Please sign in to comment.