2022-03-16 09:07:40 +00:00
|
|
|
// Code specific to working with the innertube API
|
|
|
|
// https://www.youtube.com/youtubei/v1
|
|
|
|
|
2022-03-21 23:47:11 +00:00
|
|
|
import { Http } from "@capacitor-community/http";
|
2022-04-09 03:06:35 +00:00
|
|
|
import { getBetweenStrings, delay } from "./utils";
|
2022-04-04 11:33:16 +00:00
|
|
|
import rendererUtils from "./renderers";
|
2022-03-21 23:47:11 +00:00
|
|
|
import constants from "./constants";
|
2022-03-16 09:07:40 +00:00
|
|
|
|
|
|
|
class Innertube {
|
2022-03-21 23:47:11 +00:00
|
|
|
//--- Initiation ---//
|
|
|
|
|
|
|
|
constructor(ErrorCallback) {
|
|
|
|
this.ErrorCallback = ErrorCallback || undefined;
|
|
|
|
this.retry_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkErrorCallback() {
|
|
|
|
return typeof this.ErrorCallback === "function";
|
|
|
|
}
|
|
|
|
|
|
|
|
async initAsync() {
|
|
|
|
const html = await Http.get({
|
|
|
|
url: constants.URLS.YT_URL,
|
|
|
|
params: { hl: "en" },
|
|
|
|
}).catch((error) => error);
|
|
|
|
try {
|
|
|
|
if (html instanceof Error && this.checkErrorCallback)
|
|
|
|
this.ErrorCallback(html.message, true);
|
|
|
|
try {
|
|
|
|
const data = JSON.parse(
|
|
|
|
getBetweenStrings(html.data, "ytcfg.set(", ");")
|
|
|
|
);
|
|
|
|
if (data.INNERTUBE_CONTEXT) {
|
|
|
|
this.key = data.INNERTUBE_API_KEY;
|
|
|
|
this.context = data.INNERTUBE_CONTEXT;
|
|
|
|
this.logged_in = data.LOGGED_IN;
|
|
|
|
|
|
|
|
this.context.client = constants.INNERTUBE_CLIENT(this.context.client);
|
|
|
|
this.header = constants.INNERTUBE_HEADER(this.context.client);
|
2022-03-16 09:07:40 +00:00
|
|
|
}
|
2022-03-21 23:47:11 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
2022-04-09 02:28:08 +00:00
|
|
|
if (this.checkErrorCallback) {
|
|
|
|
this.ErrorCallback(html.data, true);
|
|
|
|
this.ErrorCallback(err, true);
|
|
|
|
}
|
2022-04-09 03:06:35 +00:00
|
|
|
if (this.retry_count < 10) {
|
|
|
|
this.retry_count += 1;
|
|
|
|
if (this.checkErrorCallback)
|
|
|
|
this.ErrorCallback(
|
|
|
|
`retry count: ${this.retry_count}`,
|
|
|
|
false,
|
|
|
|
`An error occurred while trying to init the innertube API. Retrial number: ${this.retry_count}/10`
|
|
|
|
);
|
|
|
|
await delay(5000);
|
|
|
|
await this.initAsync();
|
2022-03-21 23:47:11 +00:00
|
|
|
} else {
|
|
|
|
if (this.checkErrorCallback)
|
2022-04-09 03:06:35 +00:00
|
|
|
this.ErrorCallback(
|
|
|
|
"Failed to retrieve Innertube session",
|
|
|
|
true,
|
|
|
|
"An error occurred while retrieving the innertube session. Check the Logs for more information."
|
|
|
|
);
|
2022-03-18 11:50:44 +00:00
|
|
|
}
|
2022-03-21 23:47:11 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.ErrorCallback(error, true);
|
2022-03-17 13:17:32 +00:00
|
|
|
}
|
2022-03-21 23:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static async createAsync(ErrorCallback) {
|
|
|
|
const created = new Innertube(ErrorCallback);
|
|
|
|
await created.initAsync();
|
|
|
|
return created;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--- API Calls ---//
|
|
|
|
|
2022-05-04 05:21:14 +00:00
|
|
|
async browseAsync(action_type, args) {
|
2022-03-21 23:47:11 +00:00
|
|
|
let data = { context: this.context };
|
|
|
|
|
|
|
|
switch (action_type) {
|
|
|
|
case "recommendations":
|
2022-05-04 05:21:14 +00:00
|
|
|
args.browseId = "FEwhat_to_watch";
|
2022-03-21 23:47:11 +00:00
|
|
|
break;
|
|
|
|
case "playlist":
|
2022-05-04 05:21:14 +00:00
|
|
|
case "channel":
|
|
|
|
if (args && args.browseId) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
throw new ReferenceError("No browseId provided");
|
|
|
|
}
|
2022-03-21 23:47:11 +00:00
|
|
|
default:
|
2022-03-16 09:07:40 +00:00
|
|
|
}
|
2022-05-04 05:21:14 +00:00
|
|
|
data.browseId = { ...data, args };
|
2022-03-16 09:07:40 +00:00
|
|
|
|
2022-03-21 23:47:11 +00:00
|
|
|
console.log(data);
|
|
|
|
|
|
|
|
const response = await Http.post({
|
|
|
|
url: `${constants.URLS.YT_BASE_API}/browse?key=${this.key}`,
|
|
|
|
data: data,
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
}).catch((error) => error);
|
|
|
|
|
|
|
|
if (response instanceof Error)
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
status_code: response.status,
|
|
|
|
message: response.message,
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
status_code: response.status,
|
|
|
|
data: response.data,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-04 11:33:16 +00:00
|
|
|
async getContinuationsAsync(continuation, type, contextAdditional = {}) {
|
|
|
|
let data = {
|
|
|
|
context: { ...this.context, ...contextAdditional },
|
|
|
|
continuation: continuation,
|
|
|
|
};
|
2022-04-01 01:02:08 +00:00
|
|
|
let url;
|
2022-04-19 14:03:46 +00:00
|
|
|
switch (type.toLowerCase()) {
|
2022-03-31 02:22:22 +00:00
|
|
|
case "browse":
|
|
|
|
url = `${constants.URLS.YT_BASE_API}/browse?key=${this.key}`;
|
2022-04-01 01:02:08 +00:00
|
|
|
break;
|
2022-03-31 02:22:22 +00:00
|
|
|
case "search":
|
|
|
|
url = `${constants.URLS.YT_BASE_API}/search?key=${this.key}`;
|
2022-04-01 01:02:08 +00:00
|
|
|
break;
|
2022-03-31 02:22:22 +00:00
|
|
|
case "next":
|
|
|
|
url = `${constants.URLS.YT_BASE_API}/next?key=${this.key}`;
|
2022-04-01 01:02:08 +00:00
|
|
|
break;
|
2022-03-31 02:22:22 +00:00
|
|
|
default:
|
2022-04-01 01:02:08 +00:00
|
|
|
throw "Invalid type";
|
2022-03-31 02:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const response = await Http.post({
|
|
|
|
url: url,
|
|
|
|
data: data,
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
}).catch((error) => error);
|
|
|
|
if (response instanceof Error) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
status_code: response.status,
|
|
|
|
message: response.message,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
status_code: response.status,
|
|
|
|
data: response.data,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-21 23:47:11 +00:00
|
|
|
async getVidAsync(id) {
|
|
|
|
let data = { context: this.context, videoId: id };
|
2022-03-22 23:07:01 +00:00
|
|
|
const responseNext = await Http.post({
|
2022-03-24 21:14:15 +00:00
|
|
|
url: `${constants.URLS.YT_BASE_API}/next?key=${this.key}`,
|
2022-04-04 11:33:16 +00:00
|
|
|
data: {
|
|
|
|
...data,
|
|
|
|
...{
|
|
|
|
context: {
|
|
|
|
client: {
|
2022-04-21 05:29:50 +00:00
|
|
|
clientName: constants.YT_API_VALUES.CLIENT_WEB_M,
|
2022-04-04 11:33:16 +00:00
|
|
|
clientVersion: constants.YT_API_VALUES.VERSION_WEB,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-03-22 23:07:01 +00:00
|
|
|
headers: constants.INNERTUBE_HEADER(this.context.client),
|
2022-03-21 23:47:11 +00:00
|
|
|
}).catch((error) => error);
|
|
|
|
|
2022-03-22 23:07:01 +00:00
|
|
|
const response = await Http.post({
|
2022-03-21 23:47:11 +00:00
|
|
|
url: `${constants.URLS.YT_BASE_API}/player?key=${this.key}`,
|
|
|
|
data: data,
|
2022-03-22 23:07:01 +00:00
|
|
|
headers: constants.INNERTUBE_HEADER(this.context.client),
|
2022-03-21 23:47:11 +00:00
|
|
|
}).catch((error) => error);
|
|
|
|
|
2022-03-23 13:07:03 +00:00
|
|
|
if (response.error)
|
2022-03-21 23:47:11 +00:00
|
|
|
return {
|
|
|
|
success: false,
|
2022-03-22 23:07:01 +00:00
|
|
|
status_code: response.status,
|
2022-03-21 23:47:11 +00:00
|
|
|
message: response.message,
|
2022-03-23 13:07:03 +00:00
|
|
|
};
|
|
|
|
else if (responseNext.error)
|
2022-03-22 23:07:01 +00:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
status_code: responseNext.status,
|
|
|
|
message: responseNext.message,
|
2022-03-23 13:07:03 +00:00
|
|
|
};
|
|
|
|
|
2022-03-21 23:47:11 +00:00
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
status_code: response.status,
|
2022-03-22 23:07:01 +00:00
|
|
|
data: { output: response.data, outputNext: responseNext.data },
|
2022-03-21 23:47:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:07:03 +00:00
|
|
|
async searchAsync(query) {
|
|
|
|
let data = { context: this.context, query: query };
|
|
|
|
|
|
|
|
const response = await Http.post({
|
|
|
|
url: `${constants.URLS.YT_BASE_API}/search?key=${this.key}`,
|
|
|
|
data: data,
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
}).catch((error) => error);
|
|
|
|
|
|
|
|
if (response instanceof Error)
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
status_code: response.status,
|
|
|
|
message: response.message,
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
status_code: response.status,
|
|
|
|
data: response.data,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-04 05:21:14 +00:00
|
|
|
async getEndPoint(url) {
|
|
|
|
let data = { context: this.context, url: url };
|
|
|
|
const response = await Http.post({
|
|
|
|
url: `${constants.URLS.YT_BASE_API}/navigation/resolve_url?key=${this.key}`,
|
|
|
|
data: data,
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
}).catch((error) => error);
|
|
|
|
|
|
|
|
if (response instanceof Error)
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
status_code: response.status,
|
|
|
|
message: response.message,
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
status_code: response.status,
|
|
|
|
data: response.data,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-29 07:02:52 +00:00
|
|
|
// WARNING: This is tracking the user's activity, but is required for recommendations to properly work
|
2022-04-01 13:04:52 +00:00
|
|
|
async apiStats(params, url) {
|
|
|
|
console.log(params);
|
|
|
|
await Http.get({
|
|
|
|
url: url,
|
2022-04-02 06:11:52 +00:00
|
|
|
params: {
|
|
|
|
...params,
|
|
|
|
...{
|
|
|
|
ver: 2,
|
|
|
|
c: constants.YT_API_VALUES.CLIENTNAME.toLowerCase(),
|
|
|
|
cbrver: constants.YT_API_VALUES.VERSION,
|
|
|
|
cver: constants.YT_API_VALUES.VERSION,
|
|
|
|
},
|
|
|
|
},
|
2022-04-01 13:04:52 +00:00
|
|
|
headers: this.header,
|
|
|
|
});
|
2022-03-29 07:02:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:07:03 +00:00
|
|
|
// Static methods
|
|
|
|
|
|
|
|
static getThumbnail(id, resolution) {
|
|
|
|
if (resolution == "max") {
|
|
|
|
const url = `https://img.youtube.com/vi/${id}/maxresdefault.jpg`;
|
|
|
|
let img = new Image();
|
|
|
|
img.src = url;
|
|
|
|
img.onload = function () {
|
|
|
|
if (img.height !== 120) return url;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return `https://img.youtube.com/vi/${id}/mqdefault.jpg`;
|
|
|
|
}
|
|
|
|
|
2022-03-21 23:47:11 +00:00
|
|
|
// Simple Wrappers
|
|
|
|
async getRecommendationsAsync() {
|
|
|
|
const rec = await this.browseAsync("recommendations");
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
2022-05-04 05:21:14 +00:00
|
|
|
async getChannelAsync(url) {
|
|
|
|
const channelEndpoint = await this.getEndPoint(url);
|
|
|
|
if (
|
|
|
|
channelEndpoint.success &&
|
|
|
|
channelEndpoint.data.endpoint?.browseEndpoint
|
|
|
|
) {
|
|
|
|
return await this.browseAsync(
|
|
|
|
"channel",
|
|
|
|
channelEndpoint.data.endpoint?.browseEndpoint
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw new ReferenceError("Cannot find channel");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 23:47:11 +00:00
|
|
|
async VidInfoAsync(id) {
|
|
|
|
let response = await this.getVidAsync(id);
|
|
|
|
|
|
|
|
if (
|
2022-03-22 23:07:01 +00:00
|
|
|
response.success == false ||
|
2022-03-23 13:07:03 +00:00
|
|
|
response.data.output?.playabilityStatus?.status == ("ERROR" || undefined)
|
2022-03-21 23:47:11 +00:00
|
|
|
)
|
|
|
|
throw new Error(
|
2022-04-01 01:02:08 +00:00
|
|
|
`Could not get information for video: ${
|
|
|
|
response.status_code ||
|
|
|
|
response.data.output?.playabilityStatus?.status
|
|
|
|
} - ${
|
|
|
|
response.message || response.data.output?.playabilityStatus?.reason
|
2022-03-23 13:07:03 +00:00
|
|
|
}`
|
2022-03-21 23:47:11 +00:00
|
|
|
);
|
2022-03-22 23:07:01 +00:00
|
|
|
const responseInfo = response.data.output;
|
|
|
|
const responseNext = response.data.outputNext;
|
|
|
|
const details = responseInfo.videoDetails;
|
|
|
|
// const columnUI =
|
|
|
|
// responseInfo[3].response?.contents.singleColumnWatchNextResults?.results
|
2022-03-23 13:07:03 +00:00
|
|
|
// ?.results;
|
2022-03-22 23:07:01 +00:00
|
|
|
const resolutions = responseInfo.streamingData;
|
2022-03-23 13:07:03 +00:00
|
|
|
const columnUI =
|
|
|
|
responseNext.contents.singleColumnWatchNextResults.results.results;
|
2022-04-04 11:33:16 +00:00
|
|
|
const vidMetadata = columnUI.contents.find(
|
|
|
|
(content) => content.slimVideoMetadataSectionRenderer
|
|
|
|
).slimVideoMetadataSectionRenderer;
|
|
|
|
|
|
|
|
const recommendations = columnUI?.contents.find(
|
2022-04-09 06:20:51 +00:00
|
|
|
(content) => content?.itemSectionRenderer?.targetId == "watch-next-feed"
|
2022-04-04 11:33:16 +00:00
|
|
|
).itemSectionRenderer;
|
|
|
|
|
|
|
|
const ownerData = vidMetadata.contents.find(
|
|
|
|
(content) => content.slimOwnerRenderer
|
|
|
|
)?.slimOwnerRenderer;
|
2022-03-22 23:07:01 +00:00
|
|
|
|
|
|
|
const vidData = {
|
2022-03-21 23:47:11 +00:00
|
|
|
id: details.videoId,
|
2022-03-22 23:07:01 +00:00
|
|
|
title: details.title,
|
|
|
|
isLive: details.isLiveContent,
|
|
|
|
channelName: details.author,
|
2022-04-04 11:33:16 +00:00
|
|
|
channelSubs: ownerData?.collapsedSubtitle?.runs[0]?.text,
|
2022-05-02 04:42:56 +00:00
|
|
|
channelUrl: rendererUtils.getNavigationEndpoints(
|
|
|
|
ownerData.navigationEndpoint
|
|
|
|
),
|
2022-04-04 11:33:16 +00:00
|
|
|
channelImg: ownerData?.thumbnail?.thumbnails[0].url,
|
2022-03-21 23:47:11 +00:00
|
|
|
availableResolutions: resolutions?.formats,
|
|
|
|
availableResolutionsAdaptive: resolutions?.adaptiveFormats,
|
|
|
|
metadata: {
|
2022-04-04 11:33:16 +00:00
|
|
|
contents: vidMetadata.contents,
|
2022-03-22 23:07:01 +00:00
|
|
|
description: details.shortDescription,
|
|
|
|
thumbnails: details.thumbnails?.thumbnails,
|
2022-03-21 23:47:11 +00:00
|
|
|
isPrivate: details.isPrivate,
|
2022-03-22 23:07:01 +00:00
|
|
|
viewCount: details.viewCount,
|
|
|
|
lengthSeconds: details.lengthSeconds,
|
2022-03-23 13:07:03 +00:00
|
|
|
likes: parseInt(
|
2022-04-04 11:33:16 +00:00
|
|
|
vidMetadata.contents
|
|
|
|
.find((content) => content.slimVideoActionBarRenderer)
|
|
|
|
.slimVideoActionBarRenderer.buttons.find(
|
|
|
|
(button) => button.slimMetadataToggleButtonRenderer.isLike
|
2022-03-23 13:07:03 +00:00
|
|
|
)
|
2022-04-04 11:33:16 +00:00
|
|
|
.slimMetadataToggleButtonRenderer.button.toggleButtonRenderer.defaultText.accessibility.accessibilityData.label.replace(
|
2022-03-23 13:07:03 +00:00
|
|
|
/\D/g,
|
|
|
|
""
|
|
|
|
)
|
|
|
|
), // Yes. I know.
|
2022-03-21 23:47:11 +00:00
|
|
|
},
|
|
|
|
renderedData: {
|
2022-04-04 11:33:16 +00:00
|
|
|
description: responseNext.engagementPanels
|
|
|
|
.find(
|
|
|
|
(panel) =>
|
|
|
|
panel.engagementPanelSectionListRenderer.panelIdentifier ==
|
|
|
|
"video-description-ep-identifier"
|
|
|
|
)
|
|
|
|
.engagementPanelSectionListRenderer.content.structuredDescriptionContentRenderer.items.find(
|
|
|
|
(item) => item.expandableVideoDescriptionBodyRenderer
|
|
|
|
).expandableVideoDescriptionBodyRenderer,
|
|
|
|
recommendations: recommendations,
|
2022-03-23 13:07:03 +00:00
|
|
|
recommendationsContinuation:
|
2022-04-04 11:33:16 +00:00
|
|
|
recommendations.contents[recommendations.contents.length - 1]
|
|
|
|
.continuationItemRenderer?.continuationEndpoint.continuationCommand
|
|
|
|
.token,
|
2022-03-21 23:47:11 +00:00
|
|
|
},
|
2022-04-07 07:32:24 +00:00
|
|
|
engagementPanels: responseNext.engagementPanels,
|
2022-04-09 06:20:51 +00:00
|
|
|
commentData: columnUI.contents
|
|
|
|
.find((content) => content.itemSectionRenderer?.contents)
|
|
|
|
?.itemSectionRenderer.contents.find(
|
|
|
|
(content) => content.commentsEntryPointHeaderRenderer
|
|
|
|
)?.commentsEntryPointHeaderRenderer,
|
2022-04-01 13:04:52 +00:00
|
|
|
playbackTracking: responseInfo.playbackTracking,
|
2022-04-19 14:03:46 +00:00
|
|
|
commentContinuation: responseNext.engagementPanels
|
|
|
|
.find(
|
|
|
|
(panel) =>
|
|
|
|
panel.engagementPanelSectionListRenderer.panelIdentifier ==
|
|
|
|
"engagement-panel-comments-section"
|
|
|
|
)
|
|
|
|
?.engagementPanelSectionListRenderer.content.sectionListRenderer.contents.find(
|
|
|
|
(content) => content.itemSectionRenderer
|
|
|
|
)
|
|
|
|
?.itemSectionRenderer.contents.find(
|
|
|
|
(content) => content.continuationItemRenderer
|
|
|
|
)?.continuationItemRenderer.continuationEndpoint.continuationCommand
|
|
|
|
.token,
|
2022-03-21 23:47:11 +00:00
|
|
|
};
|
2022-03-22 23:07:01 +00:00
|
|
|
|
2022-03-23 13:07:03 +00:00
|
|
|
return vidData;
|
2022-03-21 23:47:11 +00:00
|
|
|
}
|
2022-03-22 23:07:01 +00:00
|
|
|
|
2022-03-23 13:07:03 +00:00
|
|
|
async getSearchAsync(query) {
|
|
|
|
const search = await this.searchAsync(query);
|
|
|
|
if (search.success == false)
|
|
|
|
throw new Error(
|
|
|
|
`Could not get search results: ${search.status_code} - ${search.message}`
|
|
|
|
);
|
|
|
|
console.log(search.data);
|
2022-03-24 11:47:13 +00:00
|
|
|
return search.data;
|
2022-03-23 13:07:03 +00:00
|
|
|
}
|
2022-03-16 09:07:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 23:47:11 +00:00
|
|
|
export default Innertube;
|