From 320c0073966a088c55a1db63ffd6c3a6b6cde4aa Mon Sep 17 00:00:00 2001 From: LuanRT Date: Tue, 6 Sep 2022 03:34:07 -0300 Subject: [PATCH] docs: add video upload example --- examples/upload/index.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/upload/index.ts diff --git a/examples/upload/index.ts b/examples/upload/index.ts new file mode 100644 index 00000000..fbbeea64 --- /dev/null +++ b/examples/upload/index.ts @@ -0,0 +1,35 @@ +import { readFileSync, writeFileSync, existsSync } from 'fs'; +import { Innertube, UniversalCache } from 'youtubei.js'; + +const creds_path = './my_yt_creds.json'; +const creds = existsSync(creds_path) ? JSON.parse(readFileSync(creds_path).toString()) : undefined; + +(async () => { + const yt = await Innertube.create({ cache: new UniversalCache() }); + + yt.session.on('auth-pending', (data: any) => { + console.info(`Hello!\nOn your phone or computer, go to ${data.verification_url} and enter the code ${data.user_code}`); + }); + + yt.session.on('auth', (data: any) => { + writeFileSync(creds_path, JSON.stringify(data.credentials)); + console.info('Successfully signed in!'); + }); + + yt.session.on('update-credentials', (data: any) => { + writeFileSync(creds_path, JSON.stringify(data.credentials)); + console.info('Credentials updated!', data); + }); + + await yt.session.signIn(creds); + + const file = readFileSync('./my_awesome_video.mp4'); + + const upload = await yt.studio.upload(file.buffer, { + title: 'Wow!', + description: new Date().toString(), + privacy: 'UNLISTED' + }); + + console.info('Done!'); +})();