diff --git a/src/modules/processing/services/youtube.js b/src/modules/processing/services/youtube.js index 39a94f3a..301b37b7 100644 --- a/src/modules/processing/services/youtube.js +++ b/src/modules/processing/services/youtube.js @@ -24,6 +24,26 @@ const codecMatch = { } } +const transformSessionData = (cookie) => { + if (!cookie) + return; + + const values = cookie.values(); + const REQUIRED_VALUES = [ + 'access_token', 'refresh_token', + 'client_id', 'client_secret', + 'expires' + ]; + + if (REQUIRED_VALUES.some(x => typeof values[x] !== 'string')) { + return; + } + return { + ...values, + expires: new Date(values.expires), + }; +} + const cloneInnertube = async (customFetch) => { const innertube = await ytBase; if (innertube instanceof Error) { @@ -41,6 +61,17 @@ const cloneInnertube = async (customFetch) => { innertube.session.cache ); + const oauthData = transformSessionData(getCookie('youtube_oauth')); + + if (!session.logged_in && oauthData) { + await session.oauth.init(oauthData); + session.logged_in = true; + } + + if (session.logged_in) { + await session.oauth.refreshIfRequired(); + } + const yt = new Innertube(session); return yt; } @@ -62,7 +93,7 @@ export default async function(o) { } try { - info = await yt.getBasicInfo(o.id, 'IOS'); + info = await yt.getBasicInfo(o.id, yt.session.logged_in ? 'ANDROID' : 'IOS'); } catch(e) { if (e?.message === 'This video is unavailable') { return { error: 'ErrorCouldntFetch' }; diff --git a/src/modules/sub/generate-youtube-tokens.js b/src/modules/sub/generate-youtube-tokens.js new file mode 100644 index 00000000..26a6a1cd --- /dev/null +++ b/src/modules/sub/generate-youtube-tokens.js @@ -0,0 +1,30 @@ +import { Innertube } from 'youtubei.js'; + +const bail = (...msg) => (console.error(...msg), process.exit(1)); +const tube = await Innertube.create(); + +tube.session.once( + 'auth-pending', + ({ verification_url, user_code }) => console.log( + `Open ${verification_url} in a browser and enter ${user_code} when asked for the code.` + ) +); + +tube.session.once('auth-error', (err) => bail('An error occurred:', err)); +tube.session.once('auth', ({ status, credentials, ...rest }) => { + if (status !== 'SUCCESS') { + bail('something went wrong', rest); + } + + console.log(credentials); + + console.log( + JSON.stringify( + Object.entries(credentials) + .map(([k, v]) => `${k}=${v instanceof Date ? v.toISOString() : v}`) + .join('; ') + ) + ); +}); + +await tube.session.signIn(); \ No newline at end of file