Update src/libpoketube/libpoketube-core.js

This commit is contained in:
ashley 2025-04-29 19:39:14 +00:00
parent efd3c59df7
commit 99f4d03bbb

View File

@ -11,7 +11,7 @@ const { curly } = require("node-libcurl");
const getdislikes = require("../libpoketube/libpoketube-dislikes.js"); const getdislikes = require("../libpoketube/libpoketube-dislikes.js");
const getColors = require("get-image-colors"); const getColors = require("get-image-colors");
const config = require("../../config.json"); const config = require("../../config.json");
const { Innertube } = require("youtubei.js"); const { Innertube } = require("youtubei.js"); // youtubei.js client
class InnerTubePokeVidious { class InnerTubePokeVidious {
constructor(config) { constructor(config) {
@ -27,7 +27,6 @@ class InnerTubePokeVidious {
this.useragent = this.useragent =
config.useragent || config.useragent ||
"PokeTube/2.0.0 (GNU/Linux; Android 14; Trisquel 11; poketube-vidious; like FreeTube)"; "PokeTube/2.0.0 (GNU/Linux; Android 14; Trisquel 11; poketube-vidious; like FreeTube)";
this.INNERTUBE_CONTEXT_CLIENT_VERSION = "1";
this.region = "region=US"; this.region = "region=US";
this.sqp = this.sqp =
"-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw"; "-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw";
@ -46,11 +45,12 @@ class InnerTubePokeVidious {
return obj && "authorId" in obj; return obj && "authorId" in obj;
} }
async _initYouTube() { // Lazy-init the Innertube client
async _initYouTube() {
if (!this.youtubeClient) { if (!this.youtubeClient) {
this.youtubeClient = await Innertube.create({ this.youtubeClient = await Innertube.create({
lang: this.language.split("=")[1], // e.g. "en-US" lang: this.language.split("=")[1], // "en-US"
location: this.region.split("=")[1], // e.g. "US" location: this.region.split("=")[1], // "US"
}); });
} }
return this.youtubeClient; return this.youtubeClient;
@ -58,7 +58,6 @@ class InnerTubePokeVidious {
async getYouTubeApiVideo(f, v, contentlang, contentregion) { async getYouTubeApiVideo(f, v, contentlang, contentregion) {
const { fetch } = await import("undici"); const { fetch } = await import("undici");
if (v == null) return "Gib ID"; if (v == null) return "Gib ID";
if (this.cache[v] && Date.now() - this.cache[v].timestamp < 3600000) { if (this.cache[v] && Date.now() - this.cache[v].timestamp < 3600000) {
@ -66,15 +65,12 @@ class InnerTubePokeVidious {
} }
const headers = { "User-Agent": this.useragent }; const headers = { "User-Agent": this.useragent };
// init youtubei.js
const youtube = await this._initYouTube(); const youtube = await this._initYouTube();
try { try {
// fetch comments and details via youtubei.js, and legacy videoData via CURL const [commentsResponse, info, videoData] = await Promise.all([
const [commentsResponse, details, videoData] = await Promise.all([ youtube.getComments(v),
youtube.getComments(v), youtube.getInfo(v),
youtube.getDetails(v),
curly curly
.get(`${this.config.tubeApi}video?v=${v}`, { .get(`${this.config.tubeApi}video?v=${v}`, {
httpHeader: Object.entries(headers).map( httpHeader: Object.entries(headers).map(
@ -83,13 +79,11 @@ class InnerTubePokeVidious {
}) })
.then((res) => { .then((res) => {
const json = toJson(res.data); const json = toJson(res.data);
const video = this.getJson(json); return { json, video: this.getJson(json) };
return { json, video };
}), }),
]); ]);
// adapt to legacy fields const vid = { ...info, authorId: info.channel_id };
const vid = { ...details, authorId: details.channel_id };
let p = {}; let p = {};
if (f === "true") { if (f === "true") {
@ -111,7 +105,7 @@ class InnerTubePokeVidious {
channel_uploads: p, channel_uploads: p,
engagement: fe.engagement, engagement: fe.engagement,
wiki: "", wiki: "",
desc: details.description, desc: info.description,
color: await getColors( color: await getColors(
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}` `https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}`
).then((cols) => cols[0].hex()), ).then((cols) => cols[0].hex()),
@ -129,10 +123,9 @@ class InnerTubePokeVidious {
} }
isvalidvideo(v) { isvalidvideo(v) {
if (v != "assets" && v != "cdn-cgi" && v != "404") { return v !== "assets" && v !== "cdn-cgi" && v !== "404"
return /^([a-zA-Z0-9_-]{11})/.test(v); ? /^([a-zA-Z0-9_-]{11})/.test(v)
} : false;
return false;
} }
initError(args, error) { initError(args, error) {