Compare commits

...

38 Commits

Author SHA1 Message Date
LuanRT
933ec50a1c build (package): increment version 2021-10-24 23:36:12 -03:00
LuanRT
922ab51a8d build (package): increment version 2021-10-24 23:31:19 -03:00
LuanRT
330943cbc0 docs: add examples for subscriptions feed & comments 2021-10-24 23:25:51 -03:00
LuanRT
368dfc4ea3 format: remove unused param 2021-10-24 17:07:14 -03:00
LuanRT
3a9d8a411e refactor: descramble n token algorithm instead of executing it directly for better security 2021-10-24 17:03:09 -03:00
LuanRT
47ea630329 feat: now it's possible to fetch the comments section 2021-10-23 15:48:01 -03:00
LuanRT
c8e8f34a83 refactor: change pref index order 2021-10-22 17:37:18 -03:00
LuanRT
14a5b0f0e8 chore: improve protobuf encoding 2021-10-22 17:33:47 -03:00
LuanRT
e817eb46d4 chore: remove unnecessary keyword from proto file 2021-10-22 04:42:48 -03:00
LuanRT
6ca6e22fea chore: update examples 2021-10-22 04:38:33 -03:00
LuanRT
d11d03bd92 chore: update workflow 2021-10-22 04:36:37 -03:00
LuanRT
27962242b7 dev: encode with actual an actual protobuf lib instead of hard-coding stuff 2021-10-22 04:32:51 -03:00
LuanRT
f54993e3b7 build (package): add protons dependency 2021-10-22 02:47:18 -03:00
LuanRT
d2ec5ebe9c dev: add proto file 2021-10-22 02:38:59 -03:00
LuanRT
10abf386b4 revert: put back accidentally removed code 2021-10-20 02:53:32 -03:00
LuanRT
f0360cac69 format: rephrase comments 2021-10-20 02:51:21 -03:00
LuanRT
24c11f2c06 fix: remove unnecessary chat polling call 2021-10-20 02:47:06 -03:00
LuanRT
ff2ca5ad3b format: remove unnecessary async keyword 2021-10-20 02:21:05 -03:00
LuanRT
391a4300c1 refactor: organize live chat actions 2021-10-20 02:17:13 -03:00
LuanRT
530e310da6 docs: update live chat example 2021-10-17 02:42:39 -03:00
LuanRT
73bac32886 fix: don't make additional requests if there aren't any more chunks to download 2021-10-17 02:37:35 -03:00
LuanRT
8023e74adb build (package): increment version 2021-10-15 03:32:05 -03:00
LuanRT
09ce31061d docs: add live chat examples & improve the rest of the documentation 2021-10-15 03:29:47 -03:00
LuanRT
7072485782 format: organize code & fix accidental typos 2021-10-15 00:39:29 -03:00
LuanRT
4bd79e5903 fix(livechat): clear message queue once its contents are no longer necessary 2021-10-15 00:31:53 -03:00
LuanRT
028e723226 format: rephrasing error messages 2021-10-15 00:09:14 -03:00
LuanRT
1a68875aad fix: add some better error handling 2021-10-15 00:04:39 -03:00
LuanRT
de7d52a62c fix: don't try polling the livechat if the live has already ended 2021-10-14 23:56:37 -03:00
LuanRT
b8a2fd01cc chore (examples): remove cookies example 2021-10-14 19:56:20 -03:00
LuanRT
b9ea6e36c8 fix: streams with both audio and video should always emit 'end' 2021-10-14 19:13:22 -03:00
LuanRT
783e6d2435 format: rephrasing comment 2021-10-14 17:11:19 -03:00
LuanRT
0b11e441ff chore: final adjustments 2021-10-14 17:06:20 -03:00
LuanRT
d7db0d2304 style: format code and remove unused stuff 2021-10-14 16:55:35 -03:00
LuanRT
d674eef530 feat: add support for livechats 2021-10-14 16:54:02 -03:00
LuanRT
17aa4edb66 refactor: remove unused parameter in the Innertube constructor 2021-10-13 21:54:44 -03:00
LuanRT
656c0fb7b8 fix: just a typo 2021-10-13 15:23:49 -03:00
LuanRT
d4dce16be0 feat: add method to change notification preferences and get unseen notifications count 2021-10-13 05:55:02 -03:00
LuanRT
80a9ece314 format: remove unnecessary async keyword 2021-10-12 21:08:08 -03:00
15 changed files with 1201 additions and 317 deletions

View File

@@ -17,7 +17,7 @@ jobs:
strategy: strategy:
matrix: matrix:
node-version: [10.x, 12.x, 14.x, 15.x] node-version: [ 12.x, 14.x, 15.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps: steps:

427
README.md
View File

@@ -1,22 +1,30 @@
# YouTube.js # YouTube.js
[![Build](https://github.com/LuanRT/YouTube.js/actions/workflows/node.js.yml/badge.svg)](https://github.com/LuanRT/YouTube.js/actions/workflows/node.js.yml) [![Build](https://github.com/LuanRT/YouTube.js/actions/workflows/node.js.yml/badge.svg)](https://github.com/LuanRT/YouTube.js/actions/workflows/node.js.yml)
[![NPM](https://img.shields.io/npm/v/youtubei.js?color=%2335C757)](https://www.npmjs.com/package/youtubei.js)
[![CodeFactor](https://www.codefactor.io/repository/github/luanrt/youtube.js/badge)](https://www.codefactor.io/repository/github/luanrt/youtube.js)
An object-oriented wrapper around the Innertube API, which is what YouTube itself uses. This makes YouTube.js fast, simple & efficient. And big thanks to [@gatecrasher777](https://github.com/gatecrasher777/ytcog) for his research on the workings of the Innertube API! An object-oriented wrapper around the Innertube API, which is what YouTube itself uses. This makes YouTube.js fast, simple & efficient. And big thanks to [@gatecrasher777](https://github.com/gatecrasher777/ytcog) for his research on the workings of the Innertube API!
#### What can it do? #### What can it do?
As of now, this is one of the most advanced & stable YouTube libraries out there, and it can: As of now, this is one of the most advanced & stable YouTube libraries out there, here's a short summary of what it can do:
- Search. - Search videos
- Get detailed info about videos. - Get detailed info about any video
- Fetch notifications (sign-in required). - Fetch live chat & live stats in real time
- Subscribe/Unsubscribe/Like/Dislike/Comment (sign-in required). - Fetch notifications
- Fetch subscriptions feed
- Change notifications preferences for a channel
- Subscribe/Unsubscribe/Like/Dislike/Comment
- Easily sign into your account in an easy & reliable way.
- Last but not least, you can also download videos! - Last but not least, you can also download videos!
Do note that you must be signed-in to perform actions that involve an account, such as commenting, subscribing, sending messages to a live chat, etc.
#### Do I need an API key to use this? #### Do I need an API key to use this?
No, since it's basically what YouTube itself uses to populate its app/website no API keys are required. No, YouTube.js does not use any official API so no API keys are required.
## Installation ## Installation
@@ -30,11 +38,13 @@ npm install youtubei.js
[2. Interactions](https://github.com/LuanRT/YouTube.js#interactions) [2. Interactions](https://github.com/LuanRT/YouTube.js#interactions)
[3. Downloading Videos](https://github.com/LuanRT/YouTube.js#downloading-videos) [3. Fetching live chats](https://github.com/LuanRT/YouTube.js#fetching-live-chats)
[4. Signing-in](https://github.com/LuanRT/YouTube.js#signing-in) [4. Downloading videos](https://github.com/LuanRT/YouTube.js#downloading-videos)
[5. Disclaimer](https://github.com/LuanRT/YouTube.js#disclaimer) [5. Signing-in](https://github.com/LuanRT/YouTube.js#signing-in)
[6. Disclaimer](https://github.com/LuanRT/YouTube.js#disclaimer)
First of all we're gonna start by initializing the Innertube class: First of all we're gonna start by initializing the Innertube class:
@@ -178,8 +188,271 @@ const video = await youtube.getDetails(search.videos[0].id);
</p> </p>
</details> </details>
Getting comments:
Fetching notifications: ```js
const video = await youtube.getDetails(VIDEO_ID_HERE);
const comments = await video.getComments();
// If you want to load more comments simply call:
const comments_continuation = await comments.getContinuation();
```
<details>
<summary>Output</summary>
<p>
```js
{
"comments":[
{
"text":"The amazing thing to me is the engineering. It's truly remarkable that we can build machines like these.",
"author":{
"name":"Mark B",
"thumbnail":[
{
"url":"https://yt3.ggpht.com/ytc/AKedOLTKxmup9YqNEMvf-nSdOe7F6CwWhUtu4mpUsg=s48-c-k-c0x00ffffff-no-rj",
"width":48,
"height":48
},
{
"url":"https://yt3.ggpht.com/ytc/AKedOLTKxmup9YqNEMvf-nSdOe7F6CwWhUtu4mpUsg=s88-c-k-c0x00ffffff-no-rj",
"width":88,
"height":88
},
{
"url":"https://yt3.ggpht.com/ytc/AKedOLTKxmup9YqNEMvf-nSdOe7F6CwWhUtu4mpUsg=s176-c-k-c0x00ffffff-no-rj",
"width":176,
"height":176
}
],
"channel_id":"UClnPXUOtCLnKsbS2reuN7wg"
},
"metadata":{
"published":"2 months ago",
"is_liked":false,
"is_channel_owner":false,
"like_count":"54",
"reply_count":3,
"id":"Ugy-bGGepYil_2dAQUp4AaABAg"
}
},
{
"text":"May 25th, 2021 and everything has gone perfectly! Ingenuity, moxy and perseverance all working to plan. Unbelievable accomplishments!!!",
"author":{
"name":"cliff luebke",
"thumbnail":[
{
"url":"https://yt3.ggpht.com/ytc/AKedOLR1_6jvPZa_ycrkUEVxVxo0Alo25e7O8fOcm5v9ww=s48-c-k-c0x00ffffff-no-rj",
"width":48,
"height":48
},
{
"url":"https://yt3.ggpht.com/ytc/AKedOLR1_6jvPZa_ycrkUEVxVxo0Alo25e7O8fOcm5v9ww=s88-c-k-c0x00ffffff-no-rj",
"width":88,
"height":88
},
{
"url":"https://yt3.ggpht.com/ytc/AKedOLR1_6jvPZa_ycrkUEVxVxo0Alo25e7O8fOcm5v9ww=s176-c-k-c0x00ffffff-no-rj",
"width":176,
"height":176
}
],
"channel_id":"UCeVFeX4jCgaJpvNJ4f_I4RA"
},
"metadata":{
"published":"4 months ago",
"is_liked":false,
"is_channel_owner":false,
"like_count":"54",
"reply_count":0,
"id":"UgylkZHOe7v78hxHPpl4AaABAg"
}
},
//...
],
"comment_count":"3,231" // not available in continuations
}
```
</p>
</details>
Getting subscriptions feed:
```js
const mysubfeed = await youtube.getSubscriptionsFeed();
```
<details>
<summary>Output</summary>
<p>
```js
{
"today":[
{
"title":"Life As My P*nis",
"id":"udDINILQH10",
"channel":"penguinz0",
"metadata":{
"view_count":"220,432 views",
"thumbnail":[
{
"url":"https://i.ytimg.com/vi/udDINILQH10/hqdefault.jpg?sqp=-oaymwEbCNIBEHZIVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLAQ7bbeUhCxRSg-g-CPek-soixUMQ",
"width":210,
"height":118
},
{
"url":"https://i.ytimg.com/vi/udDINILQH10/hqdefault.jpg?sqp=-oaymwEcCPYBEIoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLB6fuvBJMeLtkM0TLkZwharsyojjA",
"width":246,
"height":138
},
{
"url":"https://i.ytimg.com/vi/udDINILQH10/hqdefault.jpg?sqp=-oaymwEcCNACELwBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLDd7BncH1QuZD-Hada_n6dAVRTnmg",
"width":336,
"height":188
}
],
"published":"2 hours ago",
"badges":"N/A",
"owner_badges":[
"Verified"
]
}
},
{
"title":"Perseverance and Ingenuity went two weeks without contacting Earth",
"id":"VsmYZMVCHuc",
"channel":"Mars Guy",
"metadata":{
"view_count":"2,633 views",
"thumbnail":[
{
"url":"https://i.ytimg.com/vi/VsmYZMVCHuc/hqdefault.jpg?sqp=-oaymwEbCNIBEHZIVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLA0-vwAgpFbMO1zG4HTzdHZey1kZQ",
"width":210,
"height":118
},
{
"url":"https://i.ytimg.com/vi/VsmYZMVCHuc/hqdefault.jpg?sqp=-oaymwEcCPYBEIoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLBtM3W-RXsCfPnxgnrktaBkiL9zzg",
"width":246,
"height":138
},
{
"url":"https://i.ytimg.com/vi/VsmYZMVCHuc/hqdefault.jpg?sqp=-oaymwEcCNACELwBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLDTil7At4FUVYeSNySOoFoKlPXWSA",
"width":336,
"height":188
}
],
"published":"15 hours ago",
"badges":"N/A",
"owner_badges":"N/A"
}
}
//...
],
"yesterday":[
{
"title":"Fortnite - S.T.A.R.S (Resident Evil) | PS5, PS4",
"id":"-ZLEQOVbWD4",
"channel":"PlayStation",
"metadata":{
"view_count":"157,197 views",
"thumbnail":[
{
"url":"https://i.ytimg.com/vi/-ZLEQOVbWD4/hqdefault.jpg?sqp=-oaymwEbCNIBEHZIVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLAeA1fLzsEA0ZIouNJuMDJOqOc9Ng",
"width":210,
"height":118
},
{
"url":"https://i.ytimg.com/vi/-ZLEQOVbWD4/hqdefault.jpg?sqp=-oaymwEcCPYBEIoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLDzvhxJ6m2ztykk2ezNH2Din33hEw",
"width":246,
"height":138
},
{
"url":"https://i.ytimg.com/vi/-ZLEQOVbWD4/hqdefault.jpg?sqp=-oaymwEcCNACELwBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLDo67DvnaiKzOVfNm9lJg_Edd1UDQ",
"width":336,
"height":188
}
],
"published":"1 day ago",
"badges":"N/A",
"owner_badges":[
"Verified"
]
}
},
//...
],
"this_week":[
{
"title":"Horrible $100 Million Gold Mansion",
"id":"F-d3CEYJyrg",
"channel":"penguinz0",
"metadata":{
"view_count":"693,041 views",
"thumbnail":[
{
"url":"https://i.ytimg.com/vi/F-d3CEYJyrg/hqdefault.jpg?sqp=-oaymwEbCNIBEHZIVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLDdMMGG-50O4U5uIcoZ2FoiO6Mopg",
"width":210,
"height":118
},
{
"url":"https://i.ytimg.com/vi/F-d3CEYJyrg/hqdefault.jpg?sqp=-oaymwEcCPYBEIoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLAwqMy1ekLaxWGnjWwCJl7z7Nw2aQ",
"width":246,
"height":138
},
{
"url":"https://i.ytimg.com/vi/F-d3CEYJyrg/hqdefault.jpg?sqp=-oaymwEcCNACELwBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLADaPBg0vh52e6clHvUf5otBZO9HA",
"width":336,
"height":188
}
],
"published":"2 days ago",
"badges":"N/A",
"owner_badges":[
"Verified"
]
}
},
{
"title":"OOopsieeee",
"id":"mJ2WOIhEPm8",
"channel":"PewDiePie",
"metadata":{
"view_count":"1,953,970 views",
"thumbnail":[
{
"url":"https://i.ytimg.com/vi/mJ2WOIhEPm8/hqdefault.jpg?sqp=-oaymwEbCNIBEHZIVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLCrJe2a6rasJICj_jchMquZ2YGVrQ",
"width":210,
"height":118
},
{
"url":"https://i.ytimg.com/vi/mJ2WOIhEPm8/hqdefault.jpg?sqp=-oaymwEcCPYBEIoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCpxERsiLJayuKegeb5mHw3Ok6wGA",
"width":246,
"height":138
},
{
"url":"https://i.ytimg.com/vi/mJ2WOIhEPm8/hqdefault.jpg?sqp=-oaymwEcCNACELwBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLAlRLjypimzrE3GD_iGYDGwTlCGvA",
"width":336,
"height":188
}
],
"published":"2 days ago",
"badges":"N/A",
"owner_badges":[
"Verified"
]
}
},
//...
]
}
```
</p>
</details>
Getting notifications:
```js ```js
const notifications = await youtube.getNotifications(); const notifications = await youtube.getNotifications();
@@ -243,65 +516,61 @@ const video = await youtube.getDetails(VIDEO_ID_HERE);
await video.comment('Haha, nice!'); await video.comment('Haha, nice!');
``` ```
* Changing notification preferences:
```js
const video = await youtube.getDetails(VIDEO_ID_HERE);
await video.setNotificationPref('ALL'); // ALL | NONE | PERSONALIZED
```
All of the interactions above will return ```{ success: true, status_code: 200 }``` if everything goes alright. All of the interactions above will return ```{ success: true, status_code: 200 }``` if everything goes alright.
### Signing-in: ### Fetching live chats:
--- ---
YouTube.js isn't able to download live content yet, but it does allow you to fetch live chats in an easy way plus you can also send messages!
This library allows you to sign-in in two different ways:
- Using OAuth 2.0, easy, simple & reliable.
- Cookies, usually more complicated to get and unreliable.
OAuth 2.0:
```js ```js
const fs = require('fs');
const Innertube = require('youtubei.js'); const Innertube = require('youtubei.js');
const creds_path = './yt_oauth_creds.json';
async function start() { async function start() {
const creds = fs.existsSync(creds_path) && JSON.parse(fs.readFileSync(creds_path).toString()) || {};
const youtube = await new Innertube(); const youtube = await new Innertube();
const search = await youtube.search('Some random live');
const video = await youtube.getDetails(search.videos[0].id);
// Only triggered when signing-in. // This should only be called if you're sure it's a live and that it's still ongoing
youtube.on('auth', (data) => { const livechat = video.getLivechat();
if (data.status === 'AUTHORIZATION_PENDING') {
console.info(`Hello!\nOn your phone or computer, go to ${data.verification_url} and enter the code ${data.code}`); // Updated stats about the livestream
} else if (data.status === 'SUCCESS') { livechat.on('update-metadata', (data) => {
fs.writeFileSync(creds_path, JSON.stringify({ access_token: data.access_token, refresh_token: data.refresh_token })); console.info('Info:', data);
console.info('Successfully signed-in, enjoy!'); });
// Fired whenever there is a new message or other chat events
livechat.on('chat-update', (message) => {
console.info(`- ${message.author.name}\n${message.text}\n\n`);
if(message.text == '!info') {
livechat.sendMessage('Hello! This message was sent from YouTube.js');
} }
}); });
// Triggered whenever the access token is refreshed.
youtube.on('update-credentials', (data) => {
fs.writeFileSync(creds_path, JSON.stringify({ access_token: data.access_token, refresh_token: data.refresh_token }));
console.info('Credentials updated!', data);
});
await youtube.signIn(creds);
//...
} }
start(); start();
``` ```
Stop fetching the live chat:
Cookies:
```js ```js
const Innertube = require('youtubei.js'); livechat.stop();
```
async function start() { Deleting a message:
const youtube = await new Innertube(COOKIE_HERE); ```js
//... const msg = await livechat.sendMessage('Nice live!');
} await msg.deleteMessage();
start();
``` ```
### Downloading videos: ### Downloading videos:
---
The library provides an easy-to-use and simple downloader:
```js ```js
const fs = require('fs'); const fs = require('fs');
@@ -349,6 +618,65 @@ Cancelling a download:
stream.cancel(); stream.cancel();
``` ```
### Signing-in:
---
This library allows you to sign-in in two different ways:
- Using OAuth 2.0, easy, simple & reliable.
- Cookies, usually more complicated to get and unreliable.
OAuth:
```js
const fs = require('fs');
const Innertube = require('youtubei.js');
const creds_path = './yt_oauth_creds.json';
async function start() {
const creds = fs.existsSync(creds_path) && JSON.parse(fs.readFileSync(creds_path).toString()) || {};
const youtube = await new Innertube();
// Only triggered when signing-in.
youtube.on('auth', (data) => {
if (data.status === 'AUTHORIZATION_PENDING') {
console.info(`Hello!\nOn your phone or computer, go to ${data.verification_url} and enter the code ${data.code}`);
} else if (data.status === 'SUCCESS') {
fs.writeFileSync(creds_path, JSON.stringify({ access_token: data.access_token, refresh_token: data.refresh_token }));
console.info('Successfully signed-in, enjoy!');
}
});
// Triggered whenever the access token is refreshed.
youtube.on('update-credentials', (data) => {
fs.writeFileSync(creds_path, JSON.stringify({ access_token: data.access_token, refresh_token: data.refresh_token }));
console.info('Credentials updated!', data);
});
await youtube.signIn(creds);
//...
}
start();
```
Cookies:
```js
const Innertube = require('youtubei.js');
async function start() {
const youtube = await new Innertube(COOKIE_HERE);
//...
}
start();
```
## Note
Never sign-in with your personal account, you might get banned if you spam (don't ever do that) or simply because YouTube detected unusual activity coming from your account. Also, I'm not responsible if any of that happens to you.
## Contributing ## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
@@ -358,5 +686,6 @@ This project is not affiliated with, endorsed, or sponsored by YouTube or any of
All trademarks, logos and brand names are the property of their respective owners. All trademarks, logos and brand names are the property of their respective owners.
Should you have any questions or concerns please contact me directly via email. Should you have any questions or concerns please contact me directly via email.
## License ## License
[MIT](https://choosealicense.com/licenses/mit/) [MIT](https://choosealicense.com/licenses/mit/)

View File

@@ -1,17 +1,22 @@
'use strict';
const fs = require('fs'); const fs = require('fs');
const Innertube = require('..'); const Innertube = require('..');
const COOKIE = 'YT_COOKIE_HERE';
async function start() { async function start() {
const youtube = await new Innertube(COOKIE); const youtube = await new Innertube();
// Searching, getting details about videos & making interactions: // Searching, getting details about videos & making interactions:
const search = await youtube.search('Looking for life on Mars - documentary'); const search = await youtube.search('Looking for life on Mars - documentary');
console.info('Search results:', search); console.info('Search results:', search);
if (search.videos.length === 0)
return console.info('[INFO]', 'Could not find any video about that on YouTube.');
const video = await youtube.getDetails(search.videos[0].id); const video = await youtube.getDetails(search.videos[0].id);
console.info('Video details:', video); console.info('Video details:', video);
if (video.error) return;
if (youtube.logged_in) { if (youtube.logged_in) {
const myNotifications = await youtube.getNotifications(); const myNotifications = await youtube.getNotifications();
console.info('My notifications:', myNotifications); console.info('My notifications:', myNotifications);
@@ -54,7 +59,7 @@ async function start() {
type: 'videoandaudio' // can be “video”, “audio” and “videoandaudio” type: 'videoandaudio' // can be “video”, “audio” and “videoandaudio”
}); });
stream.pipe(fs.createWriteStream(`./${search.videos[0].title}.mp4`)); stream.pipe(fs.createWriteStream(`./${search.videos[0].id}.mp4`));
stream.on('start', () => { stream.on('start', () => {
console.info('[DOWNLOADER]', 'Starting download now!'); console.info('[DOWNLOADER]', 'Starting download now!');

View File

@@ -3,169 +3,166 @@
const Axios = require('axios'); const Axios = require('axios');
const Utils = require('./Utils'); const Utils = require('./Utils');
const Constants = require('./Constants'); const Constants = require('./Constants');
const Uuid = require('uuid');
async function subscribe(session, video_id, channel_id) { async function engage(session, engagement_type, args = {}) {
if (!session.logged_in) throw new Error('You must be logged in to subscribe to a channel'); if (!session.logged_in) throw new Error('You are not logged in');
let data = { let data = {};
context: session.context, switch (engagement_type) {
channelIds: [channel_id] case 'like/like':
}; case 'like/dislike':
case 'like/removelike':
const response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/subscription/subscribe${session.logged_in && session.cookie.length < 1 ? '' : `?key=${session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session, id: video_id, data })).catch((error) => error); data = {
if (response instanceof Error) { context: session.context,
return { target: {
success: false, videoId: args.video_id
status_code: response.response.status, }
message: response.message };
}; break;
} else if (response.data.responseContext) { case 'subscription/subscribe':
return { case 'subscription/unsubscribe':
success: true, data = {
status_code: response.status, context: session.context,
}; channelIds: [args.channel_id]
};
break;
case 'comment/create_comment':
data = {
context: session.context,
commentText: args.text,
createCommentParams: Utils.generateCommentParams(args.video_id)
};
break;
default:
} }
const response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/${engagement_type}${session.logged_in && session.cookie.length < 1 ? '' : `?key=${session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session, id: args.video_id, data })).catch((error) => error);
if (response instanceof Error) return { success: false, status_code: response.response.status, message: response.message };
return {
success: true,
status_code: response.status
};
} }
async function unsubscribe(session, video_id, channel_id) { async function browse(session, action_type) {
if (!session.logged_in) throw new Error('You must be logged in to unsubscribe from a channel'); if (!session.logged_in) throw new Error('You are not logged in');
let data = { let data;
context: session.context, switch (action_type) {
channelIds: [channel_id] case 'subscriptions_feed':
}; data = {
context: session.context,
const response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/subscription/unsubscribe${session.logged_in && session.cookie.length < 1 ? '' : `?key=${session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session, id: video_id, data })).catch((error) => error); browseId: 'FEsubscriptions'
if (response instanceof Error) { };
return { break;
success: false, default:
status_code: response.response.status,
message: response.message
};
} else if (response.data.responseContext) {
return {
success: true,
status_code: response.status,
};
} }
const response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/browse${session.logged_in && session.cookie.length < 1 ? '' : `?key=${session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session })).catch((error) => error);
if (response instanceof Error) return { success: false, status_code: response.response.status, message: response.message };
return {
success: true,
status_code: response.status,
data: response.data
};
} }
async function likeVideo(session, video_id) { async function notifications(session, action_type, args = {}) {
if (!session.logged_in) throw new Error('You must be logged in to like a video'); if (!session.logged_in) throw new Error('You are not logged in');
let data = { let data;
context: session.context, switch (action_type) {
target: { case 'modify_channel_preference':
videoId: video_id let pref_types = { PERSONALIZED: 1, ALL: 2, NONE: 3 };
} data = {
}; context: session.context,
params: Utils.encodeNotificationPref(args.channel_id, pref_types[args.pref.toUpperCase()])
const response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/like/like${session.logged_in && session.cookie.length < 1 ? '' : `?key=${session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session, id: video_id, data })).catch((error) => error); };
if (response instanceof Error) { break;
return { case 'get_notification_menu':
success: false, data = {
status_code: response.response.status, context: session.context,
message: response.message notificationsMenuRequestType: 'NOTIFICATIONS_MENU_REQUEST_TYPE_INBOX'
}; };
} else if (response.data.responseContext) { break;
return { case 'get_unseen_count':
success: true, data = {
status_code: response.status, context: session.context
}; };
break;
default:
} }
const response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/notification/${action_type}${session.logged_in && session.cookie.length < 1 ? '' : `?key=${session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session })).catch((error) => error);
if (response instanceof Error) return { success: false, status_code: response.response.status, message: response.message };
if (action_type === 'modify_channel_preference') return { success: true, status_code: response.status };
return {
success: true,
status_code: response.status,
data: response.data
};
} }
async function dislikeVideo(session, video_id) { async function livechat(session, action_type, args = {}) {
if (!session.logged_in) throw new Error('You must be logged in to like a video'); let data;
let data = { switch (action_type) {
context: session.context, case 'live_chat/send_message':
target: { data = {
videoId: video_id context: session.context,
} params: Utils.generateMessageParams(args.channel_id, args.video_id),
}; clientMessageId: `INntLiB${Uuid.v4()}`,
richMessage: {
const response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/like/dislike${session.logged_in && session.cookie.length < 1 ? '' : `?key=${session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session, id: video_id, data })).catch((error) => error); textSegments: [{ text: args.text }]
if (response instanceof Error) { }
return { };
success: false, break;
status_code: response.response.status, case 'live_chat/get_item_context_menu':
message: response.message data = {
}; context: session.context
} else if (response.data.responseContext) { };
return { break;
success: true, case 'live_chat/moderate':
status_code: response.status, data = {
}; context: session.context,
params: args.cmd_params
};
break;
default:
} }
const response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/${action_type}${session.logged_in && session.cookie.length < 1 ? '' : `?key=${session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session, params: args.params })).catch((error) => error);
if (response instanceof Error) return { success: false, status_code: response.response.status, message: response.message };
return {
success: true,
status_code: response.status,
data: response.data
};
} }
async function removeLike(session, video_id) { async function getContinuation(session, info = {}) {
if (!session.logged_in) throw new Error('You must be logged in to remove a like/dislike.'); let data = { context: session.context };
let data = {
context: session.context,
target: {
videoId: video_id
}
};
const response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/like/removelike${session.logged_in && session.cookie.length < 1 ? '' : `?key=${session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session, id: video_id, data })).catch((error) => error); if (info.continuation_token) {
if (response instanceof Error) { data.continuation = info.continuation_token;
return {
success: false,
status_code: response.response.status,
message: response.message
};
} else if (response.data.responseContext) {
return {
success: true,
status_code: response.status,
};
} }
if (info.video_id) {
data.videoId = info.video_id;
data.racyCheckOk = true;
data.contentCheckOk = false;
data.autonavState = 'STATE_NONE';
data.playbackContext = {
vis: 0,
lactMilliseconds: '-1'
};
data.captionsRequested = false;
}
const response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/next${session.logged_in && session.cookie.length < 1 ? '' : `?key=${session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session })).catch((error) => error);
if (response instanceof Error) return { success: false, status_code: response.response.status, message: response.message };
return {
success: true,
status_code: response.status,
data: response.data
};
} }
async function commentVideo(session, video_id, text) { module.exports = { engage, browse, notifications, livechat, getContinuation };
if (!text) throw new Error('No text was provided');
if (!session.logged_in) throw new Error('You must be logged in to post a comment.');
let data = {
context: session.context,
commentText: text,
createCommentParams: Utils.encodeId(video_id)
};
const response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/comment/create_comment${session.logged_in && session.cookie.length < 1 ? '' : `?key=${session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session, id: video_id, data })).catch((error) => error);
if (response instanceof Error) {
return {
success: false,
status_code: response.response.status,
message: response.message
};
} else if (response.data.responseContext) {
return {
success: true,
status_code: response.status,
};
}
}
async function getNotifications(session) {
if (!session.logged_in) throw new Error('You must be logged in to fetch notifications');
let data = {
context: session.context,
notificationsMenuRequestType: 'NOTIFICATIONS_MENU_REQUEST_TYPE_INBOX'
};
const response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/notification/get_notification_menu${session.logged_in && session.cookie.length < 1 ? '' : `?key=${session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session, data, desktop: true })).catch((error) => error);
if (response instanceof Error) {
return {
success: false,
status_code: response.response.status,
message: response.message
};
} else {
return {
success: true,
status_code: response.status,
data: response.data
};
}
}
module.exports = { subscribe, unsubscribe, likeVideo, dislikeVideo, removeLike, commentVideo, getNotifications };

View File

@@ -43,6 +43,7 @@ const default_headers = (session) => {
const innertube_request_opts = (info) => { const innertube_request_opts = (info) => {
if (info.desktop === undefined) info.desktop = true; if (info.desktop === undefined) info.desktop = true;
let req_opts = { let req_opts = {
params: info.params || {},
headers: { headers: {
'accept': '*/*', 'accept': '*/*',
'user-agent': Utils.getRandomUserAgent(info.desktop ? 'desktop' : 'mobile').userAgent, 'user-agent': Utils.getRandomUserAgent(info.desktop ? 'desktop' : 'mobile').userAgent,
@@ -63,10 +64,6 @@ const innertube_request_opts = (info) => {
req_opts.headers.authorization = info.session.cookie.length < 1 ? `Bearer ${info.session.access_token}` : info.session.auth_apisid; req_opts.headers.authorization = info.session.cookie.length < 1 ? `Bearer ${info.session.access_token}` : info.session.auth_apisid;
} }
if (info.data) {
req_opts.headers['content-length'] = Buffer.byteLength(JSON.stringify(info.data), 'utf8');
}
if (info.id) { if (info.id) {
req_opts.headers.referer = (info.desktop ? urls.YT_BASE_URL : urls.YT_MOBILE_URL) + '/watch?v=' + info.id; req_opts.headers.referer = (info.desktop ? urls.YT_BASE_URL : urls.YT_MOBILE_URL) + '/watch?v=' + info.id;
} }
@@ -161,21 +158,31 @@ const formatVideoData = (data, context, desktop) => {
video_details.description = data[2].playerResponse.videoDetails.shortDescription; video_details.description = data[2].playerResponse.videoDetails.shortDescription;
video_details.thumbnail = data[2].playerResponse.videoDetails.thumbnail.thumbnails.slice(-1)[0]; video_details.thumbnail = data[2].playerResponse.videoDetails.thumbnail.thumbnails.slice(-1)[0];
// actions // Functions
video_details.like = like => {}; video_details.like = () => {};
video_details.dislike = dislike => {}; video_details.dislike = () => {};
video_details.removeLike = remove_like => {}; video_details.removeLike = () => {};
video_details.subscribe = subscribe => {}; video_details.subscribe = () => {};
video_details.unsubscribe = unsubscribe => {}; video_details.unsubscribe = () => {};
video_details.comment = comment => {}; video_details.comment = () => {};
video_details.getComments = () => {};
video_details.setNotificationPref = () => {};
video_details.getLivechat = () => {};
// additional metadata // Additional metadata
video_details.metadata = metadata; video_details.metadata = metadata;
} }
return video_details; return video_details;
}; };
const base64_alphabet = {
normal: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'.split(''),
reverse: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'.split('')
};
const filters = (order) => { const filters = (order) => {
// TODO: Refactor this with protobuf encoding
switch (order) { switch (order) {
case 'any,any,relevance': case 'any,any,relevance':
return 'EgIQAQ%3D%3D'; return 'EgIQAQ%3D%3D';
@@ -325,4 +332,4 @@ const filters = (order) => {
} }
}; };
module.exports = { urls, oauth, oauth_reqopts, default_headers, innertube_request_opts, video_details_reqbody, stream_headers, formatVideoData, filters }; module.exports = { urls, oauth, oauth_reqopts, default_headers, innertube_request_opts, video_details_reqbody, stream_headers, formatVideoData, base64_alphabet, filters };

View File

@@ -5,7 +5,9 @@ const Stream = require('stream');
const OAuth = require('./OAuth'); const OAuth = require('./OAuth');
const Utils = require('./Utils'); const Utils = require('./Utils');
const Player = require('./Player'); const Player = require('./Player');
const NToken = require('./NToken');
const Actions = require('./Actions'); const Actions = require('./Actions');
const Livechat = require('./Livechat');
const Constants = require('./Constants'); const Constants = require('./Constants');
const SigDecipher = require('./SigDecipher'); const SigDecipher = require('./SigDecipher');
const EventEmitter = require('events'); const EventEmitter = require('events');
@@ -13,7 +15,7 @@ const TimeToSeconds = require('time-to-seconds');
const CancelToken = Axios.CancelToken; const CancelToken = Axios.CancelToken;
class Innertube extends EventEmitter { class Innertube extends EventEmitter {
constructor(cookie, sign_in) { constructor(cookie) {
super(); super();
this.cookie = cookie || ''; this.cookie = cookie || '';
return this.init(); return this.init();
@@ -90,6 +92,7 @@ class Innertube extends EventEmitter {
} }
async search(query, options = { period: 'any', order: 'relevance', duration: 'any' }) { async search(query, options = { period: 'any', order: 'relevance', duration: 'any' }) {
if (!query) throw new Error('No query was provided');
if (!this.initialized) throw new Error('Missing Innertube data.'); if (!this.initialized) throw new Error('Missing Innertube data.');
const yt_response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/search${this.logged_in && this.cookie.length < 1 ? '' : `?key=${this.key}`}`, JSON.stringify({ context: this.context, params: Constants.filters(options.period + ',' + options.duration + ',' + options.order), query }), Constants.innertube_request_opts({ session: this })).catch((error) => error); const yt_response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/search${this.logged_in && this.cookie.length < 1 ? '' : `?key=${this.key}`}`, JSON.stringify({ context: this.context, params: Constants.filters(options.period + ',' + options.duration + ',' + options.order), query }), Constants.innertube_request_opts({ session: this })).catch((error) => error);
@@ -134,23 +137,124 @@ class Innertube extends EventEmitter {
} }
async getDetails(id) { async getDetails(id) {
if (!id) return { error: 'Missing video id' };
const data = await this.requestVideoInfo(id, false); const data = await this.requestVideoInfo(id, false);
const video_data = Constants.formatVideoData(data, this, false); const video_data = Constants.formatVideoData(data, this, false);
video_data.like = like => Actions.likeVideo(this, id); if (video_data.metadata.is_live_content) {
video_data.dislike = dislike => Actions.dislikeVideo(this, id); const data_continuation = await Actions.getContinuation(this, { video_id: id });
video_data.removeLike = remove_like => Actions.removeLike(this, id); if (!data_continuation.data.contents.twoColumnWatchNextResults.conversationBar) return;
video_data.subscribe = subscribe => Actions.subscribe(this, id, video_data.metadata.channel_id); video_data.getLivechat = () => new Livechat(this, data_continuation.data.contents.twoColumnWatchNextResults.conversationBar.liveChatRenderer.continuations[0].reloadContinuationData.continuation, video_data.metadata.channel_id, id);
video_data.unsubscribe = unsubscribe => Actions.unsubscribe(this, id, video_data.metadata.channel_id); } else {
video_data.comment = comment => Actions.commentVideo(this, id, comment); video_data.getLivechat = () => {};
}
video_data.like = () => Actions.engage(this, 'like/like', { video_id: id });
video_data.dislike = () => Actions.engage(this, 'like/dislike', { video_id: id });
video_data.removeLike = () => Actions.engage(this, 'like/removelike', { video_id: id });
video_data.subscribe = () => Actions.engage(this, 'subscription/subscribe', { video_id: id, channel_id: video_data.metadata.channel_id });
video_data.unsubscribe = () => Actions.engage(this, 'subscription/unsubscribe', { video_id: id, channel_id: video_data.metadata.channel_id });
video_data.comment = text => Actions.engage(this, 'comment/create_comment', { video_id: id, text });
video_data.getComments = () => this.getComments(id);
video_data.setNotificationPref = pref => Actions.notifications(this, 'modify_channel_preference', { channel_id: video_data.metadata.channel_id, pref: pref || 'NONE' });
return video_data; return video_data;
} }
async getComments(video_id, token) {
let comment_section_token;
if (!token) {
const data_continuation = await Actions.getContinuation(this, { video_id });
const item_section_renderer = data_continuation.data.contents.twoColumnWatchNextResults.results.results.contents.find((item) => item.itemSectionRenderer);
comment_section_token = item_section_renderer.itemSectionRenderer.contents[0].continuationItemRenderer.continuationEndpoint.continuationCommand.token;
}
const response = await Actions.getContinuation(this, { continuation_token: comment_section_token || token });
if (!response.success) throw new Error('Could not fetch comment section');
const comments_section = { comments: [] };
!token && (comments_section.comment_count = response.data.onResponseReceivedEndpoints[0].reloadContinuationItemsCommand.continuationItems && response.data.onResponseReceivedEndpoints[0].reloadContinuationItemsCommand.continuationItems[0].commentsHeaderRenderer.countText.runs[0].text || 'N/A');
let continuation_token;
!token && (continuation_token = response.data.onResponseReceivedEndpoints[1].reloadContinuationItemsCommand.continuationItems.find((item) => item.continuationItemRenderer).continuationItemRenderer.continuationEndpoint.continuationCommand.token)
|| (continuation_token = response.data.onResponseReceivedEndpoints[0].appendContinuationItemsAction.continuationItems.find((item) => item.continuationItemRenderer).continuationItemRenderer.continuationEndpoint.continuationCommand.token);
comments_section.getContinuation = () => this.getComments(video_id, continuation_token);
let contents;
!token && (contents = response.data.onResponseReceivedEndpoints[1].reloadContinuationItemsCommand.continuationItems)
|| (contents = response.data.onResponseReceivedEndpoints[0].appendContinuationItemsAction.continuationItems);
contents.forEach((thread) => {
if (!thread.commentThreadRenderer) return;
const comment = {
text: thread.commentThreadRenderer.comment.commentRenderer.contentText.runs.map((t) => t.text).join(' '),
author: {
name: thread.commentThreadRenderer.comment.commentRenderer.authorText.simpleText,
thumbnail: thread.commentThreadRenderer.comment.commentRenderer.authorThumbnail.thumbnails,
channel_id: thread.commentThreadRenderer.comment.commentRenderer.authorEndpoint.browseEndpoint.browseId
},
metadata: {
published: thread.commentThreadRenderer.comment.commentRenderer.publishedTimeText.runs[0].text,
is_liked: thread.commentThreadRenderer.comment.commentRenderer.isLiked,
is_channel_owner: thread.commentThreadRenderer.comment.commentRenderer.authorIsChannelOwner,
like_count: thread.commentThreadRenderer.comment.commentRenderer.voteCount.simpleText,
reply_count: thread.commentThreadRenderer.comment.commentRenderer.replyCount || 0,
id: thread.commentThreadRenderer.comment.commentRenderer.commentId,
}
};
comments_section.comments.push(comment);
});
return comments_section;
}
async getSubscriptionsFeed() {
const response = await Actions.browse(this, 'subscriptions_feed');
if (!response.success) throw new Error('Could not fetch subscriptions feed');
const contents = response.data.contents.twoColumnBrowseResultsRenderer.tabs[0].tabRenderer.content.sectionListRenderer.contents;
const subscriptions_feed = {};
contents.forEach((section) => {
if (!section.itemSectionRenderer) return;
const section_contents = section.itemSectionRenderer.contents[0];
const section_items = section_contents.shelfRenderer.content.gridRenderer.items;
const key = section_contents.shelfRenderer.title.runs[0].text;
subscriptions_feed[key.toLowerCase().replace(/ +/g, '_')] = [];
section_items.forEach((item) => {
const content = {
title: item.gridVideoRenderer.title.runs.map((run) => run.text).join(' '),
id: item.gridVideoRenderer.videoId,
channel: item.gridVideoRenderer.shortBylineText && item.gridVideoRenderer.shortBylineText.runs[0].text || 'N/A',
metadata: {
view_count: item.gridVideoRenderer.viewCountText && item.gridVideoRenderer.viewCountText.simpleText || 'N/A',
thumbnail: item.gridVideoRenderer.thumbnail && item.gridVideoRenderer.thumbnail.thumbnails || [],
published: item.gridVideoRenderer.publishedTimeText && item.gridVideoRenderer.publishedTimeText.simpleText || 'N/A',
badges: item.gridVideoRenderer.badges && item.gridVideoRenderer.badges.map((badge) => badge.metadataBadgeRenderer.label) || 'N/A',
owner_badges: item.gridVideoRenderer.ownerBadges && item.gridVideoRenderer.ownerBadges.map((badge) => badge.metadataBadgeRenderer.tooltip) || 'N/A'
}
};
subscriptions_feed[key.toLowerCase().replace(/ +/g, '_')].push(content);
});
});
return subscriptions_feed;
}
async getNotifications() { async getNotifications() {
const response = await Actions.getNotifications(this); const response = await Actions.notifications(this, 'get_notification_menu');
const contents = response.data.actions[0].openPopupAction.popup.multiPageMenuRenderer.sections[0].multiPageMenuNotificationSectionRenderer.items; if (!response.success) throw new Error('Could not fetch notifications');
return contents.map((notification) => {
const contents = response.data.actions[0].openPopupAction.popup.multiPageMenuRenderer.sections[0];
if (!contents.multiPageMenuNotificationSectionRenderer) return { error: 'You don\'t have any notification.' };
return contents.multiPageMenuNotificationSectionRenderer.items.map((notification) => {
if (!notification.notificationRenderer) return; if (!notification.notificationRenderer) return;
notification = notification.notificationRenderer; notification = notification.notificationRenderer;
return { return {
@@ -159,17 +263,23 @@ class Innertube extends EventEmitter {
channel_name: notification.contextualMenu.menuRenderer.items[1].menuServiceItemRenderer.text.runs[1].text, channel_name: notification.contextualMenu.menuRenderer.items[1].menuServiceItemRenderer.text.runs[1].text,
channel_thumbnail: notification.thumbnail.thumbnails[0], channel_thumbnail: notification.thumbnail.thumbnails[0],
video_thumbnail: notification.videoThumbnail.thumbnails[0], video_thumbnail: notification.videoThumbnail.thumbnails[0],
video_url: 'https://youtu.be/' + notification.navigationEndpoint.watchEndpoint.videoId, video_url: `https://youtu.be/${notification.navigationEndpoint.watchEndpoint.videoId}`,
read: notification.read, read: notification.read,
notification_id: notification.notificationId, notification_id: notification.notificationId,
}; };
}).filter((notification_block) => notification_block); }).filter((notification_block) => notification_block);
} }
async getUnseenNotificationsCount() {
const response = await Actions.notifications(this, 'get_unseen_count');
if (!response.success) throw new Error('Could not fetch unseen notifications count');
return response.data.unseenCount;
}
async requestVideoInfo(id, desktop) { async requestVideoInfo(id, desktop) {
let response; let response;
if (!desktop) { if (!desktop) {
response = await Axios.get(Constants.urls.YT_WATCH_PAGE + '?v=' + id + 't=8s&pbj=1&bpctr=9999999999&has_verified=1&', Constants.innertube_request_opts({ session: this, id, desktop: false })).catch((error) => error); response = await Axios.get(`${Constants.urls.YT_WATCH_PAGE}?v=${id}&t=8s&pbj=1`, Constants.innertube_request_opts({ session: this, id, desktop: false })).catch((error) => error);
} else { } else {
response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/player${this.logged_in && this.cookie.length < 1 ? '' : `?key=${this.key}`}`, JSON.stringify(Constants.video_details_reqbody(id, this.sts, this.context)), Constants.innertube_request_opts({ session: this, id, desktop: true })).catch((error) => error); response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/player${this.logged_in && this.cookie.length < 1 ? '' : `?key=${this.key}`}`, JSON.stringify(Constants.video_details_reqbody(id, this.sts, this.context)), Constants.innertube_request_opts({ session: this, id, desktop: true })).catch((error) => error);
} }
@@ -178,6 +288,8 @@ class Innertube extends EventEmitter {
} }
download(id, options = {}) { download(id, options = {}) {
if (!id) throw new Error('Missing video id');
options.quality = options.quality || '360p'; options.quality = options.quality || '360p';
options.type = options.type || 'videoandaudio'; options.type = options.type || 'videoandaudio';
options.format = options.format || 'mp4'; options.format = options.format || 'mp4';
@@ -197,12 +309,12 @@ class Innertube extends EventEmitter {
format.url = format.url || format.signatureCipher || format.cipher; format.url = format.url || format.signatureCipher || format.cipher;
if (format.signatureCipher || format.cipher) { if (format.signatureCipher || format.cipher) {
format.url = new SigDecipher(format.url, this.context.client.clientVersion, this.player.sig_decipher_sc, this.player.encodeN).decipher(); format.url = new SigDecipher(format.url, this.context.client.clientVersion, this.player).decipher();
} else { } else {
const url_components = new URL(format.url); const url_components = new URL(format.url);
url_components.searchParams.set('cver', this.context.client.clientVersion); url_components.searchParams.set('cver', this.context.client.clientVersion);
url_components.searchParams.set('ratebypass', 'yes'); url_components.searchParams.set('ratebypass', 'yes');
url_components.searchParams.set('n', this.player.encodeN(url_components.searchParams.get('n'))); url_components.searchParams.set('n', new NToken(this.player.ntoken_sc).transform(url_components.searchParams.get('n')));
format.url = url_components.toString(); format.url = url_components.toString();
} }
@@ -262,7 +374,12 @@ class Innertube extends EventEmitter {
} }
if (options.type == 'videoandaudio') { if (options.type == 'videoandaudio') {
const response = await Axios.get(selected_format.url, { cancelToken: new CancelToken(function executor(c) { cancel = c; }), responseType: 'stream', reponseEncoding: 'binary', headers: Constants.stream_headers() }).catch((error) => error); const response = await Axios.get(selected_format.url, {
responseType: 'stream',
cancelToken: new CancelToken(function executor(c) { cancel = c; }),
headers: Constants.stream_headers()
}).catch((error) => error);
if (response instanceof Error) { if (response instanceof Error) {
stream.emit('error', { message: response.message, type: 'REQUEST_FAILED' }); stream.emit('error', { message: response.message, type: 'REQUEST_FAILED' });
return stream; return stream;
@@ -286,18 +403,26 @@ class Innertube extends EventEmitter {
} }
}); });
response.data.pipe(stream, true); response.data.pipe(stream, { end: true });
} else { } else {
const chunk_size = 1048576 * 10; // 10MB const chunk_size = 1048576 * 10; // 10MB
let chunk_start = 0; let chunk_start = 0;
let chunk_end = chunk_size; let chunk_end = chunk_size;
let downloaded_size = 0; let downloaded_size = 0;
let end = false;
stream.emit('start'); stream.emit('start');
const downloadChunk = async () => { const downloadChunk = async () => {
const response = await Axios.get(selected_format.url, { cancelToken: new CancelToken(function executor(c) { cancel = c; }), responseType: 'stream', headers: Constants.stream_headers(`bytes=${chunk_start}-${chunk_end || ''}`) }).catch((error) => error); if (chunk_end >= selected_format.contentLength) end = true;
const response = await Axios.get(`${selected_format.url}&range=${chunk_start}-${chunk_end || ''}`, {
responseType: 'stream',
cancelToken: new CancelToken(function executor(c) { cancel = c; }),
headers: Constants.stream_headers()
}).catch((error) => error);
if (response instanceof Error) { if (response instanceof Error) {
stream.emit('error', { message: response.message, type: 'REQUEST_FAILED' }); stream.emit('error', { message: response.message, type: 'REQUEST_FAILED' });
return stream; return stream;
@@ -310,17 +435,6 @@ class Innertube extends EventEmitter {
stream.emit('progress', { chunk_size: chunk.length, downloaded_size: (downloaded_size / 1024 / 1024).toFixed(2), percentage, size, raw_data: { chunk_size: chunk.length, downloaded: downloaded_size, size: response.headers['content-length'] } }); stream.emit('progress', { chunk_size: chunk.length, downloaded_size: (downloaded_size / 1024 / 1024).toFixed(2), percentage, size, raw_data: { chunk_size: chunk.length, downloaded: downloaded_size, size: response.headers['content-length'] } });
}); });
response.data.on('end', () => {
chunk_start = chunk_end + 1;
chunk_end += chunk_size;
if (downloaded_size < selected_format.contentLength) {
downloadChunk();
} else {
stream.emit('end');
}
});
response.data.on('error', (err) => { response.data.on('error', (err) => {
if (cancelled) { if (cancelled) {
stream.emit('error', { message: 'The download was cancelled.', type: 'DOWNLOAD_CANCELLED' }); stream.emit('error', { message: 'The download was cancelled.', type: 'DOWNLOAD_CANCELLED' });
@@ -329,7 +443,15 @@ class Innertube extends EventEmitter {
} }
}); });
response.data.pipe(stream, { end: false }); response.data.on('end', () => {
if (!end) {
chunk_start = chunk_end + 1;
chunk_end += chunk_size;
downloadChunk();
}
});
response.data.pipe(stream, { end });
}; };
downloadChunk(); downloadChunk();
} }
@@ -339,6 +461,7 @@ class Innertube extends EventEmitter {
cancelled = true; cancelled = true;
cancel(); cancel();
}; };
return stream; return stream;
} }
} }

132
lib/Livechat.js Normal file
View File

@@ -0,0 +1,132 @@
'use strict';
const Axios = require('axios');
const Actions = require('./Actions');
const Constants = require('./Constants');
const EventEmitter = require('events');
class Livechat extends EventEmitter {
constructor(session, token, channel_id, video_id) {
super(session);
this.ctoken = token;
this.session = session;
this.video_id = video_id;
this.channel_id = channel_id;
this.message_queue = [];
this.id_cache = [];
this.poll_intervals_ms = 1000;
this.running = true;
this.poll();
}
enqueueActionGroup(group) {
group.forEach((action) => {
if (!action.addChatItemAction) return; //TODO: handle different action types
const message_content = action.addChatItemAction.item.liveChatTextMessageRenderer;
if (!message_content) return;
const message = {
text: message_content.message.runs.map((item) => item.text).join(' '),
author: {
name: message_content.authorName && message_content.authorName.simpleText || 'N/',
channel_id: message_content.authorExternalChannelId,
profile_picture: message_content.authorPhoto.thumbnails
},
timestamp: message_content.timestampUsec,
id: message_content.id
};
this.message_queue.push(message);
});
}
async poll() {
if (!this.running) return;
let data;
data = { context: this.session.context, continuation: this.ctoken };
const livechat = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/live_chat/get_live_chat${this.session.logged_in && this.session.cookie.length < 1 ? '' : `?key=${this.session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session: this.session, data, desktop: true }));
if (livechat instanceof Error) throw new Error(`Error polling livechat: ${livechat.message}`);
const continuation_contents = livechat.data.continuationContents;
const action_group = continuation_contents.liveChatContinuation.actions;
this.enqueueActionGroup(action_group);
// Why don't we just emit the message directly? Well, enqueueing the messages is necessary so they are not emitted in a “messy” way, funny enough that's exactly how YouTube does it in its livechat js player.
this.message_queue.forEach((message, index) => {
if (this.id_cache.includes(message.id)) return;
setTimeout(() => this.emit('chat-update', message), message.timestamp / 1000 - new Date().getTime());
this.id_cache.push(message.id);
});
this.message_queue = [];
data = { context: this.session.context, videoId: this.video_id };
if (this.metadata_ctoken) data.continuation = this.metadata_ctoken;
const updated_metadata = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/updated_metadata${this.session.logged_in && this.session.cookie.length < 1 ? '' : `?key=${this.session.key}`}`, JSON.stringify(data), Constants.innertube_request_opts({ session: this.session, data, desktop: true }));
if (updated_metadata instanceof Error) throw new Error(`Error polling updated metadata: ${updated_metadata.message}`);
this.metadata_ctoken = updated_metadata.data.continuation.timedContinuationData.continuation;
const metadata = updated_metadata.data.actions;
this.emit('update-metadata', {
likes: metadata[1].updateToggleButtonTextAction.defaultText.simpleText,
dislikes: metadata[2].updateToggleButtonTextAction.defaultText.simpleText,
view_count: {
simple_text: metadata[0].updateViewershipAction.viewCount.videoViewCountRenderer.viewCount.simpleText,
short_view_count: metadata[0].updateViewershipAction.viewCount.videoViewCountRenderer.extraShortViewCount.simpleText
}
});
this.livechat_poller = setTimeout(async () => await this.poll(), this.poll_intervals_ms);
}
async sendMessage(text) {
const message = await Actions.livechat(this.session, 'live_chat/send_message', { text, channel_id: this.channel_id, video_id: this.video_id });
if (!message.success) return message;
const deleteMessage = async () => {
const menu = await Actions.livechat(this.session, 'live_chat/get_item_context_menu', { params: { params: message.data.actions[0].addChatItemAction.item.liveChatTextMessageRenderer.contextMenuEndpoint.liveChatItemContextMenuEndpoint.params, pbj: 1 } });
if (!menu.success) return menu;
const chat_item_menu = menu.data.liveChatItemContextMenuSupportedRenderers.menuRenderer.items[0];
const cmd = await Actions.livechat(this.session, 'live_chat/moderate', { cmd_params: chat_item_menu.menuServiceItemRenderer.serviceEndpoint.moderateLiveChatEndpoint.params });
if (!cmd.success) return cmd;
return { success: true, status_code: cmd.status_code };
};
return {
success: true,
status_code: message.status_code,
deleteMessage,
message_data: {
text: message.data.actions[0].addChatItemAction.item.liveChatTextMessageRenderer.message.runs.map((item) => item.text).join(' '),
author: {
name: message.data.actions[0].addChatItemAction.item.liveChatTextMessageRenderer.authorName && message.data.actions[0].addChatItemAction.item.liveChatTextMessageRenderer.authorName.simpleText || 'N/',
channel_id: message.data.actions[0].addChatItemAction.item.liveChatTextMessageRenderer.authorExternalChannelId,
profile_picture: message.data.actions[0].addChatItemAction.item.liveChatTextMessageRenderer.authorPhoto.thumbnails
},
timestamp: message.data.actions[0].addChatItemAction.item.liveChatTextMessageRenderer.timestampUsec,
id: message.data.actions[0].addChatItemAction.item.liveChatTextMessageRenderer.id
}
};
}
async blockUser(msg_params) {
/* TODO: Implement this */
throw new Error('Not implemented');
}
stop() {
this.running = false;
clearTimeout(this.livechat_poller);
}
}
module.exports = Livechat;

129
lib/NToken.js Normal file
View File

@@ -0,0 +1,129 @@
'use strict';
const Utils = require('./Utils');
const Constants = require('./Constants');
class NToken {
constructor(raw_code) {
this.raw_code = raw_code;
this.null_placeholder_regex = /c\[(.*?)\]=c/g;
this.transformation_args_regex = /c\[(.*?)\]\((.+?)\)/g;
}
transform(n) {
let n_token = n.split('');
let transformations = this.getTransformationData(this.raw_code);
// Identifies the necessary transformation functions and emulates them accordingly.
transformations = transformations.map((el) => {
if (el != null && typeof el != 'number') {
const is_reverse_base64 = el.includes('case 65:');
if (el.includes('function(d){for(var')) {
el = (arr) => this.pushSplice(arr);
} else if (el.includes('d.push(e)')) {
el = (arr, item) => this.push(arr, item);
} else if (el.includes('d.reverse()')) {
el = (arr) => this.reverse(arr);
} else if (el.includes('d.length;d.splice(e,1)')) {
el = (arr, index) => this.spliceOnce(arr, index);
} else if (el.includes('d[0])[0])')) {
el = (arr, index) => this.spliceTwice(arr, index);
} else if (el.includes('reverse().forEach')) {
el = (arr, index) => this.spliceReverseUnshift(arr, index);
} else if (el.includes('f=d[0];d[0]')) {
el = (arr, index) => this.swapFirstItem(arr, index);
} else if (el.includes('unshift(d.pop())')) {
el = (arr, index) => this.unshiftPop(arr, index);
} else if (el.includes('switch')) {
el = (arr, e) => this.translateAB(arr, e, is_reverse_base64);
} else if (el === 'b') {
el = n_token;
}
}
return el;
});
// Fills the null placeholders with a copy of the transformations array.
let null_placeholder_positions = [...this.raw_code.matchAll(this.null_placeholder_regex)].map((item) => parseInt(item[1]));
null_placeholder_positions.forEach((pos) => transformations[pos] = transformations);
// Parses and emulates calls to functions of the transformations array.
let transformation_args = [...Utils.getStringBetweenStrings(this.raw_code.replace(/\n/g, ''), 'try{', '}catch').matchAll(this.transformation_args_regex)].map((params) => ({ index: params[1], params: params[2] }));
transformation_args.forEach((data) => {
const index = data.index;
const param_index = data.params.split(',').map((param) => param.match(/c\[(.*?)\]/)[1]);
transformations[index](transformations[param_index[0]], transformations[param_index[1]]);
});
return n_token.join('');
}
getTransformationData() {
let transformation_data = '[' + Utils.getStringBetweenStrings(this.raw_code, 'c=[', '];c') + ']';
transformation_data = transformation_data
.replace(/function\(d,e\)/g, '"function(d,e)')
.replace(/function\(d\)/g, '"function(d)')
.replace(/,b,/g, ',"b",')
.replace(/,b/g, ',"b"')
.replace(/b,/g, '"b",')
.replace(/b]/g, '"b"]')
.replace(/},/g, '}",')
.replace(/""/g, '')
.replace(/length]\)}"/g, 'length])}');
return JSON.parse(transformation_data);
}
translateAB(arr, e, is_reverse_base64) {
let characters = is_reverse_base64 && Constants.base64_alphabet.reverse || Constants.base64_alphabet.normal;
arr.forEach(function(char, index, loc) {
this.push(loc[index] = characters[(characters.indexOf(char) - characters.indexOf(this[index]) + 64) % characters.length]);
}, e.split(''));
}
unshiftPop(arr, index) {
index = (index % arr.length + arr.length) % arr.length;
for (; index--;) {
arr.unshift(arr.pop());
}
}
swapFirstItem(arr, index) {
let oldValue = arr[0];
index = (index % arr.length + arr.length) % arr.length;
arr[0] = arr[index];
arr[index] = oldValue;
}
spliceReverseUnshift(arr, index) {
index = (index % arr.length + arr.length) % arr.length;
arr.splice(-index).reverse().forEach(function(f) {
arr.unshift(f);
});
}
spliceOnce(arr, index) {
index = (index % arr.length + arr.length) % arr.length;
arr.splice(index, 1);
}
spliceTwice(arr, index) {
index = (index % arr.length + arr.length) % arr.length;
arr.splice(0, 1, arr.splice(index, 1, arr[0])[0]);
}
pushSplice(arr) {
for (let index = arr.length; index;)
arr.push(arr.splice(--index, 1)[0]);
}
push(arr, item) {
arr.push(item);
}
reverse(arr) {
arr.reverse();
}
}
module.exports = NToken;

View File

@@ -7,49 +7,49 @@ const EventEmitter = require('events');
const Uuid = require("uuid"); const Uuid = require("uuid");
class OAuth extends EventEmitter { class OAuth extends EventEmitter {
constructor (creds) { constructor(creds) {
super(); super();
// Default interval between requests when waiting for authorization. // Default interval between requests when waiting for authorization.
this.refresh_interval = 5; this.refresh_interval = 5;
// OAuth URLs: // OAuth URLs:
this.oauth_code_url = `${Constants.urls.YT_BASE_URL}/o/oauth2/device/code`; this.oauth_code_url = `${Constants.urls.YT_BASE_URL}/o/oauth2/device/code`;
this.oauth_token_url = `${Constants.urls.YT_BASE_URL}/o/oauth2/token`; this.oauth_token_url = `${Constants.urls.YT_BASE_URL}/o/oauth2/token`;
// Used to check whether an access token is valid or not. // Used to check whether an access token is valid or not.
this.guide_url = `${Constants.urls.YT_BASE_URL}/youtubei/v1/guide`; this.guide_url = `${Constants.urls.YT_BASE_URL}/youtubei/v1/guide`;
// These are always the same, so we shouldn't have any problems for now. // These are always the same, so we shouldn't have any problems for now.
this.model_name = Constants.oauth.model_name; this.model_name = Constants.oauth.model_name;
this.grant_type = Constants.oauth.grant_type; this.grant_type = Constants.oauth.grant_type;
this.scope = Constants.oauth.scope; this.scope = Constants.oauth.scope;
// Script that contains important information such as client id and client secret. // Script that contains important information such as client id and client secret.
this.auth_script_regex = /<script id=\"base-js\" src=\"(.*?)\" nonce=".*?"><\/script>/; this.auth_script_regex = /<script id=\"base-js\" src=\"(.*?)\" nonce=".*?"><\/script>/;
// Used to find the credentials inside the script. // Used to find the credentials inside the script.
this.identity_regex = /var .+?=\"(?<id>.+?)\",[.|\s].?=\"(?<secret>.+?)\"/; this.identity_regex = /var .+?=\"(?<id>.+?)\",[.|\s].?=\"(?<secret>.+?)\"/;
if (creds.access_token != undefined && creds.refresh_token != undefined) return; if (creds.access_token != undefined && creds.refresh_token != undefined) return;
this.requestAuthCode(); this.requestAuthCode();
} }
async waitForAuth(device_code) { waitForAuth(device_code) {
const data = { const data = {
client_id: this.client_id, client_id: this.client_id,
client_secret: this.client_secret, client_secret: this.client_secret,
code: device_code, code: device_code,
grant_type: this.grant_type grant_type: this.grant_type
}; };
setTimeout(async () => { setTimeout(async () => {
const response = await Axios.post(this.oauth_token_url, JSON.stringify(data), Constants.oauth_reqopts).catch((error) => error); const response = await Axios.post(this.oauth_token_url, JSON.stringify(data), Constants.oauth_reqopts).catch((error) => error);
if (response instanceof Error) if (response instanceof Error)
return this.emit('auth', { return this.emit('auth', {
error: 'Could not get auth token.', error: 'Could not get auth token.',
status: 'FAILED' status: 'FAILED'
}); });
if (response.data.error) { if (response.data.error) {
switch (response.data.error) { switch (response.data.error) {
case 'slow_down': case 'slow_down':
@@ -73,7 +73,7 @@ class OAuth extends EventEmitter {
} }
} else { } else {
this.emit('auth', { this.emit('auth', {
access_token: response.data.access_token, access_token: response.data.access_token,
refresh_token: response.data.refresh_token, refresh_token: response.data.refresh_token,
token_type: response.data.token_type, token_type: response.data.token_type,
expires: response.data.expires_in, expires: response.data.expires_in,
@@ -83,86 +83,86 @@ class OAuth extends EventEmitter {
} }
}, 1000 * this.refresh_interval); }, 1000 * this.refresh_interval);
} }
async requestAuthCode() { async requestAuthCode() {
const identity = await this.getClientIdentity(); const identity = await this.getClientIdentity();
this.client_id = identity.id; this.client_id = identity.id;
this.client_secret = identity.secret; this.client_secret = identity.secret;
const data = { const data = {
client_id: this.client_id, client_id: this.client_id,
scope: this.scope, scope: this.scope,
device_id : Uuid.v4(), device_id: Uuid.v4(),
model_name: this.model_name model_name: this.model_name
}; };
const response = await Axios.post(this.oauth_code_url, JSON.stringify(data), Constants.oauth_reqopts).catch((error) => error); const response = await Axios.post(this.oauth_code_url, JSON.stringify(data), Constants.oauth_reqopts).catch((error) => error);
if (response instanceof Error) if (response instanceof Error)
return this.emit('auth', { return this.emit('auth', {
error: 'Could not get auth code.', error: 'Could not get auth code.',
status: 'FAILED' status: 'FAILED'
}); });
this.emit('auth', { this.emit('auth', {
code: response.data.user_code, code: response.data.user_code,
status: 'AUTHORIZATION_PENDING', status: 'AUTHORIZATION_PENDING',
expires_in: response.data.expires_in, expires_in: response.data.expires_in,
verification_url: response.data.verification_url verification_url: response.data.verification_url
}); });
this.refresh_interval = response.data.interval; this.refresh_interval = response.data.interval;
// Keeps requesting at a specific rate until the authorization is granted or denied. // Keeps requesting at a specific rate until the authorization is granted or denied.
this.waitForAuth(response.data.device_code); this.waitForAuth(response.data.device_code);
} }
async getClientIdentity() { async getClientIdentity() {
// The first request is made to get the auth script url, hard-coding it isn't viable as it changes overtime. // The first request is made to get the auth script url, hard-coding it isn't viable as it changes overtime.
const yttv_response = await Axios.get(`${Constants.urls.YT_BASE_URL}/tv`, Constants.oauth_reqopts).catch((error) => error); const yttv_response = await Axios.get(`${Constants.urls.YT_BASE_URL}/tv`, Constants.oauth_reqopts).catch((error) => error);
if (yttv_response instanceof Error) throw new Error(`Could not get identify: ${yttv_response.message}`); if (yttv_response instanceof Error) throw new Error(`Could not get identify: ${yttv_response.message}`);
// Here we get the script and extract the necessary data to proceed with the auth flow. // Here we get the script and extract the necessary data to proceed with the auth flow.
const url_body = this.auth_script_regex.exec(yttv_response.data)[1]; const url_body = this.auth_script_regex.exec(yttv_response.data)[1];
const script_url = `${Constants.urls.YT_BASE_URL}/${url_body}`; const script_url = `${Constants.urls.YT_BASE_URL}/${url_body}`;
const response = await Axios.get(script_url, Constants.default_headers).catch((error) => error); const response = await Axios.get(script_url, Constants.default_headers).catch((error) => error);
if (response instanceof Error) throw new Error(`Could not fetch data from auth script: ${response.message}`); if (response instanceof Error) throw new Error(`Could not fetch data from auth script: ${response.message}`);
const identify_function = Utils.getStringBetweenStrings(response.data, '=function(){var a=window.environment', '(function()'); const identity_function = Utils.getStringBetweenStrings(response.data, '=function(){var a=window.environment', '(function()');
const client_identity = identify_function.match(this.identity_regex).groups; const client_identity = identity_function.match(this.identity_regex).groups;
return client_identity; return client_identity;
} }
async refreshAccessToken (refresh_token) { async refreshAccessToken(refresh_token) {
const identity = await this.getClientIdentity(); const identity = await this.getClientIdentity();
const data = { const data = {
client_id: identity.id, client_id: identity.id,
client_secret: identity.secret, client_secret: identity.secret,
refresh_token, refresh_token,
grant_type : 'refresh_token', grant_type: 'refresh_token',
}; };
const response = await Axios.post(this.oauth_token_url, JSON.stringify(data), Constants.oauth_reqopts).catch((error) => error); const response = await Axios.post(this.oauth_token_url, JSON.stringify(data), Constants.oauth_reqopts).catch((error) => error);
if (response instanceof Error) if (response instanceof Error)
return this.emit('refresh-token', { return this.emit('refresh-token', {
error: 'Could not refresh token.', error: 'Could not refresh token.',
status: 'FAILED' status: 'FAILED'
}); });
this.emit('refresh-token', { this.emit('refresh-token', {
access_token: response.data.access_token, access_token: response.data.access_token,
token_type: response.data.token_type, token_type: response.data.token_type,
expires: response.data.expires_in, expires: response.data.expires_in,
scope: response.data.scope, scope: response.data.scope,
status: 'SUCCESS' status: 'SUCCESS'
}); });
} }
async checkTokenValidity(access_token, session) { async checkTokenValidity(access_token, session) {
let headers = Constants.innertube_request_opts({ session }).headers; let headers = Constants.innertube_request_opts({ session }).headers;
headers.authorization = `Bearer ${access_token}`; headers.authorization = `Bearer ${access_token}`;
const response = await Axios.post(this.guide_url, JSON.stringify({ context : session.context }), { headers }).catch((error) => error); const response = await Axios.post(this.guide_url, JSON.stringify({ context: session.context }), { headers }).catch((error) => error);
if (response instanceof Error) return 'INVALID'; if (response instanceof Error) return 'INVALID';
return 'VALID'; return 'VALID';
} }

View File

@@ -13,16 +13,16 @@ class Player {
} }
async init() { async init() {
if (fs.existsSync(this.tmp_cache_dir + '/' + this.player_name + '.js')) { if (fs.existsSync(`${this.tmp_cache_dir}/${this.player_name}.js`)) {
const player_data = fs.readFileSync(this.tmp_cache_dir + '/' + this.player_name + '.js').toString(); const player_data = fs.readFileSync(`${this.tmp_cache_dir}/${this.player_name}.js`).toString();
this.getSigDecipherCode(player_data); this.getSigDecipherCode(player_data);
this.getNEncoder(player_data); this.getNEncoder(player_data);
} else { } else {
const response = await Axios.get(Constants.urls.YT_BASE_URL + this.session.player_url, { path: this.session.playerUrl, headers: { 'content-type': 'text/javascript', 'user-agent': Utils.getRandomUserAgent('desktop').userAgent } }).catch((error) => error); const response = await Axios.get(`${Constants.urls.YT_BASE_URL}${this.session.player_url}`, { path: this.session.playerUrl, headers: { 'content-type': 'text/javascript', 'user-agent': Utils.getRandomUserAgent('desktop').userAgent } }).catch((error) => error);
if (response instanceof Error) throw new Error('Could not get player data: ' + response.message); if (response instanceof Error) throw new Error('Could not get player data: ' + response.message);
fs.mkdirSync(this.tmp_cache_dir, { recursive: true }); fs.mkdirSync(this.tmp_cache_dir, { recursive: true });
fs.writeFileSync(this.tmp_cache_dir + '/' + this.player_name + '.js', response.data); fs.writeFileSync(`${this.tmp_cache_dir}/${this.player_name}.js`, response.data);
this.getSigDecipherCode(response.data); this.getSigDecipherCode(response.data);
this.getNEncoder(response.data); this.getNEncoder(response.data);
@@ -30,14 +30,13 @@ class Player {
} }
getSigDecipherCode(data) { getSigDecipherCode(data) {
const actions_algorithm_code = Utils.getStringBetweenStrings(data, 'this.audioTracks};var', '};'); const manipulation_algorithm_code = Utils.getStringBetweenStrings(data, 'this.audioTracks};var', '};');
const actions_sequence_code = Utils.getStringBetweenStrings(data, 'function(a){a=a.split("")', 'return a.join("")}'); const manipulation_sequence_code = Utils.getStringBetweenStrings(data, 'function(a){a=a.split("")', 'return a.join("")}');
this.sig_decipher_sc = actions_algorithm_code + actions_sequence_code; this.sig_decipher_sc = manipulation_algorithm_code + manipulation_sequence_code;
} }
getNEncoder(data) { getNEncoder(data) {
const raw_code = 'var b=a.split("")' + Utils.getStringBetweenStrings(data, 'b=a.split("")', '}return b.join("")}') + '} return b.join("");'; this.ntoken_sc = 'var b=a.split("")' + Utils.getStringBetweenStrings(data, 'b=a.split("")', '}return b.join("")}') + '} return b.join("");';
this.encodeN = Utils.createFunction('a', raw_code);
} }
} }

View File

@@ -1,13 +1,13 @@
'use strict'; 'use strict';
const NToken = require('./NToken');
const QueryString = require('querystring'); const QueryString = require('querystring');
class SigDecipher { class SigDecipher {
constructor(url, cver, func_code, encode_n) { constructor(url, cver, player) {
this.url = url; this.url = url;
this.cver = cver; this.cver = cver;
this.func_code = func_code; this.player = player;
this.encode_n = encode_n;
this.func_regex = /(.{2}):function\(.*?\){(.*?)}/g; this.func_regex = /(.{2}):function\(.*?\){(.*?)}/g;
this.actions_regex = /;.{2}\.(.{2})\(.*?,(.*?)\)/g; this.actions_regex = /;.{2}\.(.{2})\(.*?,(.*?)\)/g;
} }
@@ -33,7 +33,7 @@ class SigDecipher {
let actions; let actions;
let signature = args.s.split(''); let signature = args.s.split('');
while ((actions = this.actions_regex.exec(this.func_code)) !== null) { while ((actions = this.actions_regex.exec(this.player.sig_decipher_sc)) !== null) {
switch (actions[1]) { switch (actions[1]) {
case functions[0]: case functions[0]:
reverse(signature, actions[2]); reverse(signature, actions[2]);
@@ -49,10 +49,11 @@ class SigDecipher {
} }
const url_components = new URL(args.url); const url_components = new URL(args.url);
args.sp !== undefined ? url_components.searchParams.set(args.sp, signature.join('')) : url_components.searchParams.set('signature', signature.join('')); args.sp !== undefined ? url_components.searchParams.set(args.sp, signature.join('')) : url_components.searchParams.set('signature', signature.join(''));
url_components.searchParams.set('cver', this.cver); url_components.searchParams.set('cver', this.cver);
url_components.searchParams.set('ratebypass', 'yes'); url_components.searchParams.set('ratebypass', 'yes');
url_components.searchParams.set('n', this.encode_n(url_components.searchParams.get('n'))); url_components.searchParams.set('n', new NToken(this.player.ntoken_sc).transform(url_components.searchParams.get('n')));
return url_components.toString(); return url_components.toString();
} }
@@ -60,7 +61,7 @@ class SigDecipher {
let func; let func;
let func_name = []; let func_name = [];
while ((func = this.func_regex.exec(this.func_code)) !== null) { while ((func = this.func_regex.exec(this.player.sig_decipher_sc)) !== null) {
if (func[0].includes('reverse()')) { if (func[0].includes('reverse()')) {
func_name[0] = func[1]; func_name[0] = func[1];
} else if (func[0].includes('splice')) { } else if (func[0].includes('splice')) {

View File

@@ -1,5 +1,7 @@
'use strict'; 'use strict';
const Fs = require('fs');
const Proto = require('protons');
const Crypto = require('crypto'); const Crypto = require('crypto');
const UserAgent = require('user-agents'); const UserAgent = require('user-agents');
@@ -35,12 +37,50 @@ function escapeStringRegexp(string) {
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d"); return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
} }
function createFunction(input, raw_code) { // I hate this function encodeNotificationPref(channel_id, index) {
return new Function(input, raw_code); const youtube_proto = Proto(Fs.readFileSync(`${__dirname}/proto/youtube.proto`));
const buf = youtube_proto.NotificationPreferences.encode({
channel_id,
pref_id: {
index
},
number_0: 0,
number_1: 4
});
return encodeURIComponent(Buffer.from(buf).toString('base64'));
} }
function encodeId(id) { function generateMessageParams(channel_id, video_id) {
return encodeURI(new Buffer.from(` ` + id + `*`).toString('base64').replace('==', '') + 'BQBw=='); const youtube_proto = Proto(Fs.readFileSync(`${__dirname}/proto/youtube.proto`));
const buf = youtube_proto.LiveMessageParams.encode({
params: {
ids: {
channel_id,
video_id
}
},
number_0: 1,
number_1: 4
});
return Buffer.from(encodeURIComponent(Buffer.from(buf).toString('base64'))).toString('base64');
} }
module.exports = { getRandomUserAgent, generateSidAuth, getStringBetweenStrings, createFunction, encodeId }; function generateCommentParams(video_id) {
const youtube_proto = Proto(Fs.readFileSync(`${__dirname}/proto/youtube.proto`));
const buf = youtube_proto.CreateCommentParams.encode({
video_id,
params: {
index: 0
},
number: 7
});
return encodeURIComponent(Buffer.from(buf).toString('base64'));
}
module.exports = { getRandomUserAgent, generateSidAuth, getStringBetweenStrings, generateMessageParams, generateCommentParams, encodeNotificationPref };

34
lib/proto/youtube.proto Normal file
View File

@@ -0,0 +1,34 @@
syntax = "proto2";
package proto;
message NotificationPreferences {
string channel_id = 1;
message Preference {
int32 index = 1;
}
Preference pref_id = 2;
int32 number_0 = 3;
int32 number_1 = 4;
}
message LiveMessageParams {
message Params {
message Ids {
string channel_id = 1;
string video_id = 2;
}
Ids ids = 5;
}
Params params = 1;
int32 number_0 = 2;
int32 number_1 = 3;
}
message CreateCommentParams {
string video_id = 2;
message Params {
int32 index = 1;
}
Params params = 5;
int32 number = 10;
}

103
package-lock.json generated
View File

@@ -1,15 +1,16 @@
{ {
"name": "youtubei.js", "name": "youtubei.js",
"version": "1.0.5", "version": "1.2.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "youtubei.js", "name": "youtubei.js",
"version": "1.0.5", "version": "1.2.1",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"axios": "^0.21.4", "axios": "^0.21.4",
"protons": "^2.0.3",
"time-to-seconds": "^1.1.5", "time-to-seconds": "^1.1.5",
"user-agents": "^1.0.778", "user-agents": "^1.0.778",
"uuid": "^8.3.2" "uuid": "^8.3.2"
@@ -76,6 +77,35 @@
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz", "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
"integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=" "integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8="
}, },
"node_modules/multiformats": {
"version": "9.4.9",
"resolved": "https://registry.npmjs.org/multiformats/-/multiformats-9.4.9.tgz",
"integrity": "sha512-zA84TTJcRfRMpjvYqy63piBbSEdqlIGqNNSpP6kspqtougqjo60PRhIFo+oAxrjkof14WMCImvr7acK6rPpXLw=="
},
"node_modules/protocol-buffers-schema": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/protocol-buffers-schema/-/protocol-buffers-schema-3.6.0.tgz",
"integrity": "sha512-TdDRD+/QNdrCGCE7v8340QyuXd4kIWIgapsE2+n/SaGiSSbomYl4TjHlvIoCWRpE7wFt02EpB35VVA2ImcBVqw=="
},
"node_modules/protons": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/protons/-/protons-2.0.3.tgz",
"integrity": "sha512-j6JikP/H7gNybNinZhAHMN07Vjr1i4lVupg598l4I9gSTjJqOvKnwjzYX2PzvBTSVf2eZ2nWv4vG+mtW8L6tpA==",
"dependencies": {
"protocol-buffers-schema": "^3.3.1",
"signed-varint": "^2.0.1",
"uint8arrays": "^3.0.0",
"varint": "^5.0.0"
}
},
"node_modules/signed-varint": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/signed-varint/-/signed-varint-2.0.1.tgz",
"integrity": "sha1-UKmYnafJjCxh2tEZvJdHDvhSgSk=",
"dependencies": {
"varint": "~5.0.0"
}
},
"node_modules/time-to-seconds": { "node_modules/time-to-seconds": {
"version": "1.1.5", "version": "1.1.5",
"resolved": "https://registry.npmjs.org/time-to-seconds/-/time-to-seconds-1.1.5.tgz", "resolved": "https://registry.npmjs.org/time-to-seconds/-/time-to-seconds-1.1.5.tgz",
@@ -85,6 +115,14 @@
"vscode": "^1.22.0" "vscode": "^1.22.0"
} }
}, },
"node_modules/uint8arrays": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/uint8arrays/-/uint8arrays-3.0.0.tgz",
"integrity": "sha512-HRCx0q6O9Bfbp+HHSfQQKD7wU70+lydKVt4EghkdOvlK/NlrF90z+eXV34mUd48rNvVJXwkrMSPpCATkct8fJA==",
"dependencies": {
"multiformats": "^9.4.2"
}
},
"node_modules/underscore": { "node_modules/underscore": {
"version": "1.13.1", "version": "1.13.1",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.1.tgz", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.1.tgz",
@@ -99,9 +137,9 @@
} }
}, },
"node_modules/user-agents": { "node_modules/user-agents": {
"version": "1.0.801", "version": "1.0.814",
"resolved": "https://registry.npmjs.org/user-agents/-/user-agents-1.0.801.tgz", "resolved": "https://registry.npmjs.org/user-agents/-/user-agents-1.0.814.tgz",
"integrity": "sha512-giB7GP2g71STtQaYbSDpd5T+XzbGr5ni+1NpEbeQnifnFiOIQeQonXOC2kDxGKvubzul6qQb/BwG9LlIQ1zxXA==", "integrity": "sha512-OU3lCkkaUCghnZiZuN2edSJ+//ituJihs3P1FCjCDwe1tMBS8eni68CQ0TBbPoVPDtoAqxM3Z7qbP7EWGlhBxA==",
"dependencies": { "dependencies": {
"dot-json": "^1.2.2", "dot-json": "^1.2.2",
"lodash.clonedeep": "^4.5.0" "lodash.clonedeep": "^4.5.0"
@@ -114,6 +152,11 @@
"bin": { "bin": {
"uuid": "dist/bin/uuid" "uuid": "dist/bin/uuid"
} }
},
"node_modules/varint": {
"version": "5.0.2",
"resolved": "https://registry.npmjs.org/varint/-/varint-5.0.2.tgz",
"integrity": "sha512-lKxKYG6H03yCZUpAGOPOsMcGxd1RHCu1iKvEHYDPmTyq2HueGhD73ssNBqqQWfvYs04G9iUFRvmAVLW20Jw6ow=="
} }
}, },
"dependencies": { "dependencies": {
@@ -155,11 +198,48 @@
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz", "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
"integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=" "integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8="
}, },
"multiformats": {
"version": "9.4.9",
"resolved": "https://registry.npmjs.org/multiformats/-/multiformats-9.4.9.tgz",
"integrity": "sha512-zA84TTJcRfRMpjvYqy63piBbSEdqlIGqNNSpP6kspqtougqjo60PRhIFo+oAxrjkof14WMCImvr7acK6rPpXLw=="
},
"protocol-buffers-schema": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/protocol-buffers-schema/-/protocol-buffers-schema-3.6.0.tgz",
"integrity": "sha512-TdDRD+/QNdrCGCE7v8340QyuXd4kIWIgapsE2+n/SaGiSSbomYl4TjHlvIoCWRpE7wFt02EpB35VVA2ImcBVqw=="
},
"protons": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/protons/-/protons-2.0.3.tgz",
"integrity": "sha512-j6JikP/H7gNybNinZhAHMN07Vjr1i4lVupg598l4I9gSTjJqOvKnwjzYX2PzvBTSVf2eZ2nWv4vG+mtW8L6tpA==",
"requires": {
"protocol-buffers-schema": "^3.3.1",
"signed-varint": "^2.0.1",
"uint8arrays": "^3.0.0",
"varint": "^5.0.0"
}
},
"signed-varint": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/signed-varint/-/signed-varint-2.0.1.tgz",
"integrity": "sha1-UKmYnafJjCxh2tEZvJdHDvhSgSk=",
"requires": {
"varint": "~5.0.0"
}
},
"time-to-seconds": { "time-to-seconds": {
"version": "1.1.5", "version": "1.1.5",
"resolved": "https://registry.npmjs.org/time-to-seconds/-/time-to-seconds-1.1.5.tgz", "resolved": "https://registry.npmjs.org/time-to-seconds/-/time-to-seconds-1.1.5.tgz",
"integrity": "sha512-mpzJDHGF4VdhiahyusCUSy+BWJdN3q8Cluzfy0n7GMU9IIj+HJDX9bbbr7wVSUiqmRn1vqhhfECgdfj+SByu2A==" "integrity": "sha512-mpzJDHGF4VdhiahyusCUSy+BWJdN3q8Cluzfy0n7GMU9IIj+HJDX9bbbr7wVSUiqmRn1vqhhfECgdfj+SByu2A=="
}, },
"uint8arrays": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/uint8arrays/-/uint8arrays-3.0.0.tgz",
"integrity": "sha512-HRCx0q6O9Bfbp+HHSfQQKD7wU70+lydKVt4EghkdOvlK/NlrF90z+eXV34mUd48rNvVJXwkrMSPpCATkct8fJA==",
"requires": {
"multiformats": "^9.4.2"
}
},
"underscore": { "underscore": {
"version": "1.13.1", "version": "1.13.1",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.1.tgz", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.1.tgz",
@@ -174,9 +254,9 @@
} }
}, },
"user-agents": { "user-agents": {
"version": "1.0.801", "version": "1.0.814",
"resolved": "https://registry.npmjs.org/user-agents/-/user-agents-1.0.801.tgz", "resolved": "https://registry.npmjs.org/user-agents/-/user-agents-1.0.814.tgz",
"integrity": "sha512-giB7GP2g71STtQaYbSDpd5T+XzbGr5ni+1NpEbeQnifnFiOIQeQonXOC2kDxGKvubzul6qQb/BwG9LlIQ1zxXA==", "integrity": "sha512-OU3lCkkaUCghnZiZuN2edSJ+//ituJihs3P1FCjCDwe1tMBS8eni68CQ0TBbPoVPDtoAqxM3Z7qbP7EWGlhBxA==",
"requires": { "requires": {
"dot-json": "^1.2.2", "dot-json": "^1.2.2",
"lodash.clonedeep": "^4.5.0" "lodash.clonedeep": "^4.5.0"
@@ -186,6 +266,11 @@
"version": "8.3.2", "version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
},
"varint": {
"version": "5.0.2",
"resolved": "https://registry.npmjs.org/varint/-/varint-5.0.2.tgz",
"integrity": "sha512-lKxKYG6H03yCZUpAGOPOsMcGxd1RHCu1iKvEHYDPmTyq2HueGhD73ssNBqqQWfvYs04G9iUFRvmAVLW20Jw6ow=="
} }
} }
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "youtubei.js", "name": "youtubei.js",
"version": "1.0.5", "version": "1.2.1",
"description": "An object-oriented library that allows you to search, get detailed info about videos, subscribe, unsubscribe, like, dislike, comment, download videos and much more!", "description": "An object-oriented library that allows you to search, get detailed info about videos, subscribe, unsubscribe, like, dislike, comment, download videos and much more!",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@@ -14,6 +14,7 @@
}, },
"dependencies": { "dependencies": {
"axios": "^0.21.4", "axios": "^0.21.4",
"protons": "^2.0.3",
"time-to-seconds": "^1.1.5", "time-to-seconds": "^1.1.5",
"user-agents": "^1.0.778", "user-agents": "^1.0.778",
"uuid": "^8.3.2" "uuid": "^8.3.2"
@@ -27,17 +28,19 @@
"youtube-dl", "youtube-dl",
"innertube", "innertube",
"innertubeapi", "innertubeapi",
"livechat",
"api", "api",
"search", "search",
"like", "like",
"dislike", "dislike",
"comment", "comment",
"downloader",
"automation", "automation",
"downloader",
"comments-section",
"youtube-downloader" "youtube-downloader"
], ],
"bugs": { "bugs": {
"url": "https://github.com/LuanRT/YouTube.js/issues" "url": "https://github.com/LuanRT/YouTube.js/issues"
}, },
"homepage": "https://github.com/LuanRT/YouTube.js#readme" "homepage": "https://github.com/LuanRT/YouTube.js#readme"
} }