mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-13 09:32:12 +00:00
* dev: add response types * dev: refactor `Parser#parseResponse()` * dev: update YouTube parsers * dev: update YouTube Music classes * dev: update YouTube Kids classes * dev: update core classes * dev(Parser): fix some inconsistencies * chore: update docs * chore: update docs x2 * fix: export response types * chore(docs): update parser example
26 lines
1001 B
TypeScript
26 lines
1001 B
TypeScript
import { Parser, YTNodes } from 'youtubei.js';
|
|
import { readFileSync } from 'fs';
|
|
|
|
// Artist page response from YouTube Music
|
|
const data = readFileSync('./artist.json').toString();
|
|
const page = Parser.parseResponse(JSON.parse(data));
|
|
|
|
const header = page.header?.item().as(YTNodes.MusicImmersiveHeader, YTNodes.MusicVisualHeader);
|
|
|
|
console.info('Header:', header);
|
|
|
|
// The parser encapsulates all arrays in a proxy object.
|
|
// A proxy intercepts access to the actual data, allowing
|
|
// the parser to add type safety and many utility methods
|
|
// that make working with InnerTube much easier.
|
|
const tab = page.contents?.item().as(YTNodes.SingleColumnBrowseResults).tabs.firstOfType(YTNodes.Tab);
|
|
|
|
if (!tab)
|
|
throw new Error('Target tab not found');
|
|
|
|
if (!tab.content)
|
|
throw new Error('Target tab appears to be empty');
|
|
|
|
const sections = tab.content?.as(YTNodes.SectionList).contents.as(YTNodes.MusicCarouselShelf, YTNodes.MusicDescriptionShelf, YTNodes.MusicShelf);
|
|
|
|
console.info('Sections:', sections); |