From 22cae269e8c7a1f09b6485ef70305d4c44b52989 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Sun, 1 Jan 2023 19:13:36 -0300 Subject: [PATCH 1/3] feat: allow enabling safety mode Unrelated: this also simplifies the creation of sessions without a player instance. --- src/core/Session.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/core/Session.ts b/src/core/Session.ts index 9adf15f4b5..39d1e18b10 100644 --- a/src/core/Session.ts +++ b/src/core/Session.ts @@ -46,7 +46,8 @@ export interface Context { utcOffsetMinutes: number; }; user: { - lockedSafetyMode: false; + enableSafetyMode: boolean; + lockedSafetyMode: boolean; }; thirdParty?: { embedUrl: string; @@ -60,6 +61,8 @@ export interface SessionOptions { lang?: string; location?: string; account_index?: number; + retrieve_player?: boolean; + enable_safety_mode?: boolean; device_category?: DeviceCategory; client_type?: ClientType; timezone?: string; @@ -117,18 +120,24 @@ export default class Session extends EventEmitterLike { options.lang, options.location, options.account_index, + options.enable_safety_mode, options.device_category, options.client_type, options.timezone, options.fetch ); - return new Session(context, api_key, api_version, account_index, await Player.create(options.cache, options.fetch), options.cookie, options.fetch, options.cache); + return new Session( + context, api_key, api_version, account_index, + options.retrieve_player ? await Player.create(options.cache, options.fetch) : undefined, + options.cookie, options.fetch, options.cache + ); } static async getSessionData( lang = 'en-US', location = '', account_index = 0, + enable_safety_mode = false, device_category: DeviceCategory = 'desktop', client_name: ClientType = ClientType.WEB, tz: string = Intl.DateTimeFormat().resolvedOptions().timeZone, @@ -186,6 +195,7 @@ export default class Session extends EventEmitterLike { utcOffsetMinutes: new Date().getTimezoneOffset() }, user: { + enableSafetyMode: enable_safety_mode, lockedSafetyMode: false }, request: { From 1cdf4134abb93309833c7583222ce5704163d484 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Sun, 1 Jan 2023 19:29:18 -0300 Subject: [PATCH 2/3] chore: add tests --- test/main.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/main.test.ts b/test/main.test.ts index de2130f956..915f9b1618 100644 --- a/test/main.test.ts +++ b/test/main.test.ts @@ -10,7 +10,7 @@ describe('YouTube.js Tests', () => { beforeAll(async () => { yt = await Innertube.create(); }); - + describe('Info', () => { let info: any; @@ -116,6 +116,11 @@ describe('YouTube.js Tests', () => { }); describe('General', () => { + it('should create sessions without a player instance', async () => { + const nop_yt = await Innertube.create({ retrieve_player: false }); + expect(nop_yt.session.player).toBeUndefined(); + }); + it('should resolve a URL', async () => { const url = await yt.resolveURL('https://www.youtube.com/@linustechtips'); expect(url.payload.browseId).toBe(CHANNELS[0].ID); From dab6ffb5a9e57e07cca6f430710749a734159b00 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Sun, 1 Jan 2023 19:46:12 -0300 Subject: [PATCH 3/3] fix: retrieve player by default --- src/core/Session.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/Session.ts b/src/core/Session.ts index 39d1e18b10..4741ed3295 100644 --- a/src/core/Session.ts +++ b/src/core/Session.ts @@ -126,9 +126,10 @@ export default class Session extends EventEmitterLike { options.timezone, options.fetch ); + return new Session( context, api_key, api_version, account_index, - options.retrieve_player ? await Player.create(options.cache, options.fetch) : undefined, + options.retrieve_player === false ? undefined : await Player.create(options.cache, options.fetch), options.cookie, options.fetch, options.cache ); }