Skip to content

Instantly share code, notes, and snippets.

@tuefekci
Created August 24, 2022 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuefekci/6c0ad2f2fbfb195001afc7af047a808d to your computer and use it in GitHub Desktop.
Save tuefekci/6c0ad2f2fbfb195001afc7af047a808d to your computer and use it in GitHub Desktop.
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import fs from 'fs';
const { Innertube, UniversalCache } = require('youtubei.js');
(async () => {
const youtube = await Innertube.create({
// required if you wish to use OAuth#cacheCredentials
//cache: new UniversalCache()
});
const creds_path = './yt_oauth_creds.json';
const creds = fs.existsSync(creds_path) ? JSON.parse(fs.readFileSync(creds_path).toString()) : {};
youtube.session.on('auth', (data) => {
switch (data.status) {
case 'AUTHORIZATION_PENDING':
console.info(`
Hello! On your phone or computer,
go to ${data.verification_url} and enter
the code ${data.code}.
`);
break;
case 'SUCCESS':
fs.writeFileSync(creds_path, JSON.stringify(data.credentials));
console.info('Successfully signed in, enjoy!');
break;
}
});
youtube.session.on('auth-pending', (data) => console.info(data));
youtube.session.on('update-credentials', (data) => {
fs.writeFileSync(creds_path, JSON.stringify(data.credentials));
console.info('Credentials updated!', data);
});
await youtube.session.signIn(creds).then(async() => {
const info = await youtube.account.getInfo();
console.info('Account getInfo:', info);
const getAnalytics = await youtube.account.getAnalytics();
console.info('Account getAnalytics:', getAnalytics);
try {
const buffer = fs.readFileSync('./short.mp4');
const upload = await youtube.studio.upload(new Uint8Array(buffer), {
title: "WOW!",
description: "test test test",
privacy: 'PRIVATE',
is_draft: true
});
} catch (error) {
console.error(error);
}
console.log("success!")
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment