Skip to content

Commit

Permalink
fix: cannot search playlist only
Browse files Browse the repository at this point in the history
  • Loading branch information
skick1234 committed Nov 20, 2023
1 parent e37d755 commit 64200d4
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 79 deletions.
6 changes: 1 addition & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
"rules": {
"no-await-in-loop": "warn",
"no-compare-neg-zero": "error",
"no-extra-parens": ["warn", "all", {
"nestedBinaryExpressions": false
}],
"no-template-curly-in-string": "error",
"no-unsafe-negation": "error",
"valid-jsdoc": ["error", {
Expand Down Expand Up @@ -95,7 +92,7 @@
"func-names": "error",
"func-name-matching": "error",
"func-style": ["error", "declaration", { "allowArrowFunctions": true }],
"indent": ["error", 2, { "SwitchCase": 1 }],
"indent": ["error", 2, { "SwitchCase": 1, "offsetTernaryExpressions": true }],
"key-spacing": "error",
"keyword-spacing": "error",
"max-depth": "error",
Expand All @@ -117,7 +114,6 @@
"nonblock-statement-body-position": "error",
"object-curly-spacing": ["error", "always"],
"operator-assignment": "error",
"operator-linebreak": ["error", "after"],
"padded-blocks": ["error", "never"],
"quote-props": ["error", "as-needed"],
"quotes": ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
Expand Down
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ko_fi: skick
21 changes: 0 additions & 21 deletions .github/workflows/lint.yml

This file was deleted.

26 changes: 0 additions & 26 deletions .github/workflows/node_ci.yml

This file was deleted.

31 changes: 31 additions & 0 deletions .github/workflows/publish_npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Publish @distube/ytdl-core
on:
push:
tags:
- '*'
jobs:
publish:
name: Build & Publish
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

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

- name: Install dependencies
run: npm i

- name: Publish
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}

- name: Deprecate older versions
run: npm deprecate @distube/ytdl-core@"< ${{ github.ref_name }}" "This version is deprecated, please upgrade to the latest version."
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
coverage
.nyc_output
test.js
package-lock.json
package-lock.json
dumps
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"semi": true,
"trailingComma": "all",
"printWidth": 120,
"endOfLine": "lf",
"quoteProps": "as-needed",
"arrowParens": "avoid",
"tabWidth": 2,
"singleQuote": true
}
51 changes: 33 additions & 18 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const PARSE_ITEM = require('./parseItem.js');
const { request } = require('undici');
const UTIL = require('./util.js');
const QS = require('querystring');

const BASE_SEARCH_URL = 'https://www.youtube.com/results?';
const BASE_API_URL = 'https://www.youtube.com/youtubei/v1/search?key=';
const BASE_SEARCH_URL = 'https://www.youtube.com/results';
const BASE_API_URL = 'https://www.youtube.com/youtubei/v1/search';
const CACHE = new Map();

// Save api key and client version for safeSearch
Expand All @@ -16,7 +15,7 @@ const saveCache = (parsed, opts) => {
};

// eslint-disable-next-line complexity
const main = module.exports = async (searchString, options, rt = 3) => {
const main = (module.exports = async (searchString, options, rt = 3) => {
if (rt === 2) {
CACHE.delete('apiKey');
CACHE.delete('clientVersion');
Expand All @@ -26,29 +25,43 @@ const main = module.exports = async (searchString, options, rt = 3) => {
// Set default values
const opts = UTIL.checkArgs(searchString, options);

const ref = BASE_SEARCH_URL + QS.encode(opts.query);
let parsed = {};
if (!opts.safeSearch || !CACHE.has('apiKey') || !CACHE.has('clientVersion') || !CACHE.has('playlistParams')) {
const body = await request(ref, opts.requestOptions).then(r => r.body.text());
const body = await request(BASE_SEARCH_URL, Object.assign({}, opts.requestOptions, { query: opts.query })).then(r =>
r.body.text(),
);
parsed = UTIL.parseBody(body, opts);
let plParams = UTIL.betweenFromRight(body, `"params":"`, '"}},"tooltip":"Search for Playlist"');
if (plParams) CACHE.set('playlistParams', plParams);
saveCache(parsed, opts);
}
if (opts.type === 'playlist') {
let params = 'EgIQAw%253D%253D';
if (CACHE.has('playlistParams')) params = CACHE.get('playlistParams');
parsed.json = await UTIL.doPost(BASE_API_URL + parsed.apiKey, opts, {
context: parsed.context,
params,
query: searchString,
}).catch(() => null);
const params = CACHE.has('playlistParams') ? CACHE.get('playlistParams') : 'EgIQAw%253D%253D';
parsed.json = await UTIL.doPost(
BASE_API_URL,
Object.assign({}, opts.requestOptions, {
query: { key: parsed.apiKey, prettyPrint: false },
}),
{
context: parsed.context,
params,
query: searchString,
},
);
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');
const context = parsed.context;
parsed.json = await UTIL.doPost(BASE_API_URL + parsed.apiKey, opts, { context, query: searchString });
parsed.json = await UTIL.doPost(
BASE_API_URL,
Object.assign({}, opts.requestOptions, {
query: { key: parsed.apiKey, prettyPrint: false },
}),
{
context: parsed.context,
query: searchString,
},
);
} catch (e) {
if (rt === 1) throw e;
}
Expand All @@ -67,7 +80,8 @@ const main = module.exports = async (searchString, options, rt = 3) => {
);

// Parse items
resp.items = rawItems.map(a => PARSE_ITEM(a, resp))
resp.items = rawItems
.map(a => PARSE_ITEM(a, resp))
.filter(r => r && r.type === opts.type)
.filter((_, index) => index < opts.limit);

Expand All @@ -93,7 +107,7 @@ const main = module.exports = async (searchString, options, rt = 3) => {
UTIL.logger(parsed);
throw new Error(e);
}
};
});

const parsePage2 = async (apiKey, token, context, opts) => {
const json = await UTIL.doPost(BASE_API_URL + apiKey, opts.requestOptions, { context, continuation: token });
Expand All @@ -105,7 +119,8 @@ const parsePage2 = async (apiKey, token, context, opts) => {
const { rawItems, continuation } = UTIL.parsePage2Wrapper(
json.onResponseReceivedCommands[0].appendContinuationItemsAction.continuationItems,
);
const parsedItems = rawItems.map(PARSE_ITEM)
const parsedItems = rawItems
.map(PARSE_ITEM)
.filter(r => r && r.type === opts.type)
.filter((_, index) => index < opts.limit);

Expand Down
10 changes: 5 additions & 5 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exports.parseBody = (body, options = {}) => {
return { json, apiKey, context };
};

const buildPostContext = exports.buildPostContext = (clientVersion, options = {}) => {
const buildPostContext = (exports.buildPostContext = (clientVersion, options = {}) => {
// Make deep copy and set clientVersion
const context = clone(DEFAULT_CONTEXT);
context.client.clientVersion = clientVersion;
Expand All @@ -54,13 +54,13 @@ const buildPostContext = exports.buildPostContext = (clientVersion, options = {}
if (options.utcOffsetMinutes) context.client.utcOffsetMinutes = options.utcOffsetMinutes;
if (options.safeSearch) context.user.enableSafetyMode = true;
return context;
};
});

// Parsing utility
const parseText = exports.parseText = txt =>
typeof txt === 'object' ? txt.simpleText || (Array.isArray(txt.runs) ? txt.runs.map(a => a.text).join('') : '') : '';
const parseText = (exports.parseText = txt =>
typeof txt === 'object' ? txt.simpleText || (Array.isArray(txt.runs) ? txt.runs.map(a => a.text).join('') : '') : '');

exports.parseIntegerFromText = x => typeof x === 'string' ? Number(x) : Number(parseText(x).replace(/\D+/g, ''));
exports.parseIntegerFromText = x => (typeof x === 'string' ? Number(x) : Number(parseText(x).replace(/\D+/g, '')));

// Request Utility
exports.doPost = (url, opts, payload) => {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
"lint:fix": "eslint --fix ./"
},
"dependencies": {
"undici": "^5.25.2"
"undici": "^5.27.2"
},
"devDependencies": {
"eslint": "^8.49.0"
"eslint": "^8.54.0"
},
"engines": {
"node": ">=8"
},
"homepage": "https://github.com/distubejs/ytsr#readme"
"homepage": "https://github.com/distubejs/ytsr#readme",
"funding": "https://github.com/distubejs/ytsr?sponsor=1"
}

0 comments on commit 64200d4

Please sign in to comment.