Skip to content

Commit

Permalink
feat(getSearchSuggestions): Add optional pq param for better sugges…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
LuanRT committed Dec 17, 2024
1 parent 342fdd1 commit c61db19
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
27 changes: 16 additions & 11 deletions src/Innertube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,26 @@ export default class Innertube {
return new Search(this.actions, response);
}

async getSearchSuggestions(query: string): Promise<string[]> {
const url = new URL(`${Constants.URLS.YT_SUGGESTIONS}search`);
url.searchParams.set('q', query);
async getSearchSuggestions(query: string, previous_query?: string): Promise<string[]> {
const url = new URL(`${Constants.URLS.YT_SUGGESTIONS}/complete/search`);
url.searchParams.set('client', 'youtube');
url.searchParams.set('gs_ri', 'youtube');
url.searchParams.set('gs_id', '0');
url.searchParams.set('cp', '0');
url.searchParams.set('ds', 'yt');
url.searchParams.set('sugexp', '');
url.searchParams.set('hl', this.#session.context.client.hl);
url.searchParams.set('gl', this.#session.context.client.gl);
url.searchParams.set('ds', 'yt');
url.searchParams.set('client', 'youtube');
url.searchParams.set('xssi', 't');
url.searchParams.set('oe', 'UTF');

url.searchParams.set('q', query);

if (previous_query)
url.searchParams.set('pq', previous_query);
const response = await this.#session.http.fetch(url);
const response_data = await response.text();
const text = await response.text();

const data = JSON.parse(response_data.replace(')]}\'', ''));
return data[1].map((suggestion: any) => suggestion[0]);
const data = JSON.parse(text.replace('window.google.ac.h(', '').slice(0, -1));
return data[1].map((suggestion: (string | number)[]) => suggestion[0]);
}

async getComments(video_id: string, sort_by?: 'TOP_COMMENTS' | 'NEWEST_FIRST', comment_id?: string): Promise<Comments> {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export const URLS = Object.freeze({
YT_BASE: 'https://www.youtube.com',
YT_MUSIC_BASE: 'https://music.youtube.com',
YT_SUGGESTIONS: 'https://suggestqueries.google.com/complete/',
YT_SUGGESTIONS: 'https://suggestqueries-clients6.youtube.com',
YT_UPLOAD: 'https://upload.youtube.com/',
API: Object.freeze({
BASE: 'https://youtubei.googleapis.com',
Expand Down Expand Up @@ -36,7 +36,8 @@ export const CLIENTS = Object.freeze({
VERSION: '2.20241121.01.00',
API_KEY: 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8',
API_VERSION: 'v1',
STATIC_VISITOR_ID: '6zpwvWUNAco'
STATIC_VISITOR_ID: '6zpwvWUNAco',
SUGG_EXP_ID: 'ytzpb5_e2,ytpo.bo.lqp.elu=1,ytpo.bo.lqp.ecsc=1,ytpo.bo.lqp.mcsc=3,ytpo.bo.lqp.mec=1,ytpo.bo.lqp.rw=0.8,ytpo.bo.lqp.fw=0.2,ytpo.bo.lqp.szp=1,ytpo.bo.lqp.mz=3,ytpo.bo.lqp.al=en_us,ytpo.bo.lqp.zrm=1,ytpo.bo.lqp.er=1,ytpo.bo.ro.erl=1,ytpo.bo.ro.mlus=3,ytpo.bo.ro.erls=3,ytpo.bo.qfo.mlus=3,ytzprp.ppp.e=1,ytzprp.ppp.st=772,ytzprp.ppp.p=5'
},
MWEB: {
NAME_ID: '2',
Expand Down

0 comments on commit c61db19

Please sign in to comment.