refactor: move generate-youtube-tokens to util

This commit is contained in:
dumbmoron
2024-06-08 11:52:53 +00:00
parent 9e09bcab6e
commit ebe6668bc0

View File

@@ -0,0 +1,34 @@
import { Innertube } from 'youtubei.js';
const bail = (...msg) => {
console.error(...msg);
throw new Error(msg);
};
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();