Compare commits

...

22 Commits

Author SHA1 Message Date
github-actions[bot]
b7cacc34f3 chore(main): release 8.2.0 (#567)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2024-01-08 20:46:30 -03:00
Brahim Hadriche
8f07e49512 fix(Parser): Add SortFilterHeader (#563)
* Fix for SortFilterHeader

* fix(Settings): Use `YTNode#is` to identify headers with a title

---------

Co-authored-by: LuanRT <luan.lrt4@gmail.com>
2024-01-08 20:37:06 -03:00
Luan
abd8a82cd0 chore(docs): Update auth documentation and examples (#568)
* chore(docs): Update auth documentation and examples

* chore(docs): Minor rewording

* chore(docs): Fix library version in the OAuth2 example
2024-01-08 20:16:16 -03:00
Luan
7ffd0fc25e feat(OAuth): Allow passing custom client identity (#566) 2024-01-08 20:03:01 -03:00
github-actions[bot]
b50408fc1c chore(main): release 8.1.0 (#548)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2023-12-26 23:24:27 -03:00
Brahim Hadriche
9618f38fe1 fear(parser): Add DecoratedAvatarView (#544)
* Add DecoratedAvatarView

* Export the class

* Update PageHeaderView

* Adjust thumbnails

* Add avatar view

* Apply suggestions from code review

---------

Co-authored-by: absidue <48293849+absidue@users.noreply.github.com>
2023-12-26 23:21:37 -03:00
LuanRT
e7efec2cf4 Merge branch 'main' of https://github.com/LuanRT/YouTube.js 2023-12-26 23:17:19 -03:00
LuanRT
82d5d1e3e1 chore: Fix import formatting in multiple files 2023-12-26 23:16:45 -03:00
LuanRT
9c503f4fa8 fix(VideoInfo): Restore like, dislike & removeRating methods 2023-12-26 23:15:31 -03:00
RenautMestdagh
4dd977e375 Update interaction-manager.md (#562) 2023-12-26 21:36:55 -03:00
Daniel Wykerd
e4f2a00c84 feat(generator): add support for arrays (#556)
* feat(generator): add support for arrays

* fix(parser): add overload for non array validTypes

Add Parser#parse overload to support non array validTypes.

Fixes issue in generator generating invalid Parser#parse calls
introduced in #551.
2023-12-21 19:02:44 -03:00
absidue
fcd3044982 feat(parser): Support new like and dislike nodes (#557) 2023-12-21 19:02:19 -03:00
Brahim Hadriche
14578ac96a feat(YouTube): Add FEchannels feed (#560) 2023-12-21 19:00:31 -03:00
absidue
5c83e999df fix(Format): Extract correct audio language from captions (#553) 2023-12-07 08:46:05 -03:00
LuanRT
4e67240ff9 chore(FeedNudge): Add Text import 2023-12-04 15:51:09 -03:00
absidue
f938c34ee8 feat(generator): Add support for generating view models (#550) 2023-12-04 15:46:09 -03:00
absidue
bd487f8bef fix(generator): Output Parser.parseItem() calls with one valid type, without the array (#551) 2023-12-04 15:45:38 -03:00
absidue
48a5d4e7c3 feat(Thumbnail): Support sources in Thumbnail.fromResponse (#552) 2023-12-04 13:50:08 -03:00
absidue
37ae55a7c3 chore(protobuf): Commit generated files missing from #512 (#549)
Co-authored-by: Konstantin <duell10111@t-online.de>
2023-12-02 11:35:05 -03:00
LuanRT
923232de07 chore(PlayerConfig): Add default value to some fields 2023-12-01 17:56:25 -03:00
LuanRT
a1c3ef8fbb Merge branch 'main' of https://github.com/LuanRT/YouTube.js 2023-12-01 17:15:07 -03:00
LuanRT
5c9c231cc2 feat(MediaInfo): Parse player config 2023-12-01 17:14:36 -03:00
46 changed files with 1162 additions and 133 deletions

View File

@@ -1,5 +1,36 @@
# Changelog
## [8.2.0](https://github.com/LuanRT/YouTube.js/compare/v8.1.0...v8.2.0) (2024-01-08)
### Features
* **OAuth:** Allow passing custom client identity ([#566](https://github.com/LuanRT/YouTube.js/issues/566)) ([7ffd0fc](https://github.com/LuanRT/YouTube.js/commit/7ffd0fc25edef99a938e7986b1c74af05b8f954e))
### Bug Fixes
* **Parser:** Add `SortFilterHeader` ([#563](https://github.com/LuanRT/YouTube.js/issues/563)) ([8f07e49](https://github.com/LuanRT/YouTube.js/commit/8f07e49512c59eb72debc80a9d9623ca62330858))
## [8.1.0](https://github.com/LuanRT/YouTube.js/compare/v8.0.0...v8.1.0) (2023-12-27)
### Features
* **generator:** add support for arrays ([#556](https://github.com/LuanRT/YouTube.js/issues/556)) ([e4f2a00](https://github.com/LuanRT/YouTube.js/commit/e4f2a00c843fe453cc7904f79e35597cc6e2e619))
* **generator:** Add support for generating view models ([#550](https://github.com/LuanRT/YouTube.js/issues/550)) ([f938c34](https://github.com/LuanRT/YouTube.js/commit/f938c34ee81186774096b3d24d06250211ce2851))
* **MediaInfo:** Parse player config ([5c9c231](https://github.com/LuanRT/YouTube.js/commit/5c9c231cc2f17c49da03daa8262043b985320e9a))
* **parser:** Support new like and dislike nodes ([#557](https://github.com/LuanRT/YouTube.js/issues/557)) ([fcd3044](https://github.com/LuanRT/YouTube.js/commit/fcd30449821763e9b5b57718dd02eff15d964d2b))
* **Thumbnail:** Support `sources` in `Thumbnail.fromResponse` ([#552](https://github.com/LuanRT/YouTube.js/issues/552)) ([48a5d4e](https://github.com/LuanRT/YouTube.js/commit/48a5d4e7c37b76f8980f9b68e8815aef7a6d91ab))
* **YouTube:** Add FEchannels feed ([#560](https://github.com/LuanRT/YouTube.js/issues/560)) ([14578ac](https://github.com/LuanRT/YouTube.js/commit/14578ac96af4b8bee652cce87d043173de964113))
### Bug Fixes
* **Format:** Extract correct audio language from captions ([#553](https://github.com/LuanRT/YouTube.js/issues/553)) ([5c83e99](https://github.com/LuanRT/YouTube.js/commit/5c83e999dfa00386d18369f42aa9aa10123ba578))
* **generator:** Output Parser.parseItem() calls with one valid type, without the array ([#551](https://github.com/LuanRT/YouTube.js/issues/551)) ([bd487f8](https://github.com/LuanRT/YouTube.js/commit/bd487f8befe7f62022c61ff3aae7f487104e81eb))
* **VideoInfo:** Restore `like`, `dislike` & `removeRating` methods ([9c503f4](https://github.com/LuanRT/YouTube.js/commit/9c503f4fa8a750558cedbeca974faf36e304147e))
## [8.0.0](https://github.com/LuanRT/YouTube.js/compare/v7.0.0...v8.0.0) (2023-12-01)

View File

@@ -37,7 +37,7 @@ Dislikes given video.
| video_id | `string` | Video id |
<a name="removerating"></a>
### removeLike(video_id)
### removeRating(video_id)
Remover like/dislike.
@@ -105,4 +105,4 @@ Only works with channels you are subscribed to.
| Param | Type | Description |
| --- | --- | --- |
| channel_id | `string` | Channel id |
| type | `string` | `PERSONALIZED`, `ALL` or `NONE` |
| type | `string` | `PERSONALIZED`, `ALL` or `NONE` |

View File

@@ -1,8 +1,11 @@
# Authentication via OAuth
# OAuth2
## Usage
## Custom OAuth2 Credentials
Just like the official Data API, YouTube.js supports using your own OAuth2 credentials. A working example can be found [here](https://github.com/LuanRT/YouTube.js/blob/main/examples/auth/custom-oauth2-creds).
Before using any methods which require authentication, you have to authenticate the session:
## YouTube TV OAuth2
The library supports signing in using YouTube TV's client id. This is the recommended way to sign in as it doesn't require you to create your own OAuth2 credentials.
```js
// 'auth-pending' is fired with the info needed to sign in via OAuth.
@@ -25,9 +28,11 @@ yt.session.on('update-credentials', ({ credentials }) => {
await yt.session.signIn(/* credentials */);
```
### Cache Credentials
A working example can be found [here](https://github.com/LuanRT/YouTube.js/blob/main/examples/auth/yttv-oauth2.js).
If you don't wish to sign in every time you start the session, you can cache the credentials. Note that this SHOULD NOT be used in production, save your credentials in a database/file instead and pass them to `Session#signIn(creds?)` when signing in.
## Cache Credentials
If you don't want to start the sign in flow every time you initialize the session, you can cache the credentials. Note that this SHOULD NOT be used in production, save your credentials in a database/file instead and pass them to `Session#signIn(creds?)` when signing in.
```js
// If you use this, the next call to signIn won't fire 'auth-pending' instead just 'auth'
@@ -36,9 +41,9 @@ await yt.session.oauth.cacheCredentials();
**Note:** When using cached credentials, you are still required to make a call to `Session#signIn()`.
### Sign Out
## Sign Out
The sign out method may be used to sign out of the current session. This should also remove the cached credentials.
The sign out method may be used to sign out of the current session. This removes and revokes the credentials.
```js
await yt.session.signOut();
@@ -47,3 +52,14 @@ await yt.session.signOut();
// and only want to delete the cached credentials, use:
await yt.session.oauth.removeCache();
```
# Cookies
> **Note**
> This is not as reliable as OAuth2 as cookies expire and can be completely revoked at any time.
```js
const yt = await Innertube.create({
cookie: '...'
});
```

View File

@@ -0,0 +1,141 @@
import express from 'express';
import { Innertube, UniversalCache, YTNodes } from 'youtubei.js';
import { OAuth2Client } from 'google-auth-library';
const app = express();
let innertube: Innertube | undefined;
let oAuth2Client: OAuth2Client | undefined;
/**
* To get your own client id and secret, visit https://console.developers.google.com/, create a new project,
* and create an OAuth 2.0 Client ID (Web application) under the Credentials tab.
*
* Don't forget to add http://localhost:3000/login as an authorized redirect URI.
*/
const clientId = 'YOUR_OAUTH2_CLIENT_ID';
const clientSecret = 'YOUR_OAUTH2_CLIENT_SECRET';
const redirectUri = 'http://localhost:3000/login';
const port = 3000;
let authorizationUrl: string | undefined;
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true, limit: '3mb' }))
const cache = new UniversalCache(true);
console.info("Cache dir:", cache.cache_dir);
app.get('/', async (_req, res) => {
if (!innertube) {
console.info('Creating innertube instance.');
innertube = await Innertube.create({ cache });
innertube.session.on("update-credentials", async (_credentials) => {
console.info('Credentials updated.');
await innertube?.session.oauth.cacheCredentials();
});
}
if (await cache.get('youtubei_oauth_credentials')) {
await innertube.session.signIn();
}
if (innertube.session.logged_in) {
console.info('Innertube instance is logged in.');
const userInfo = await innertube.account.getInfo();
const library = await innertube.getLibrary();
const html = `
<p>Hello ${userInfo.contents?.contents.first().account_name.text}! You have ${userInfo.contents?.contents.first().account_byline.text} on your YouTube channel.</p>
<p>Email: ${userInfo.contents?.contents.first().endpoint.payload.directSigninUserProfile.email}</p>
<p>Obfuscated Gaia ID: ${userInfo.contents?.contents.first().endpoint.payload.directSigninIdentity.effectiveObfuscatedGaiaId}</p>
<p>Channel URL: <a href="https://www.youtube.com/channel/${userInfo.footers?.endpoint.payload.browseId}">https://www.youtube.com/channel/${userInfo.footers?.endpoint.payload.browseId}</a></p>
<p>Profile Picture:</p>
<img src="${userInfo.contents?.contents.first().account_photo[0].url}" />
<p>Recently watched videos:</p>
<ul>
${library.videos.map((video) => `<li><a href="${video.as(YTNodes.GridVideo).endpoint.toURL()}">${video.title.toString()}</a> by ${video.as(YTNodes.GridVideo).author.name.toString()} - ${video.as(YTNodes.GridVideo).duration?.text}</li>`).join('')}
</ul>
<button onclick="window.location.href = '/logout'">Logout</button>
`;
return res.send(html);
}
if (!oAuth2Client) {
console.info('Creating OAuth2 client.');
oAuth2Client = new OAuth2Client(
clientId,
clientSecret,
redirectUri
);
authorizationUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: [
"http://gdata.youtube.com",
"https://www.googleapis.com/auth/youtube-paid-content"
],
include_granted_scopes: true,
prompt: 'consent',
});
console.info('Redirecting to authorization URL...');
res.redirect(authorizationUrl);
} else if (authorizationUrl) {
console.info('OAuth2 client already exists. Redirecting to authorization URL...');
res.redirect(authorizationUrl);
}
});
app.get('/login', async (req, res) => {
const { code } = req.query;
if (!code) {
return res.send('No code provided.');
}
if (!oAuth2Client || !innertube) {
return res.send('OAuth2 client or innertube instance is not initialized.');
}
const { tokens } = await oAuth2Client.getToken(code as string);
if (tokens.access_token && tokens.refresh_token && tokens.expiry_date) {
await innertube.session.signIn({
access_token: tokens.access_token,
refresh_token: tokens.refresh_token,
expires: new Date(tokens.expiry_date),
client_id: clientId,
client_secret: clientSecret,
});
await innertube.session.oauth.cacheCredentials();
console.log('Logged in successfully. Redirecting to home page...');
res.redirect('/');
}
});
app.get('/logout', async (_req, res) => {
if (!innertube) {
return res.send('Innertube instance is not initialized.');
}
await innertube.session.signOut();
console.log('Logged out successfully. Redirecting to home page...');
res.redirect('/');
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});

View File

@@ -0,0 +1,20 @@
{
"name": "yt-oauth-example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"google-auth-library": "^9.4.1",
"youtubei.js": "^8.1.0"
},
"devDependencies": {
"@types/express": "^4.17.21"
}
}

View File

@@ -0,0 +1,109 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
/* Projects */
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
// "outDir": "./", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "youtubei.js",
"version": "8.0.0",
"version": "8.2.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "youtubei.js",
"version": "8.0.0",
"version": "8.2.0",
"funding": [
"https://github.com/sponsors/LuanRT"
],

View File

@@ -1,6 +1,6 @@
{
"name": "youtubei.js",
"version": "8.0.0",
"version": "8.2.0",
"description": "A wrapper around YouTube's private API. Supports YouTube, YouTube Music, YouTube Kids and YouTube Studio (WIP).",
"type": "module",
"types": "./dist/src/platform/lib.d.ts",

View File

@@ -280,6 +280,16 @@ export default class Innertube {
return new Feed(this.actions, response);
}
/**
* Retrieves channels feed.
*/
async getChannelsFeed(): Promise<Feed<IBrowseResponse>> {
const response = await this.actions.execute(
BrowseEndpoint.PATH, { ...BrowseEndpoint.build({ browse_id: 'FEchannels' }), parse: true }
);
return new Feed(this.actions, response);
}
/**
* Retrieves contents for a given channel.
* @param id - Channel id

View File

@@ -167,6 +167,7 @@ export default class Actions {
'FElibrary',
'FEhistory',
'FEsubscriptions',
'FEchannels',
'FEmusic_listening_review',
'FEmusic_library_landing',
'SPaccount_overview',

View File

@@ -2,6 +2,9 @@ import * as Constants from '../utils/Constants.js';
import { OAuthError, Platform } from '../utils/Utils.js';
import type Session from './Session.js';
/**
* Represents the credentials used for authentication.
*/
export interface Credentials {
/**
* Token used to sign in.
@@ -15,6 +18,14 @@ export interface Credentials {
* Access token's expiration date, which is usually 24hrs-ish.
*/
expires: Date;
/**
* Optional client ID.
*/
client_id?: string;
/**
* Optional client secret.
*/
client_secret?: string;
}
// TODO: actual type info for this.
@@ -28,6 +39,11 @@ export type OAuthAuthEventHandler = (data: {
export type OAuthAuthPendingEventHandler = (data: OAuthAuthPendingData) => any;
export type OAuthAuthErrorEventHandler = (err: OAuthError) => any;
export type OAuthClientIdentity = {
client_id: string;
client_secret: string;
};
export default class OAuth {
#identity?: Record<string, string>;
#session: Session;
@@ -71,6 +87,8 @@ export default class OAuth {
this.#credentials = {
access_token: credentials.access_token,
refresh_token: credentials.refresh_token,
client_id: credentials.client_id,
client_secret: credentials.client_secret,
expires: new Date(credentials.expires)
};
@@ -157,6 +175,8 @@ export default class OAuth {
this.#credentials = {
access_token: response_data.access_token,
refresh_token: response_data.refresh_token,
client_id: this.#identity?.client_id,
client_secret: this.#identity?.client_secret,
expires: expiration_date
};
@@ -206,6 +226,8 @@ export default class OAuth {
this.#credentials = {
access_token: response_data.access_token,
refresh_token: response_data.refresh_token || this.#credentials.refresh_token,
client_id: this.#identity.client_id,
client_secret: this.#identity.client_secret,
expires: expiration_date
};
@@ -226,7 +248,14 @@ export default class OAuth {
/**
* Retrieves client identity from YouTube TV.
*/
async #getClientIdentity(): Promise<{ [key: string]: string; }> {
async #getClientIdentity(): Promise<OAuthClientIdentity> {
if (this.#credentials?.client_id && this.credentials?.client_secret) {
return {
client_id: this.#credentials.client_id,
client_secret: this.credentials.client_secret
};
}
const response = await this.#session.http.fetch_function(new URL('/tv', Constants.URLS.YT_BASE), { headers: Constants.OAUTH.HEADERS });
const response_data = await response.text();
@@ -241,7 +270,7 @@ export default class OAuth {
.replace(/\n/g, '')
.match(Constants.OAUTH.REGEX.CLIENT_IDENTITY);
const groups = client_identity?.groups;
const groups = client_identity?.groups as OAuthClientIdentity | null;
if (!groups)
throw new OAuthError('Could not obtain client identity.', { status: 'FAILED' });

View File

@@ -5,7 +5,7 @@ import type { DownloadOptions, FormatFilter, FormatOptions, URLTransformer } fro
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 type { INextResponse, IPlayerConfig, 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';
@@ -20,6 +20,7 @@ export default class MediaInfo {
#playback_tracking;
streaming_data;
playability_status;
player_config: IPlayerConfig;
constructor(data: [ApiResponse, ApiResponse?], actions: Actions, cpn: string) {
this.#actions = actions;
@@ -35,6 +36,7 @@ export default class MediaInfo {
this.streaming_data = info.streaming_data;
this.playability_status = info.playability_status;
this.player_config = info.player_config;
this.#playback_tracking = info.playback_tracking;
}

View File

@@ -0,0 +1,30 @@
import { YTNode } from '../helpers.js';
import { type RawNode } from '../index.js';
import { Thumbnail } from '../misc.js';
export default class AvatarView extends YTNode {
static type = 'AvatarView';
image: {
sources: Thumbnail[],
processor: {
border_image_processor: {
circular: boolean
}
}
};
avatar_image_size: string;
constructor(data: RawNode) {
super();
this.image = {
sources: data.image.sources.map((x: any) => new Thumbnail(x)).sort((a: Thumbnail, b: Thumbnail) => b.width - a.width),
processor: {
border_image_processor: {
circular: data.image.processor.borderImageProcessor.circular
}
}
};
this.avatar_image_size = data.avatarImageSize;
}
}

View File

@@ -15,6 +15,6 @@ export default class ChannelExternalLinkView extends YTNode {
this.title = Text.fromAttributed(data.title);
this.link = Text.fromAttributed(data.link);
this.favicon = data.favicon.sources.map((x: any) => new Thumbnail(x)).sort((a: Thumbnail, b: Thumbnail) => b.width - a.width);
this.favicon = Thumbnail.fromResponse(data.favicon);
}
}

View File

@@ -10,7 +10,7 @@ export default class ContentPreviewImageView extends YTNode {
constructor(data: RawNode) {
super();
this.image = data.image.sources.map((x: any) => new Thumbnail(x)).sort((a: Thumbnail, b: Thumbnail) => b.width - a.width);
this.image = Thumbnail.fromResponse(data.image);
this.style = data.style;
}
}

View File

@@ -0,0 +1,19 @@
import { YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';
import NavigationEndpoint from './NavigationEndpoint.js';
import AvatarView from './AvatarView.js';
export default class DecoratedAvatarView extends YTNode {
static type = 'DecoratedAvatarView';
avatar: AvatarView;
a11y_label: string;
on_tap_endpoint: NavigationEndpoint;
constructor(data: RawNode) {
super();
this.avatar = new AvatarView(data.avatar.avatarViewModel);
this.a11y_label = data.a11yLabel;
this.on_tap_endpoint = new NavigationEndpoint(data.rendererContext.commandContext.onTap.innertubeCommand);
}
}

View File

@@ -0,0 +1,16 @@
import { YTNode } from '../helpers.js';
import { Parser, type RawNode } from '../index.js';
import ToggleButtonView from './ToggleButtonView.js';
export default class DislikeButtonView extends YTNode {
static type = 'DislikeButtonView';
toggle_button: ToggleButtonView | null;
dislike_entity_key: string;
constructor(data: RawNode) {
super();
this.toggle_button = Parser.parseItem(data.toggleButtonViewModel, ToggleButtonView);
this.dislike_entity_key = data.dislikeEntityKey;
}
}

View File

@@ -1,5 +1,6 @@
import { YTNode } from '../helpers.js';
import { NavigationEndpoint } from '../nodes.js';
import NavigationEndpoint from './NavigationEndpoint.js';
import Text from './misc/Text.js';
import type { RawNode } from '../types/index.js';

View File

@@ -1,4 +1,5 @@
import NavigationEndpoint from './NavigationEndpoint.js';
import Thumbnail from './misc/Thumbnail.js';
import { YTNode, observe } from '../helpers.js';
import { type RawNode } from '../index.js';
@@ -6,11 +7,7 @@ export class Panel extends YTNode {
static type = 'Panel';
thumbnail?: {
image: {
url: string;
width: number;
height: number;
}[];
image: Thumbnail[];
endpoint: NavigationEndpoint;
on_long_press_endpoint: NavigationEndpoint;
content_mode: string;
@@ -18,16 +15,8 @@ export class Panel extends YTNode {
};
background_image: {
image: {
url: string;
width: number;
height: number;
}[];
gradient_image: {
url: string;
width: number;
height: number;
}[];
image: Thumbnail[];
gradient_image: Thumbnail[];
};
strapline: string;
@@ -48,7 +37,7 @@ export class Panel extends YTNode {
if (data.thumbnail) {
this.thumbnail = {
image: data.thumbnail.image.sources,
image: Thumbnail.fromResponse(data.thumbnail.image),
endpoint: new NavigationEndpoint(data.thumbnail.onTap),
on_long_press_endpoint: new NavigationEndpoint(data.thumbnail.onLongPress),
content_mode: data.thumbnail.contentMode,
@@ -57,8 +46,8 @@ export class Panel extends YTNode {
}
this.background_image = {
image: data.backgroundImage.image.sources,
gradient_image: data.backgroundImage.gradientImage.sources
image: Thumbnail.fromResponse(data.backgroundImage.image),
gradient_image: Thumbnail.fromResponse(data.backgroundImage.gradientImage)
};
this.strapline = data.strapline;

View File

@@ -3,18 +3,19 @@ import { Parser, type RawNode } from '../index.js';
import ItemSectionHeader from './ItemSectionHeader.js';
import ItemSectionTabbedHeader from './ItemSectionTabbedHeader.js';
import CommentsHeader from './comments/CommentsHeader.js';
import SortFilterHeader from './SortFilterHeader.js';
export default class ItemSection extends YTNode {
static type = 'ItemSection';
header: CommentsHeader | ItemSectionHeader | ItemSectionTabbedHeader | null;
header: CommentsHeader | ItemSectionHeader | ItemSectionTabbedHeader | SortFilterHeader | null;
contents: ObservedArray<YTNode>;
target_id?: string;
continuation?: string;
constructor(data: RawNode) {
super();
this.header = Parser.parseItem(data.header, [ CommentsHeader, ItemSectionHeader, ItemSectionTabbedHeader ]);
this.header = Parser.parseItem(data.header, [ CommentsHeader, ItemSectionHeader, ItemSectionTabbedHeader, SortFilterHeader ]);
this.contents = Parser.parseArray(data.contents);
if (data.targetId || data.sectionIdentifier) {

View File

@@ -0,0 +1,24 @@
import { YTNode } from '../helpers.js';
import { Parser, type RawNode } from '../index.js';
import ToggleButtonView from './ToggleButtonView.js';
export default class LikeButtonView extends YTNode {
static type = 'LikeButtonView';
toggle_button: ToggleButtonView | null;
like_status_entity_key: string;
like_status_entity: {
key: string,
like_status: string
};
constructor(data: RawNode) {
super();
this.toggle_button = Parser.parseItem(data.toggleButtonViewModel, ToggleButtonView);
this.like_status_entity_key = data.likeStatusEntityKey;
this.like_status_entity = {
key: data.likeStatusEntity.key,
like_status: data.likeStatusEntity.likeStatus
};
}
}

View File

@@ -1,4 +1,5 @@
import NavigationEndpoint from './NavigationEndpoint.js';
import Thumbnail from './misc/Thumbnail.js';
import { YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';
@@ -21,11 +22,7 @@ class ActionButton {
class Panel {
static type = 'Panel';
image: {
url: string;
width: number;
height: number;
}[];
image: Thumbnail[];
content_mode: string;
crop_options: string;
@@ -34,7 +31,7 @@ class Panel {
action_buttons: ActionButton[];
constructor (data: RawNode) {
this.image = data.image.image.sources;
this.image = Thumbnail.fromResponse(data.image.image);
this.content_mode = data.image.contentMode;
this.crop_options = data.image.cropOptions;
this.image_aspect_ratio = data.imageAspectRatio;

View File

@@ -2,6 +2,7 @@ import { YTNode } from '../helpers.js';
import { Parser, type RawNode } from '../index.js';
import ContentMetadataView from './ContentMetadataView.js';
import ContentPreviewImageView from './ContentPreviewImageView.js';
import DecoratedAvatarView from './DecoratedAvatarView.js';
import DynamicTextView from './DynamicTextView.js';
import FlexibleActionsView from './FlexibleActionsView.js';
@@ -9,14 +10,14 @@ export default class PageHeaderView extends YTNode {
static type = 'PageHeaderView';
title: DynamicTextView | null;
image: ContentPreviewImageView | null;
image: ContentPreviewImageView | DecoratedAvatarView | null;
metadata: ContentMetadataView | null;
actions: FlexibleActionsView | null;
constructor(data: RawNode) {
super();
this.title = Parser.parseItem(data.title, DynamicTextView);
this.image = Parser.parseItem(data.image, ContentPreviewImageView);
this.image = Parser.parseItem(data.image, [ ContentPreviewImageView, DecoratedAvatarView ]);
this.metadata = Parser.parseItem(data.metadata, ContentMetadataView);
this.actions = Parser.parseItem(data.actions, FlexibleActionsView);
}

View File

@@ -1,4 +1,4 @@
import type { ObservedArray} from '../helpers.js';
import type { ObservedArray } from '../helpers.js';
import { YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';
import { Parser } from '../index.js';

View File

@@ -0,0 +1,56 @@
import { YTNode } from '../helpers.js';
import { Parser, type RawNode } from '../index.js';
import LikeButtonView from './LikeButtonView.js';
import DislikeButtonView from './DislikeButtonView.js';
export default class SegmentedLikeDislikeButtonView extends YTNode {
static type = 'SegmentedLikeDislikeButtonView';
like_button: LikeButtonView | null;
dislike_button: DislikeButtonView | null;
icon_type: string;
like_count_entity: {
key: string
};
dynamic_like_count_update_data: {
update_status_key: string,
placeholder_like_count_values_key: string,
update_delay_loop_id: string,
update_delay_sec: number
};
like_count?: number;
short_like_count?: string;
constructor(data: RawNode) {
super();
this.like_button = Parser.parseItem(data.likeButtonViewModel, LikeButtonView);
this.dislike_button = Parser.parseItem(data.dislikeButtonViewModel, DislikeButtonView);
this.icon_type = data.iconType;
if (this.like_button && this.like_button.toggle_button) {
const toggle_button = this.like_button.toggle_button;
if (toggle_button.default_button) {
this.short_like_count = toggle_button.default_button.title;
this.like_count = parseInt(toggle_button.default_button.accessibility_text.replace(/\D/g, ''));
} else if (toggle_button.toggled_button) {
this.short_like_count = toggle_button.toggled_button.title;
this.like_count = parseInt(toggle_button.toggled_button.accessibility_text.replace(/\D/g, ''));
}
}
this.like_count_entity = {
key: data.likeCountEntity.key
};
this.dynamic_like_count_update_data = {
update_status_key: data.dynamicLikeCountUpdateData.updateStatusKey,
placeholder_like_count_values_key: data.dynamicLikeCountUpdateData.placeholderLikeCountValuesKey,
update_delay_loop_id: data.dynamicLikeCountUpdateData.updateDelayLoopId,
update_delay_sec: data.dynamicLikeCountUpdateData.updateDelaySec
};
}
}

View File

@@ -0,0 +1,13 @@
import { YTNode } from '../helpers.js';
import { Parser, YTNodes, type RawNode } from '../index.js';
export default class SortFilterHeader extends YTNode {
static type = 'SortFilterHeader';
filter_menu: YTNodes.SortFilterSubMenu | null;
constructor(data: RawNode) {
super();
this.filter_menu = Parser.parseItem(data.filterMenu, YTNodes.SortFilterSubMenu);
}
}

View File

@@ -0,0 +1,20 @@
import { YTNode } from '../helpers.js';
import { Parser, type RawNode } from '../index.js';
import ButtonView from './ButtonView.js';
export default class ToggleButtonView extends YTNode {
static type = 'ToggleButtonView';
default_button: ButtonView | null;
toggled_button: ButtonView | null;
identifier?: string;
is_toggling_disabled: boolean;
constructor(data: RawNode) {
super();
this.default_button = Parser.parseItem(data.defaultButtonViewModel, ButtonView);
this.toggled_button = Parser.parseItem(data.toggledButtonViewModel, ButtonView);
this.identifier = data.identifier;
this.is_toggling_disabled = data.isTogglingDisabled;
}
}

View File

@@ -10,9 +10,7 @@ import Thumbnail from './misc/Thumbnail.js';
export default class VideoAttributeView extends YTNode {
static type = 'VideoAttributeView';
image: ContentPreviewImageView | {
sources: Thumbnail[];
} | null;
image: ContentPreviewImageView | Thumbnail[] | null;
image_style: string;
title: string;
subtitle: string;
@@ -26,11 +24,9 @@ export default class VideoAttributeView extends YTNode {
constructor(data: RawNode) {
super();
// @NOTE: "image" is not a renderer so not sure why we're parsing it as one. Leaving this hack here for now to avoid breaking things.
if (data.image?.sources) {
this.image = {
sources: data.image.sources.map((x: any) => new Thumbnail(x)).sort((a: Thumbnail, b: Thumbnail) => b.width - a.width)
};
this.image = Thumbnail.fromResponse(data.image);
} else {
this.image = Parser.parseItem(data.image, ContentPreviewImageView);
}

View File

@@ -1,4 +1,4 @@
import type { ObservedArray} from '../helpers.js';
import type { ObservedArray } from '../helpers.js';
import { YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';
import { Parser } from '../index.js';

View File

@@ -1,4 +1,4 @@
import type { ObservedArray} from '../helpers.js';
import type { ObservedArray } from '../helpers.js';
import { YTNode } from '../helpers.js';
import { Parser, type RawNode } from '../index.js';

View File

@@ -1,5 +1,5 @@
import { Parser } from '../../index.js';
import type { ObservedArray} from '../../helpers.js';
import type { ObservedArray } from '../../helpers.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';

View File

@@ -1,4 +1,4 @@
import type { ObservedArray} from '../../helpers.js';
import type { ObservedArray } from '../../helpers.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
import { Parser } from '../../index.js';

View File

@@ -1,4 +1,4 @@
import type { ObservedArray} from '../../helpers.js';
import type { ObservedArray } from '../../helpers.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
import { Parser } from '../../index.js';

View File

@@ -1,4 +1,4 @@
import type { SuperParsedResult} from '../../helpers.js';
import type { SuperParsedResult } from '../../helpers.js';
import { YTNode } from '../../helpers.js';
import type { RawNode } from '../../index.js';
import { Parser } from '../../index.js';

View File

@@ -15,7 +15,20 @@ export default class Thumbnail {
* Get thumbnails from response object.
*/
static fromResponse(data: any): Thumbnail[] {
if (!data || !data.thumbnails) return [];
return data.thumbnails.map((x: any) => new Thumbnail(x)).sort((a: Thumbnail, b: Thumbnail) => b.width - a.width);
if (!data) return [];
let thumbnail_data;
if (data.thumbnails) {
thumbnail_data = data.thumbnails;
} else if (data.sources) {
thumbnail_data = data.sources;
}
if (thumbnail_data) {
return thumbnail_data.map((x: any) => new Thumbnail(x)).sort((a: Thumbnail, b: Thumbnail) => b.width - a.width);
}
return [];
}
}

View File

@@ -30,27 +30,43 @@ export type MiscInferenceType = {
params: [string, string?],
}
export type InferenceType = {
type: 'renderer',
renderers: string[],
optional: boolean,
} | {
type: 'renderer_list',
renderers: string[],
optional: boolean,
} | MiscInferenceType | {
export interface ObjectInferenceType {
type: 'object',
keys: KeyInfo,
optional: boolean,
} | {
}
export interface RendererInferenceType {
type: 'renderer',
renderers: string[],
optional: boolean
}
export interface PrimativeInferenceType {
type: 'primative',
typeof: ('string' | 'number' | 'boolean' | 'bigint' | 'symbol' | 'undefined' | 'function')[],
optional: boolean,
} | {
type: 'unknown',
typeof: ('string' | 'number' | 'boolean' | 'bigint' | 'symbol' | 'undefined' | 'function' | 'never' | 'unknown')[],
optional: boolean,
}
export type ArrayInferenceType = {
type: 'array',
array_type: 'primitive',
items: PrimativeInferenceType,
optional: boolean,
} | {
type: 'array',
array_type: 'object',
items: ObjectInferenceType,
optional: boolean,
} | {
type: 'array',
array_type: 'renderer',
renderers: string[],
optional: boolean,
};
export type InferenceType = RendererInferenceType | MiscInferenceType | ObjectInferenceType | PrimativeInferenceType | ArrayInferenceType;
export type KeyInfo = (readonly [string, InferenceType])[];
const IGNORED_KEYS = new Set([
@@ -70,7 +86,7 @@ export function camelToSnake(str: string) {
* @returns The inferred type
*/
export function inferType(key: string, value: unknown): InferenceType {
let return_value: string | Record<string, any> | boolean | MiscInferenceType = false;
let return_value: string | Record<string, any> | false | MiscInferenceType | ArrayInferenceType = false;
if (typeof value === 'object' && value != null) {
if (return_value = isRenderer(value)) {
RENDERER_EXAMPLES[return_value] = Reflect.get(value, Reflect.ownKeys(value)[0]);
@@ -85,7 +101,8 @@ export function inferType(key: string, value: unknown): InferenceType {
RENDERER_EXAMPLES[key] = value;
}
return {
type: 'renderer_list',
type: 'array',
array_type: 'renderer',
renderers: Object.keys(return_value),
optional: false
};
@@ -93,6 +110,9 @@ export function inferType(key: string, value: unknown): InferenceType {
if (return_value = isMiscType(key, value)) {
return return_value as MiscInferenceType;
}
if (return_value = isArrayType(value)) {
return return_value as ArrayInferenceType;
}
}
const primative_type = typeof value;
if (primative_type === 'object')
@@ -116,6 +136,9 @@ export function inferType(key: string, value: unknown): InferenceType {
*/
export function isRendererList(value: unknown) {
const arr = Array.isArray(value);
if (arr && value.length === 0)
return false;
const is_list = arr && value.every((item) => isRenderer(item));
return (
is_list ?
@@ -176,12 +199,89 @@ export function isRenderer(value: unknown) {
const is_object = typeof value === 'object';
if (!is_object) return false;
const keys = Reflect.ownKeys(value as object);
if (keys.length === 1 && keys[0].toString().includes('Renderer')) {
return Parser.sanitizeClassName(keys[0].toString());
if (keys.length === 1) {
const first_key = keys[0].toString();
if (first_key.endsWith('Renderer') || first_key.endsWith('Model')) {
return Parser.sanitizeClassName(first_key);
}
}
return false;
}
/**
* Checks if the given value is an array
* @param value - The value to check
* @returns If it is an array, return the InferenceType. Otherwise, return false.
*/
export function isArrayType(value: unknown): false | ArrayInferenceType {
if (!Array.isArray(value))
return false;
// If the array is empty, we can't infer anything
if (value.length === 0)
return {
type: 'array',
array_type: 'primitive',
items: {
type: 'primative',
typeof: [ 'never' ],
optional: false
},
optional: false
};
// We'll infer the primative type of the array entries
const array_entry_types = value.map((item) => typeof item);
// We only support arrays that have the same primative type throughout
const all_same_type = array_entry_types.every((type) => type === array_entry_types[0]);
if (!all_same_type)
return {
type: 'array',
array_type: 'primitive',
items: {
type: 'primative',
typeof: [ 'unknown' ],
optional: false
},
optional: false
};
const type = array_entry_types[0];
if (type !== 'object')
return {
type: 'array',
array_type: 'primitive',
items: {
type: 'primative',
typeof: [ type ],
optional: false
},
optional: false
};
let key_type: KeyInfo = [];
for (let i = 0; i < value.length; i++) {
const current_keys = Object.entries(value[i] as object).map(([ key, value ]) => [ key, inferType(key, value) ] as const);
if (i === 0) {
key_type = current_keys;
continue;
}
key_type = mergeKeyInfo(key_type, current_keys).resolved_key_info;
}
return {
type: 'array',
array_type: 'object',
items: {
type: 'object',
keys: key_type,
optional: false
},
optional: false
};
}
function introspectKeysFirstPass(classdata: unknown): KeyInfo {
if (typeof classdata !== 'object' || classdata === null) {
throw new InnertubeError('Generator: Cannot introspect non-object', {
@@ -236,7 +336,7 @@ function introspectKeysSecondPass(key_info: KeyInfo) {
// Verify that its actually badges
const badge_key_info = key_info.find(([ key ]) => key === cannonical_badges);
const is_badges = badge_key_info ?
badge_key_info[1].type === 'renderer_list' && Reflect.has(badge_key_info[1].renderers, 'MetadataBadge') :
badge_key_info[1].type === 'array' && badge_key_info[1].array_type === 'renderer' && Reflect.has(badge_key_info[1].renderers, 'MetadataBadge') :
false;
if (is_badges && cannonical_badges) excluded_keys.add(cannonical_badges);
@@ -273,7 +373,7 @@ export function introspect(classdata: unknown) {
const key_info = introspect2(classdata);
const dependencies = new Map<string, any>();
for (const [ , value ] of key_info) {
if (value.type === 'renderer' || value.type === 'renderer_list')
if (value.type === 'renderer' || (value.type === 'array' && value.array_type === 'renderer'))
for (const renderer of value.renderers) {
const example = RENDERER_EXAMPLES[renderer];
if (example)
@@ -401,6 +501,10 @@ export function generateTypescriptClass(classname: string, key_info: KeyInfo) {
return `class ${classname} extends YTNode {\n static type = '${classname}';\n\n ${props.join('\n ')}\n\n constructor(data: RawNode) {\n ${constructor_lines.join('\n ')}\n }\n}\n`;
}
function toTypeDeclarationObject(indentation: number, keys: KeyInfo) {
return `{\n${keys.map(([ key, value ]) => `${' '.repeat((indentation + 2) * 2)}${camelToSnake(key)}${value.optional ? '?' : ''}: ${toTypeDeclaration(value, indentation + 1)}`).join(',\n')}\n${' '.repeat((indentation + 1) * 2)}}`;
}
/**
* For a given inference type, get the typescript type declaration
* @param inference_type - The inference type to get the declaration for
@@ -413,13 +517,33 @@ export function toTypeDeclaration(inference_type: InferenceType, indentation = 0
{
return `${inference_type.renderers.map((type) => `YTNodes.${type}`).join(' | ')} | null`;
}
case 'renderer_list':
case 'array':
{
return `ObservedArray<${inference_type.renderers.map((type) => `YTNodes.${type}`).join(' | ')}> | null`;
switch (inference_type.array_type) {
case 'renderer':
return `ObservedArray<${inference_type.renderers.map((type) => `YTNodes.${type}`).join(' | ')}> | null`;
case 'primitive':
{
const items_list = inference_type.items.typeof;
if (inference_type.items.optional && !items_list.includes('undefined'))
items_list.push('undefined');
const items =
items_list.length === 1 ?
`${items_list[0]}` : `(${items_list.join(' | ')})`;
return `${items}[]`;
}
case 'object':
return `${toTypeDeclarationObject(indentation, inference_type.items.keys)}[]`;
default:
throw new Error('Unreachable code reached! Switch missing case!');
}
}
case 'object':
{
return `{\n${inference_type.keys.map(([ key, value ]) => `${' '.repeat((indentation + 2) * 2)}${camelToSnake(key)}${value.optional ? '?' : ''}: ${toTypeDeclaration(value, indentation + 1)}`).join(',\n')}\n${' '.repeat((indentation + 1) * 2)}}`;
return toTypeDeclarationObject(indentation, inference_type.keys);
}
case 'misc':
switch (inference_type.misc_type) {
@@ -430,11 +554,14 @@ export function toTypeDeclaration(inference_type: InferenceType, indentation = 0
}
case 'primative':
return inference_type.typeof.join(' | ');
case 'unknown':
return '/* TODO: determine correct type */ unknown';
}
}
function toParserObject(indentation: number, keys: KeyInfo, key_path: string[], key: string) {
const new_keypath = [ ...key_path, key ];
return `{\n${keys.map(([ key, value ]) => `${' '.repeat((indentation + 2) * 2)}${camelToSnake(key)}: ${toParser(key, value, new_keypath, indentation + 1)}`).join(',\n')}\n${' '.repeat((indentation + 1) * 2)}}`;
}
/**
* Generate statements to parse a given inference type
* @param key - The key to parse
@@ -448,18 +575,32 @@ export function toParser(key: string, inference_type: InferenceType, key_path: s
switch (inference_type.type) {
case 'renderer':
{
parser = `Parser.parseItem(${key_path.join('.')}.${key}, [ ${inference_type.renderers.map((type) => `YTNodes.${type}`).join(', ')} ])`;
parser = `Parser.parseItem(${key_path.join('.')}.${key}, ${toParserValidTypes(inference_type.renderers)})`;
}
break;
case 'renderer_list':
case 'array':
{
parser = `Parser.parse(${key_path.join('.')}.${key}, true, [ ${inference_type.renderers.map((type) => `YTNodes.${type}`).join(', ')} ])`;
switch (inference_type.array_type) {
case 'renderer':
parser = `Parser.parse(${key_path.join('.')}.${key}, true, ${toParserValidTypes(inference_type.renderers)})`;
break;
case 'object':
parser = `${key_path.join('.')}.${key}.map((item: any) => (${toParserObject(indentation, inference_type.items.keys, [], 'item')}))`;
break;
case 'primitive':
parser = `${key_path.join('.')}.${key}`;
break;
default:
throw new Error('Unreachable code reached! Switch missing case!');
}
}
break;
case 'object':
{
const new_keypath = [ ...key_path, key ];
parser = `{\n${inference_type.keys.map(([ key, value ]) => `${' '.repeat((indentation + 2) * 2)}${camelToSnake(key)}: ${toParser(key, value, new_keypath, indentation + 1)}`).join(',\n')}\n${' '.repeat((indentation + 1) * 2)}}`;
parser = toParserObject(indentation, inference_type.keys, key_path, key);
}
break;
case 'misc':
@@ -482,7 +623,6 @@ export function toParser(key: string, inference_type: InferenceType, key_path: s
throw new Error('Unreachable code reached! Switch missing case!');
break;
case 'primative':
case 'unknown':
parser = `${key_path.join('.')}.${key}`;
break;
}
@@ -491,6 +631,14 @@ export function toParser(key: string, inference_type: InferenceType, key_path: s
return parser;
}
function toParserValidTypes(types: string[]) {
if (types.length === 1) {
return `YTNodes.${types[0]}`;
}
return `[ ${types.map((type) => `YTNodes.${type}`).join(', ')} ]`;
}
function accessDataFromKeyPath(root: any, key_path: string[]) {
let data = root;
for (const key of key_path)
@@ -508,6 +656,15 @@ function hasDataFromKeyPath(root: any, key_path: string[]) {
return true;
}
function parseObject(key: string, data: unknown, key_path: string[], keys: KeyInfo, should_optional: boolean) {
const obj: any = {};
const new_key_path = [ ...key_path, key ];
for (const [ key, value ] of keys) {
obj[key] = should_optional ? parse(key, value, data, new_key_path) : undefined;
}
return obj;
}
/**
* Parse a value from a given key path using the given inference type
* @param key - The key to parse
@@ -523,18 +680,26 @@ export function parse(key: string, inference_type: InferenceType, data: unknown,
{
return should_optional ? Parser.parseItem(accessDataFromKeyPath({ data }, [ ...key_path, key ]), inference_type.renderers.map((type) => Parser.getParserByName(type))) : undefined;
}
case 'renderer_list':
case 'array':
{
return should_optional ? Parser.parse(accessDataFromKeyPath({ data }, [ ...key_path, key ]), true, inference_type.renderers.map((type) => Parser.getParserByName(type))) : undefined;
switch (inference_type.array_type) {
case 'renderer':
return should_optional ? Parser.parse(accessDataFromKeyPath({ data }, [ ...key_path, key ]), true, inference_type.renderers.map((type) => Parser.getParserByName(type))) : undefined;
break;
case 'object':
return should_optional ? accessDataFromKeyPath({ data }, [ ...key_path, key ]).map((_: any, idx: number) => {
return parseObject(`${idx}`, data, [ ...key_path, key ], inference_type.items.keys, should_optional);
}) : undefined;
case 'primitive':
return should_optional ? accessDataFromKeyPath({ data }, [ ...key_path, key ]) : undefined;
}
throw new Error('Unreachable code reached! Switch missing case!');
}
case 'object':
{
const obj: any = {};
const new_key_path = [ ...key_path, key ];
for (const [ key, value ] of inference_type.keys) {
obj[key] = should_optional ? parse(key, value, data, new_key_path) : undefined;
}
return obj;
return parseObject(key, data, key_path, inference_type.keys, should_optional);
}
case 'misc':
switch (inference_type.misc_type) {
@@ -556,7 +721,6 @@ export function parse(key: string, inference_type: InferenceType, data: unknown,
}
throw new Error('Unreachable code reached! Switch missing case!');
case 'primative':
case 'unknown':
return accessDataFromKeyPath({ data }, [ ...key_path, key ]);
}
}
@@ -585,7 +749,8 @@ export function mergeKeyInfo(key_info: KeyInfo, new_key_info: KeyInfo) {
if (type.type !== new_type.type) {
// We've got a type mismatch, this is unknown, we do not resolve unions
changed_keys.set(key, {
type: 'unknown',
type: 'primative',
typeof: [ 'unknown' ],
optional: true
});
continue;
@@ -628,27 +793,128 @@ export function mergeKeyInfo(key_info: KeyInfo, new_key_info: KeyInfo) {
if (did_change) changed_keys.set(key, resolved_key);
}
break;
case 'renderer_list':
case 'array':
{
if (new_type.type !== 'renderer_list') continue;
const union_map = {
...type.renderers,
...new_type.renderers
};
const either_optional = type.optional || new_type.optional;
const resolved_key: InferenceType = {
type: 'renderer_list',
renderers: union_map,
optional: either_optional
};
const did_change = JSON.stringify({
...resolved_key,
renderers: Object.keys(resolved_key.renderers)
}) !== JSON.stringify({
...type,
renderers: Object.keys(type.renderers)
});
if (did_change) changed_keys.set(key, resolved_key);
if (new_type.type !== 'array') continue;
switch (type.array_type) {
case 'renderer':
{
if (new_type.array_type !== 'renderer') {
// Type mismatch
changed_keys.set(key, {
type: 'array',
array_type: 'primitive',
items: {
type: 'primative',
typeof: [ 'unknown' ],
optional: true
},
optional: true
});
continue;
}
const union_map = {
...type.renderers,
...new_type.renderers
};
const either_optional = type.optional || new_type.optional;
const resolved_key: InferenceType = {
type: 'array',
array_type: 'renderer',
renderers: union_map,
optional: either_optional
};
const did_change = JSON.stringify({
...resolved_key,
renderers: Object.keys(resolved_key.renderers)
}) !== JSON.stringify({
...type,
renderers: Object.keys(type.renderers)
});
if (did_change) changed_keys.set(key, resolved_key);
}
break;
case 'object':
{
if (new_type.array_type === 'primitive' && new_type.items.typeof.length == 1 && new_type.items.typeof[0] === 'never') {
// It's an empty array. We assume the type is unchanged
continue;
}
if (new_type.array_type !== 'object') {
// Type mismatch
changed_keys.set(key, {
type: 'array',
array_type: 'primitive',
items: {
type: 'primative',
typeof: [ 'unknown' ],
optional: true
},
optional: true
});
continue;
}
const { resolved_key_info } = mergeKeyInfo(type.items.keys, new_type.items.keys);
const resolved_key: InferenceType = {
type: 'array',
array_type: 'object',
items: {
type: 'object',
keys: resolved_key_info,
optional: type.items.optional || new_type.items.optional
},
optional: type.optional || new_type.optional
};
const did_change = JSON.stringify(resolved_key) !== JSON.stringify(type);
if (did_change) changed_keys.set(key, resolved_key);
}
break;
case 'primitive':
{
if (type.items.typeof.includes('never') && new_type.array_type === 'object') {
// Type is now known from previosly unknown
changed_keys.set(key, new_type);
continue;
}
if (new_type.array_type !== 'primitive') {
// Type mismatch
changed_keys.set(key, {
type: 'array',
array_type: 'primitive',
items: {
type: 'primative',
typeof: [ 'unknown' ],
optional: true
},
optional: true
});
continue;
}
const key_types = new Set([ ...new_type.items.typeof, ...type.items.typeof ]);
if (key_types.size > 1 && key_types.has('never'))
key_types.delete('never');
const resolved_key: InferenceType = {
type: 'array',
array_type: 'primitive',
items: {
type: 'primative',
typeof: Array.from(key_types),
optional: type.items.optional || new_type.items.optional
},
optional: type.optional || new_type.optional
};
const did_change = JSON.stringify(resolved_key) !== JSON.stringify(type);
if (did_change) changed_keys.set(key, resolved_key);
}
break;
default:
throw new Error('Unreachable code reached! Switch missing case!');
}
}
break;
case 'misc':
@@ -657,7 +923,8 @@ export function mergeKeyInfo(key_info: KeyInfo, new_key_info: KeyInfo) {
if (type.misc_type !== new_type.misc_type) {
// We've got a type mismatch, this is unknown, we do not resolve unions
changed_keys.set(key, {
type: 'unknown',
type: 'primative',
typeof: [ 'unknown' ],
optional: true
});
}

View File

@@ -22,6 +22,7 @@ export { default as DataModelSection } from './classes/analytics/DataModelSectio
export { default as StatRow } from './classes/analytics/StatRow.js';
export { default as AudioOnlyPlayability } from './classes/AudioOnlyPlayability.js';
export { default as AutomixPreviewVideo } from './classes/AutomixPreviewVideo.js';
export { default as AvatarView } from './classes/AvatarView.js';
export { default as BackstageImage } from './classes/BackstageImage.js';
export { default as BackstagePost } from './classes/BackstagePost.js';
export { default as BackstagePostThread } from './classes/BackstagePostThread.js';
@@ -93,9 +94,11 @@ export { default as ContinuationItem } from './classes/ContinuationItem.js';
export { default as ConversationBar } from './classes/ConversationBar.js';
export { default as CopyLink } from './classes/CopyLink.js';
export { default as CreatePlaylistDialog } from './classes/CreatePlaylistDialog.js';
export { default as DecoratedAvatarView } from './classes/DecoratedAvatarView.js';
export { default as DecoratedPlayerBar } from './classes/DecoratedPlayerBar.js';
export { default as DefaultPromoPanel } from './classes/DefaultPromoPanel.js';
export { default as DidYouMean } from './classes/DidYouMean.js';
export { default as DislikeButtonView } from './classes/DislikeButtonView.js';
export { default as DownloadButton } from './classes/DownloadButton.js';
export { default as Dropdown } from './classes/Dropdown.js';
export { default as DropdownItem } from './classes/DropdownItem.js';
@@ -158,6 +161,7 @@ export { default as ItemSectionHeader } from './classes/ItemSectionHeader.js';
export { default as ItemSectionTab } from './classes/ItemSectionTab.js';
export { default as ItemSectionTabbedHeader } from './classes/ItemSectionTabbedHeader.js';
export { default as LikeButton } from './classes/LikeButton.js';
export { default as LikeButtonView } from './classes/LikeButtonView.js';
export { default as LiveChat } from './classes/LiveChat.js';
export { default as AddBannerToLiveChatCommand } from './classes/livechat/AddBannerToLiveChatCommand.js';
export { default as AddChatItemAction } from './classes/livechat/AddChatItemAction.js';
@@ -328,6 +332,7 @@ export { default as SearchSuggestionsSection } from './classes/SearchSuggestions
export { default as SecondarySearchContainer } from './classes/SecondarySearchContainer.js';
export { default as SectionList } from './classes/SectionList.js';
export { default as SegmentedLikeDislikeButton } from './classes/SegmentedLikeDislikeButton.js';
export { default as SegmentedLikeDislikeButtonView } from './classes/SegmentedLikeDislikeButtonView.js';
export { default as SettingBoolean } from './classes/SettingBoolean.js';
export { default as SettingsCheckbox } from './classes/SettingsCheckbox.js';
export { default as SettingsOptions } from './classes/SettingsOptions.js';
@@ -346,6 +351,7 @@ export { default as SingleColumnMusicWatchNextResults } from './classes/SingleCo
export { default as SingleHeroImage } from './classes/SingleHeroImage.js';
export { default as SlimOwner } from './classes/SlimOwner.js';
export { default as SlimVideoMetadata } from './classes/SlimVideoMetadata.js';
export { default as SortFilterHeader } from './classes/SortFilterHeader.js';
export { default as SortFilterSubMenu } from './classes/SortFilterSubMenu.js';
export { default as StructuredDescriptionContent } from './classes/StructuredDescriptionContent.js';
export { default as StructuredDescriptionPlaylistLockup } from './classes/StructuredDescriptionPlaylistLockup.js';
@@ -373,6 +379,7 @@ export { default as ThumbnailOverlayToggleButton } from './classes/ThumbnailOver
export { default as TimedMarkerDecoration } from './classes/TimedMarkerDecoration.js';
export { default as TitleAndButtonListHeader } from './classes/TitleAndButtonListHeader.js';
export { default as ToggleButton } from './classes/ToggleButton.js';
export { default as ToggleButtonView } from './classes/ToggleButtonView.js';
export { default as ToggleMenuServiceItem } from './classes/ToggleMenuServiceItem.js';
export { default as Tooltip } from './classes/Tooltip.js';
export { default as TopicChannelDetails } from './classes/TopicChannelDetails.js';

View File

@@ -398,6 +398,28 @@ export function parseResponse<T extends IParsedResponse = IParsedResponse>(data:
parsed_data.streaming_data = streaming_data;
}
if (data.playerConfig) {
const player_config = {
audio_config: {
loudness_db: data.playerConfig.audioConfig?.loudnessDb,
perceptual_loudness_db: data.playerConfig.audioConfig?.perceptualLoudnessDb,
enable_per_format_loudness: data.playerConfig.audioConfig?.enablePerFormatLoudness
},
stream_selection_config: {
max_bitrate: data.playerConfig.streamSelectionConfig?.maxBitrate || '0'
},
media_common_config: {
dynamic_readahead_config: {
max_read_ahead_media_time_ms: data.playerConfig.mediaCommonConfig?.dynamicReadaheadConfig?.maxReadAheadMediaTimeMs || 0,
min_read_ahead_media_time_ms: data.playerConfig.mediaCommonConfig?.dynamicReadaheadConfig?.minReadAheadMediaTimeMs || 0,
read_ahead_growth_rate_ms: data.playerConfig.mediaCommonConfig?.dynamicReadaheadConfig?.readAheadGrowthRateMs || 0
}
}
};
parsed_data.player_config = player_config;
}
const current_video_endpoint = data.currentVideoEndpoint ? new NavigationEndpoint(data.currentVideoEndpoint) : null;
if (current_video_endpoint) {
parsed_data.current_video_endpoint = current_video_endpoint;
@@ -545,6 +567,7 @@ export function parseArray(data?: RawNode[], validTypes?: YTNodeConstructor | YT
* @param validTypes - YTNode types that are allowed to be parsed.
*/
export function parse<T extends YTNode, K extends YTNodeConstructor<T>[]>(data: RawData, requireArray: true, validTypes?: K): ObservedArray<InstanceType<K[number]>> | null;
export function parse<T extends YTNode, K extends YTNodeConstructor<T>>(data: RawData, requireArray: true, validTypes?: K): ObservedArray<InstanceType<K>> | null;
export function parse<T extends YTNode = YTNode>(data?: RawData, requireArray?: false | undefined, validTypes?: YTNodeConstructor<T> | YTNodeConstructor<T>[]): SuperParsedResult<T>;
export function parse<T extends YTNode = YTNode>(data?: RawData, requireArray?: boolean, validTypes?: YTNodeConstructor<T> | YTNodeConstructor<T>[]) {
if (!data) return null;

View File

@@ -56,6 +56,7 @@ export interface IParsedResponse {
};
playability_status?: IPlayabilityStatus;
streaming_data?: IStreamingData;
player_config?: IPlayerConfig;
current_video_endpoint?: NavigationEndpoint;
endpoint?: NavigationEndpoint;
captions?: PlayerCaptionsTracklist;
@@ -71,6 +72,24 @@ export interface IParsedResponse {
continuationEndpoint?: YTNode;
}
export interface IPlayerConfig {
audio_config: {
loudness_db?: number;
perceptual_loudness_db?: number;
enable_per_format_loudness: boolean;
};
stream_selection_config: {
max_bitrate: string;
};
media_common_config: {
dynamic_readahead_config: {
max_read_ahead_media_time_ms: number;
min_read_ahead_media_time_ms: number;
read_ahead_growth_rate_ms: number;
};
};
}
export interface IStreamingData {
expires: Date;
formats: Format[];
@@ -87,6 +106,7 @@ export interface IPlayerResponse {
annotations?: ObservedArray<PlayerAnnotationsExpanded>;
playability_status: IPlayabilityStatus;
streaming_data?: IStreamingData;
player_config: IPlayerConfig;
playback_tracking?: {
videostats_watchtime_url: string;
videostats_playback_url: string;

View File

@@ -1,6 +1,24 @@
export type RawNode = Record<string, any>;
export type RawData = RawNode | RawNode[];
export interface IRawPlayerConfig {
audioConfig: {
loudnessDb?: number;
perceptualLoudnessDb?: number;
enablePerFormatLoudness: boolean;
};
streamSelectionConfig: {
maxBitrate: string;
};
mediaCommonConfig: {
dynamicReadaheadConfig: {
maxReadAheadMediaTimeMs: number;
minReadAheadMediaTimeMs: number;
readAheadGrowthRateMs: number;
};
};
}
export interface IRawResponse {
contents?: RawData;
onResponseReceivedActions?: RawNode[];
@@ -41,6 +59,7 @@ export interface IRawResponse {
dashManifestUrl?: string;
hlsManifestUrl?: string;
};
playerConfig?: IRawPlayerConfig;
currentVideoEndpoint?: RawNode;
unseenCount?: number;
playlistId?: string;

View File

@@ -1,5 +1,5 @@
import type { IGuideResponse } from '../types/ParsedResponse.js';
import type { IRawResponse} from '../index.js';
import type { IRawResponse } from '../index.js';
import { Parser } from '../index.js';
import type { ObservedArray } from '../helpers.js';
import GuideSection from '../classes/GuideSection.js';

View File

@@ -8,9 +8,11 @@ import SectionList from '../classes/SectionList.js';
import SettingsOptions from '../classes/SettingsOptions.js';
import SettingsSidebar from '../classes/SettingsSidebar.js';
import SettingsSwitch from '../classes/SettingsSwitch.js';
import CommentsHeader from '../classes/comments/CommentsHeader.js';
import ItemSectionHeader from '../classes/ItemSectionHeader.js';
import ItemSectionTabbedHeader from '../classes/ItemSectionTabbedHeader.js';
import Tab from '../classes/Tab.js';
import TwoColumnBrowseResults from '../classes/TwoColumnBrowseResults.js';
import type Actions from '../../core/Actions.js';
import type { ApiResponse } from '../../core/Actions.js';
import type { IBrowseResponse } from '../types/ParsedResponse.js';
@@ -42,7 +44,7 @@ class Settings {
this.introduction = contents?.shift()?.contents?.firstOfType(PageIntroduction);
this.sections = contents?.map((el: ItemSection) => ({
title: el.header?.title.toString() || null,
title: el.header?.is(CommentsHeader, ItemSectionHeader, ItemSectionTabbedHeader) ? el.header.title.toString() : null,
contents: el.contents
}));
}

View File

@@ -12,13 +12,14 @@ import RelatedChipCloud from '../classes/RelatedChipCloud.js';
import RichMetadata from '../classes/RichMetadata.js';
import RichMetadataRow from '../classes/RichMetadataRow.js';
import SegmentedLikeDislikeButton from '../classes/SegmentedLikeDislikeButton.js';
import SegmentedLikeDislikeButtonView from '../classes/SegmentedLikeDislikeButtonView.js';
import ToggleButton from '../classes/ToggleButton.js';
import TwoColumnWatchNextResults from '../classes/TwoColumnWatchNextResults.js';
import VideoPrimaryInfo from '../classes/VideoPrimaryInfo.js';
import VideoSecondaryInfo from '../classes/VideoSecondaryInfo.js';
import LiveChatWrap from './LiveChat.js';
import type NavigationEndpoint from '../classes/NavigationEndpoint.js';
import NavigationEndpoint from '../classes/NavigationEndpoint.js';
import PlayerLegacyDesktopYpcTrailer from '../classes/PlayerLegacyDesktopYpcTrailer.js';
import LiveChatWrap from './LiveChat.js';
import type CardCollection from '../classes/CardCollection.js';
import type Endscreen from '../classes/Endscreen.js';
@@ -35,6 +36,7 @@ import { InnertubeError } from '../../utils/Utils.js';
import { MediaInfo } from '../../core/mixins/index.js';
import StructuredDescriptionContent from '../classes/StructuredDescriptionContent.js';
import { VideoDescriptionMusicSection } from '../nodes.js';
import type { RawNode } from '../index.js';
class VideoInfo extends MediaInfo {
#watch_next_continuation?: ContinuationItem;
@@ -105,11 +107,10 @@ class VideoInfo extends MediaInfo {
// The combined formats only exist for the default language, even for videos with multiple audio tracks
// So we can copy the language from the default audio track to the combined formats
this.streaming_data.formats.forEach((format) => format.language = default_audio_track.language);
} else if (typeof this.captions?.default_audio_track_index !== 'undefined' && this.captions?.audio_tracks && this.captions.caption_tracks) {
} else if (this.captions?.caption_tracks && this.captions?.caption_tracks.length > 0) {
// For videos with a single audio track and captions, we can use the captions to figure out the language of the audio and combined formats
const audioTrack = this.captions.audio_tracks[this.captions.default_audio_track_index];
const index = audioTrack.default_caption_track_index || 0;
const language_code = this.captions.caption_tracks[index].language_code;
const auto_generated_caption_track = this.captions.caption_tracks.find((caption) => caption.kind === 'asr');
const language_code = auto_generated_caption_track?.language_code;
this.streaming_data.adaptive_formats.forEach((format) => {
if (format.has_audio) {
@@ -164,6 +165,17 @@ class VideoInfo extends MediaInfo {
this.basic_info.is_disliked = segmented_like_dislike_button?.dislike_button?.is_toggled;
}
const segmented_like_dislike_button_view = this.primary_info?.menu?.top_level_buttons.firstOfType(SegmentedLikeDislikeButtonView);
if (segmented_like_dislike_button_view) {
this.basic_info.like_count = segmented_like_dislike_button_view.like_count;
if (segmented_like_dislike_button_view.like_button) {
const like_status = segmented_like_dislike_button_view.like_button.like_status_entity.like_status;
this.basic_info.is_liked = like_status === 'LIKE';
this.basic_info.is_disliked = like_status === 'DISLIKE';
}
}
const comments_entry_point = results.get({ target_id: 'comments-entry-point' })?.as(ItemSection);
this.comments_entry_point_header = comments_entry_point?.contents?.firstOfType(CommentsEntryPointHeader);
@@ -239,6 +251,26 @@ class VideoInfo extends MediaInfo {
* Likes the video.
*/
async like(): Promise<ApiResponse> {
const segmented_like_dislike_button_view = this.primary_info?.menu?.top_level_buttons.firstOfType(SegmentedLikeDislikeButtonView);
if (segmented_like_dislike_button_view) {
const button = segmented_like_dislike_button_view?.like_button?.toggle_button;
if (!button || !button.default_button || !segmented_like_dislike_button_view.like_button)
throw new InnertubeError('Like button not found', { video_id: this.basic_info.id });
const like_status = segmented_like_dislike_button_view.like_button.like_status_entity.like_status;
if (like_status === 'LIKE')
throw new InnertubeError('This video is already liked', { video_id: this.basic_info.id });
const endpoint = new NavigationEndpoint(button.default_button.on_tap.payload.commands.find((cmd: RawNode) => cmd.innertubeCommand));
const response = await endpoint.call(this.actions);
return response;
}
const segmented_like_dislike_button = this.primary_info?.menu?.top_level_buttons.firstOfType(SegmentedLikeDislikeButton);
const button = segmented_like_dislike_button?.like_button;
@@ -260,6 +292,26 @@ class VideoInfo extends MediaInfo {
* Dislikes the video.
*/
async dislike(): Promise<ApiResponse> {
const segmented_like_dislike_button_view = this.primary_info?.menu?.top_level_buttons.firstOfType(SegmentedLikeDislikeButtonView);
if (segmented_like_dislike_button_view) {
const button = segmented_like_dislike_button_view?.dislike_button?.toggle_button;
if (!button || !button.default_button || !segmented_like_dislike_button_view.dislike_button || !segmented_like_dislike_button_view.like_button)
throw new InnertubeError('Dislike button not found', { video_id: this.basic_info.id });
const like_status = segmented_like_dislike_button_view.like_button.like_status_entity.like_status;
if (like_status === 'DISLIKE')
throw new InnertubeError('This video is already disliked', { video_id: this.basic_info.id });
const endpoint = new NavigationEndpoint(button.default_button.on_tap.payload.commands.find((cmd: RawNode) => cmd.innertubeCommand));
const response = await endpoint.call(this.actions);
return response;
}
const segmented_like_dislike_button = this.primary_info?.menu?.top_level_buttons.firstOfType(SegmentedLikeDislikeButton);
const button = segmented_like_dislike_button?.dislike_button;
@@ -283,6 +335,34 @@ class VideoInfo extends MediaInfo {
async removeRating(): Promise<ApiResponse> {
let button;
const segmented_like_dislike_button_view = this.primary_info?.menu?.top_level_buttons.firstOfType(SegmentedLikeDislikeButtonView);
if (segmented_like_dislike_button_view) {
const toggle_button = segmented_like_dislike_button_view?.like_button?.toggle_button;
if (!toggle_button || !toggle_button.default_button || !segmented_like_dislike_button_view.like_button)
throw new InnertubeError('Like button not found', { video_id: this.basic_info.id });
const like_status = segmented_like_dislike_button_view.like_button.like_status_entity.like_status;
if (like_status === 'LIKE') {
button = segmented_like_dislike_button_view?.like_button?.toggle_button;
} else if (like_status === 'DISLIKE') {
button = segmented_like_dislike_button_view?.dislike_button?.toggle_button;
} else {
throw new InnertubeError('This video is not liked/disliked', { video_id: this.basic_info.id });
}
if (!button || !button.toggled_button)
throw new InnertubeError('Like/Dislike button not found', { video_id: this.basic_info.id });
const endpoint = new NavigationEndpoint(button.toggled_button.on_tap.payload.commands.find((cmd: RawNode) => cmd.innertubeCommand));
const response = await endpoint.call(this.actions);
return response;
}
const segmented_like_dislike_button = this.primary_info?.menu?.top_level_buttons.firstOfType(SegmentedLikeDislikeButton);
const like_button = segmented_like_dislike_button?.like_button;

View File

@@ -0,0 +1,75 @@
import {
tsValueToJsonValueFns,
jsonValueToTsValueFns,
} from "../../../runtime/json/scalar.js";
import {
WireMessage,
} from "../../../runtime/wire/index.js";
import {
default as serialize,
} from "../../../runtime/wire/serialize.js";
import {
tsValueToWireValueFns,
wireValueToTsValueFns,
} from "../../../runtime/wire/scalar.js";
import {
default as deserialize,
} from "../../../runtime/wire/deserialize.js";
export declare namespace $.youtube.ReelSequence {
export type Params = {
number: number;
}
}
export type Type = $.youtube.ReelSequence.Params;
export function getDefaultValue(): $.youtube.ReelSequence.Params {
return {
number: 0,
};
}
export function createValue(partialValue: Partial<$.youtube.ReelSequence.Params>): $.youtube.ReelSequence.Params {
return {
...getDefaultValue(),
...partialValue,
};
}
export function encodeJson(value: $.youtube.ReelSequence.Params): unknown {
const result: any = {};
if (value.number !== undefined) result.number = tsValueToJsonValueFns.int32(value.number);
return result;
}
export function decodeJson(value: any): $.youtube.ReelSequence.Params {
const result = getDefaultValue();
if (value.number !== undefined) result.number = jsonValueToTsValueFns.int32(value.number);
return result;
}
export function encodeBinary(value: $.youtube.ReelSequence.Params): Uint8Array {
const result: WireMessage = [];
if (value.number !== undefined) {
const tsValue = value.number;
result.push(
[3, tsValueToWireValueFns.int32(tsValue)],
);
}
return serialize(result);
}
export function decodeBinary(binary: Uint8Array): $.youtube.ReelSequence.Params {
const result = getDefaultValue();
const wireMessage = deserialize(binary);
const wireFields = new Map(wireMessage);
field: {
const wireValue = wireFields.get(3);
if (wireValue === undefined) break field;
const value = wireValueToTsValueFns.int32(wireValue);
if (value === undefined) break field;
result.number = value;
}
return result;
}

View File

@@ -0,0 +1 @@
export type { Type as Params } from "./Params.js";