Skip to content

How can this module be used globally? #17

@TCH68k

Description

@TCH68k

Question

I've installed the module by npm -g install bgutils-js youtubei.js jsdom ts-node and tried to run the source got from you

import { BG } from 'bgutils-js';
import type { BgConfig } from 'bgutils-js';
import { JSDOM } from 'jsdom';
// Bun:
// import { Innertube, UniversalCache } from 'youtubei.js/web';
import { Innertube, UniversalCache } from 'youtubei.js';

// Create a barebones Innertube instance so we can get a visitor data string from YouTube.
let innertube = await Innertube.create({ retrieve_player: false });

const requestKey = 'O43z0dpjhgX20SCx4KAo';
const visitorData = innertube.session.context.client.visitorData;

if (!visitorData)
  throw new Error('Could not get visitor data');

const dom = new JSDOM();

Object.assign(globalThis, {
  window: dom.window,
  document: dom.window.document
});

const bgConfig: BgConfig = {
  fetch: (input: string | URL | globalThis.Request, init?: RequestInit) => fetch(input, init), 
  globalObj: globalThis,
  identifier: visitorData,
  requestKey
};

const bgChallenge = await BG.Challenge.create(bgConfig);

if (!bgChallenge)
  throw new Error('Could not get challenge');

const interpreterJavascript = bgChallenge.interpreterJavascript.privateDoNotAccessOrElseSafeScriptWrappedValue;

if (interpreterJavascript) {
  new Function(interpreterJavascript)();
} else throw new Error('Could not load VM');

const poTokenResult = await BG.PoToken.generate({
  program: bgChallenge.program,
  globalName: bgChallenge.globalName,
  bgConfig
});

const placeholderPoToken = BG.PoToken.generatePlaceholder(visitorData);

console.info('Session Info:', {
  visitorData,
  placeholderPoToken,
  poToken: poTokenResult.poToken,
  integrityTokenData: poTokenResult.integrityTokenData
});

innertube = await Innertube.create({
  po_token: poTokenResult.poToken,
  visitor_data: visitorData,
  cache: new UniversalCache(true),
  generate_session_locally: true
});

const info = await innertube.getBasicInfo('v8SlCIo0DRs');
if (info.streaming_data) {
  const format = info.streaming_data.adaptive_formats.find((fmt) => fmt.itag === 299);

  if (format) {
    console.log('Stream URL:', format.decipher(innertube.session.player));
  }
}

by npx ts-node test.ts and only got these errors:

/opt/node.js/23.6.0/lib/node_modules/ts-node/src/index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
test.ts:1:20 - error TS2307: Cannot find module 'bgutils-js' or its corresponding type declarations.

1 import { BG } from 'bgutils-js';
                     ~~~~~~~~~~~~
test.ts:2:31 - error TS2307: Cannot find module 'bgutils-js' or its corresponding type declarations.

2 import type { BgConfig } from 'bgutils-js';
                                ~~~~~~~~~~~~
test.ts:3:23 - error TS2307: Cannot find module 'jsdom' or its corresponding type declarations.

3 import { JSDOM } from 'jsdom';
                        ~~~~~~~
test.ts:6:43 - error TS2307: Cannot find module 'youtubei.js' or its corresponding type declarations.

6 import { Innertube, UniversalCache } from 'youtubei.js';
                                            ~~~~~~~~~~~~~
test.ts:66:61 - error TS7006: Parameter 'fmt' implicitly has an 'any' type.

66   const format = info.streaming_data.adaptive_formats.find((fmt) => fmt.itag === 299);
                                                               ~~~

    at createTSError (/opt/node.js/23.6.0/lib/node_modules/ts-node/src/index.ts:859:12)
    at reportTSError (/opt/node.js/23.6.0/lib/node_modules/ts-node/src/index.ts:863:19)
    at getOutput (/opt/node.js/23.6.0/lib/node_modules/ts-node/src/index.ts:1077:36)
    at Object.compile (/opt/node.js/23.6.0/lib/node_modules/ts-node/src/index.ts:1433:41)
    at Module.m._compile (/opt/node.js/23.6.0/lib/node_modules/ts-node/src/index.ts:1617:30)
    at loadTS (node:internal/modules/cjs/loader:1831:10)
    at Object.require.extensions.<computed> [as .ts] (/opt/node.js/23.6.0/lib/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1473:32)
    at Function._load (node:internal/modules/cjs/loader:1285:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14) {
  diagnosticCodes: [ 2307, 2307, 2307, 2307, 7006 ]
}

I've tried to search what can be the problem and got tips to install typescript and link it locally and also to install tslib and @types/node; all to no avail.

How can i use this module?

Other details

No response

Checklist

  • I am running the latest version.
  • I checked the documentation and found no answer.
  • I have searched the existing issues and made sure this is not a duplicate.
  • I have provided sufficient information.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions