Skip to content

Commit

Permalink
refactor!: replace unnecessary classes with pure functions (#468)
Browse files Browse the repository at this point in the history
* deps: update linkedom

* refactor!: remove YTNodeGenerator in favour of namespaced pure functions

BREAKING CHANGES:
- Removes `YTNodeGenerator` from `import('youtubei.js').Generator` and exposes its functions directly in `import('youtubei.js').Generator`

* refactor!: replace Parser class with pure functions

- Remove Parser class in favour of pure functions
- Merge duplicate classes `AppendContinuationItemsAction` into a single class
- Move continuation parsers into a seperate file
- Add better custom logging support to parser methods as per issue #460

* refactor!: replace Proto class with pure functions

* chore: update package-lock.json

* refactor!: replace FormatUtils with pure functions and JSX components

- Replace linkedom DASH manifest generation with a dependency free JSX implementation
- Remove FormatUtils class in favour of pure functions
- Remove DOMParser requirement
- Remove duplicate types

* refactor: implement changes from #462

* chore: lint

* fix: deno support

* fix: render valid xml document

* fix: wrong function call in DashUtils

* fix: typo in parser

Co-authored-by: LuanRT <[email protected]>

* refactor!: move streaming info logic into seperate function

This allows users to access the same data available in the dash manifest while also simplifying the manifest generation

* chore: lint

* refactor: readability improvements & fixes

Remove redundant getAudioTrackGroups
General readability improvements in StreamingInfo.ts
Share response object between `getBitrate` and `getMimeType` as to not make duplicate requests

* build: remove unnecessary step in deno build

Co-authored-by: absidue <[email protected]>

* refactor: move types to `types` directory

* docs: add back comments lost during refactor

* chore: lint

---------

Co-authored-by: LuanRT <[email protected]>
Co-authored-by: absidue <[email protected]>
  • Loading branch information
3 people authored Aug 18, 2023
1 parent eb3cca1 commit 87ed396
Show file tree
Hide file tree
Showing 41 changed files with 3,061 additions and 7,373 deletions.
2 changes: 1 addition & 1 deletion deno.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './deno/src/platform/deno.ts';
import Innertube from './deno/src/platform/deno.ts';
export default Innertube;
export default Innertube;
5,169 changes: 203 additions & 4,966 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"build:parser-map": "node ./scripts/gen-parser-map.mjs",
"build:proto": "npx pb-gen-ts --entry-path=\"src/proto\" --out-dir=\"src/proto/generated\" --ext-in-import=\".js\"",
"build:esm": "npx tsc",
"build:deno": "npx cpy ./src ./deno && npx cpy ./package.json ./deno && npx replace \".js';\" \".ts';\" ./deno -r && npx replace '.js\";' '.ts\";' ./deno -r && npx replace \"'linkedom';\" \"'https://esm.sh/linkedom';\" ./deno -r && npx replace \"'jintr';\" \"'https://esm.sh/jintr';\" ./deno -r && npx replace \"new Jinter.default\" \"new Jinter\" ./deno -r",
"build:deno": "npx cpy ./src ./deno && npx esbuild ./src/utils/DashManifest.tsx --keep-names --format=esm --platform=neutral --target=es2020 --outfile=./deno/src/utils/DashManifest.js && npx cpy ./package.json ./deno && npx replace \".js';\" \".ts';\" ./deno -r && npx replace '.js\";' '.ts\";' ./deno -r && npx replace \"'./DashManifest.ts';\" \"'./DashManifest.js';\" ./deno -r && npx replace \"'jintr';\" \"'https://esm.sh/jintr';\" ./deno -r",
"bundle:node": "npx esbuild ./dist/src/platform/node.js --bundle --target=node10 --keep-names --format=cjs --platform=node --outfile=./bundle/node.cjs --external:jintr --external:undici --external:linkedom --external:tslib --sourcemap --banner:js=\"/* eslint-disable */\"",
"bundle:browser": "npx esbuild ./dist/src/platform/web.js --banner:js=\"/* eslint-disable */\" --bundle --target=chrome58 --keep-names --format=esm --sourcemap --define:global=globalThis --conditions=module --outfile=./bundle/browser.js --platform=browser",
"bundle:browser:prod": "npm run bundle:browser -- --outfile=./bundle/browser.min.js --minify",
Expand All @@ -86,7 +86,6 @@
"license": "MIT",
"dependencies": {
"jintr": "^1.1.0",
"linkedom": "^0.14.12",
"tslib": "^2.5.0",
"undici": "^5.19.1"
},
Expand Down
4 changes: 2 additions & 2 deletions src/Innertube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Kids, Music, Studio } from './core/clients/index.js';
import { AccountManager, InteractionManager, PlaylistManager } from './core/managers/index.js';
import { Feed, TabbedFeed } from './core/mixins/index.js';

import Proto from './proto/index.js';
import * as Proto from './proto/index.js';
import * as Constants from './utils/Constants.js';
import { InnertubeError, generateRandomString, throwIfMissing } from './utils/Utils.js';

Expand All @@ -38,7 +38,7 @@ import { GetUnseenCountEndpoint } from './core/endpoints/notification/index.js';
import type { ApiResponse } from './core/Actions.js';
import type { IBrowseResponse, IParsedResponse } from './parser/types/index.js';
import type { INextRequest } from './types/index.js';
import type { DownloadOptions, FormatOptions } from './utils/FormatUtils.js';
import type { DownloadOptions, FormatOptions } from './types/FormatUtils.js';

export type InnertubeConfig = SessionOptions;

Expand Down
2 changes: 1 addition & 1 deletion src/core/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import EventEmitterLike from '../utils/EventEmitterLike.js';
import Actions from './Actions.js';
import Player from './Player.js';

import Proto from '../proto/index.js';
import * as Proto from '../proto/index.js';
import type { ICache } from '../types/Cache.js';
import type { FetchFunction } from '../types/PlatformShim.js';
import HTTPClient from '../utils/HTTPClient.js';
Expand Down
2 changes: 1 addition & 1 deletion src/core/clients/Music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import PlaylistPanel from '../../parser/classes/PlaylistPanel.js';
import SearchSuggestionsSection from '../../parser/classes/SearchSuggestionsSection.js';
import SectionList from '../../parser/classes/SectionList.js';
import Tab from '../../parser/classes/Tab.js';
import Proto from '../../proto/index.js';
import * as Proto from '../../proto/index.js';

import type { ObservedArray, YTNode } from '../../parser/helpers.js';
import type { MusicSearchFilters } from '../../types/index.js';
Expand Down
2 changes: 1 addition & 1 deletion src/core/clients/Studio.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Proto from '../../proto/index.js';
import * as Proto from '../../proto/index.js';
import * as Constants from '../../utils/Constants.js';
import { InnertubeError, MissingParamError, Platform } from '../../utils/Utils.js';

Expand Down
2 changes: 1 addition & 1 deletion src/core/managers/AccountManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Analytics from '../../parser/youtube/Analytics.js';
import Settings from '../../parser/youtube/Settings.js';
import TimeWatched from '../../parser/youtube/TimeWatched.js';

import Proto from '../../proto/index.js';
import * as Proto from '../../proto/index.js';
import { InnertubeError } from '../../utils/Utils.js';
import { Account, BrowseEndpoint, Channel } from '../endpoints/index.js';

Expand Down
2 changes: 1 addition & 1 deletion src/core/managers/InteractionManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Proto from '../../proto/index.js';
import * as Proto from '../../proto/index.js';
import type Actions from '../Actions.js';
import type { ApiResponse } from '../Actions.js';

Expand Down
20 changes: 18 additions & 2 deletions src/core/mixins/MediaInfo.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { ApiResponse } from '../Actions.js';
import type Actions from '../Actions.js';
import * as Constants from '../../utils/Constants.js';
import type { DownloadOptions, FormatFilter, FormatOptions, URLTransformer } from '../../utils/FormatUtils.js';
import FormatUtils from '../../utils/FormatUtils.js';
import type { DownloadOptions, FormatFilter, FormatOptions, URLTransformer } from '../../types/FormatUtils.js';
import * as FormatUtils from '../../utils/FormatUtils.js';
import { InnertubeError } from '../../utils/Utils.js';
import type Format from '../../parser/classes/misc/Format.js';
import type { INextResponse, IPlayerResponse } from '../../parser/index.js';
import Parser from '../../parser/index.js';
import type { DashOptions } from '../../types/DashOptions.js';
import PlayerStoryboardSpec from '../../parser/classes/PlayerStoryboardSpec.js';
import { getStreamingInfo } from '../../utils/StreamingInfo.js';

export default class MediaInfo {
#page: [IPlayerResponse, INextResponse?];
Expand Down Expand Up @@ -52,6 +53,21 @@ export default class MediaInfo {
return FormatUtils.toDash(this.streaming_data, url_transformer, format_filter, this.#cpn, this.#actions.session.player, this.#actions, storyboards);
}

/**
* Get a cleaned up representation of the adaptive_formats
*/
getStreamingInfo(url_transformer?: URLTransformer, format_filter?: FormatFilter) {
return getStreamingInfo(
this.streaming_data,
url_transformer,
format_filter,
this.cpn,
this.#actions.session.player,
this.#actions,
this.#page[0].storyboards?.is(PlayerStoryboardSpec) ? this.#page[0].storyboards : undefined
);
}

/**
* Selects the format that best matches the given options.
* @param options - Options
Expand Down
2 changes: 1 addition & 1 deletion src/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ const example_data = {

// The first argument is the name of the class, the second is the data you have for the node.
// It will return a class that extends YTNode.
const Example = Generator.YTNodeGenerator.generateRuntimeClass('Example', example_data);
const Example = Generator.generateRuntimeClass('Example', example_data);

// You may now use this class as you would any other node.
const example = new Example(example_data);
Expand Down
2 changes: 1 addition & 1 deletion src/parser/classes/GridShow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { YTNode, type ObservedArray } from '../helpers.js';
import type { RawNode } from '../index.js';
import Parser from '../parser.js';
import * as Parser from '../parser.js';
import Author from './misc/Author.js';
import Text from './misc/Text.js';
import NavigationEndpoint from './NavigationEndpoint.js';
Expand Down
2 changes: 1 addition & 1 deletion src/parser/classes/GuideCollapsibleEntry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from '../parser.js';
import * as Parser from '../parser.js';
import GuideEntry from './GuideEntry.js';
import type { RawNode } from '../index.js';
import { type ObservedArray, YTNode } from '../helpers.js';
Expand Down
2 changes: 1 addition & 1 deletion src/parser/classes/GuideCollapsibleSectionEntry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from '../parser.js';
import * as Parser from '../parser.js';
import type { RawNode } from '../index.js';
import { type ObservedArray, YTNode } from '../helpers.js';

Expand Down
2 changes: 1 addition & 1 deletion src/parser/classes/GuideSection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Text from './misc/Text.js';
import Parser from '../parser.js';
import * as Parser from '../parser.js';
import { type ObservedArray, YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';

Expand Down
22 changes: 12 additions & 10 deletions src/parser/classes/PlayerStoryboardSpec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';

export interface StoryboardData {
template_url: string;
thumbnail_width: number;
thumbnail_height: number;
thumbnail_count: number;
interval: number;
columns: number;
rows: number;
storyboard_count: number;
}

export default class PlayerStoryboardSpec extends YTNode {
static type = 'PlayerStoryboardSpec';

boards: {
template_url: string;
thumbnail_width: number;
thumbnail_height: number;
thumbnail_count: number;
interval: number;
columns: number;
rows: number;
storyboard_count: number;
}[];
boards: StoryboardData[];

constructor(data: RawNode) {
super();
Expand Down
2 changes: 1 addition & 1 deletion src/parser/classes/SharedPost.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';
import Parser from '../parser.js';
import * as Parser from '../parser.js';
import BackstagePost from './BackstagePost.js';
import Button from './Button.js';
import Menu from './menus/Menu.js';
Expand Down
7 changes: 4 additions & 3 deletions src/parser/classes/actions/AppendContinuationItemsAction.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import Parser from '../../index.js';
import type { RawNode } from '../../index.js';
import { type SuperParsedResult, YTNode } from '../../helpers.js';
import type { ObservedArray } from '../../helpers.js';
import { YTNode } from '../../helpers.js';

export default class AppendContinuationItemsAction extends YTNode {
static type = 'AppendContinuationItemsAction';

items: SuperParsedResult<YTNode>;
contents: ObservedArray<YTNode> | null;
target: string;

constructor(data: RawNode) {
super();
this.items = Parser.parse(data.continuationItems);
this.contents = Parser.parseArray(data.continuationItems);
this.target = data.target;
}
}
2 changes: 1 addition & 1 deletion src/parser/classes/comments/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import CommentReplyDialog from './CommentReplyDialog.js';
import PdgCommentChip from './PdgCommentChip.js';
import SponsorCommentBadge from './SponsorCommentBadge.js';

import Proto from '../../../proto/index.js';
import * as Proto from '../../../proto/index.js';
import { InnertubeError } from '../../../utils/Utils.js';
import { YTNode } from '../../helpers.js';

Expand Down
Loading

0 comments on commit 87ed396

Please sign in to comment.