Update src/libpoketube/libpoketube-core.js

This commit is contained in:
ashley 2025-04-29 19:47:04 +00:00
parent 99f4d03bbb
commit c9e8b8f85d

View File

@ -11,41 +11,20 @@ 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"); // youtubei.js client const { Innertube } = require("youtubei.js");
class InnerTubePokeVidious { class InnerTubePokeVidious {
constructor(config) { constructor(config) {
this.config = config; this.config = config;
this.cache = {}; this.cache = {};
this.language = "hl=en-US"; this.language = "hl=en-US";
this.param = "2AMB";
this.param_legacy = "CgIIAdgDAQ%3D%3D";
this.apikey = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8";
this.ANDROID_API_KEY = "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w";
this.ANDROID_APP_VERSION = "19.14.42";
this.ANDROID_VERSION = "13";
this.useragent =
config.useragent ||
"PokeTube/2.0.0 (GNU/Linux; Android 14; Trisquel 11; poketube-vidious; like FreeTube)";
this.region = "region=US"; this.region = "region=US";
this.sqp = this.sqp = "-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw";
"-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw"; this.useragent = config.useragent ||
"PokeTube/2.0.0 (GNU/Linux; Android 14; Trisquel 11; poketube-vidious; like FreeTube)";
this.youtubeClient = null; this.youtubeClient = null;
} }
getJson(str) {
try {
return JSON.parse(str);
} catch {
return null;
}
}
checkUnexistingObject(obj) {
return obj && "authorId" in obj;
}
// Lazy-init the Innertube client
async _initYouTube() { async _initYouTube() {
if (!this.youtubeClient) { if (!this.youtubeClient) {
this.youtubeClient = await Innertube.create({ this.youtubeClient = await Innertube.create({
@ -56,93 +35,122 @@ class InnerTubePokeVidious {
return this.youtubeClient; return this.youtubeClient;
} }
async getYouTubeApiVideo(f, v, contentlang, contentregion) { // Helper to map thumbnails to {url,width,height}
const { fetch } = await import("undici"); _mapThumbnails(thumbnails) {
if (v == null) return "Gib ID"; return thumbnails.map((t) => ({
url: t.url,
width: t.width,
height: t.height,
}));
}
if (this.cache[v] && Date.now() - this.cache[v].timestamp < 3600000) { // The one unified method
return this.cache[v].result; async getYouTubeApiVideo(f, videoId, contentlang, contentregion) {
if (!videoId) return { error: "Gib ID" };
// cache for 1h
if (this.cache[videoId] &&
Date.now() - this.cache[videoId].timestamp < 3_600_000) {
return this.cache[videoId].result;
} }
const headers = { "User-Agent": this.useragent }; const headers = { "User-Agent": this.useragent };
const youtube = await this._initYouTube(); const youtube = await this._initYouTube();
try { try {
const [commentsResponse, info, videoData] = await Promise.all([ // fetch:
youtube.getComments(v), const [ commentsData, info, legacy ] = await Promise.all([
youtube.getInfo(v), youtube.getComments(videoId), // raw comments from yt
curly youtube.getInfo(videoId), // raw info from yt
.get(`${this.config.tubeApi}video?v=${v}`, { curly.get(`${this.config.tubeApi}video?v=${videoId}`, {
httpHeader: Object.entries(headers).map( httpHeader: Object.entries(headers).map(([k,v]) => `${k}: ${v}`)
([k, val]) => `${k}: ${val}` }).then(res => {
), const json = toJson(res.data);
}) return { json: JSON.parse(json), video: JSON.parse(json).video };
.then((res) => { }),
const json = toJson(res.data);
return { json, video: this.getJson(json) };
}),
]); ]);
const vid = { ...info, authorId: info.channel_id }; // Invidious-style JSON:
const vid = info;
const resp = {
type: vid.type,
title: vid.title,
videoId: vid.id,
error: vid.info?.reason || null,
let p = {}; videoThumbnails: this._mapThumbnails(vid.thumbnails),
if (f === "true") { storyboards: vid.storyboards?.map(sb => ({
const uploadsRes = await fetch( url: sb.url,
`${this.config.invapi}/channels/${vid.authorId}?hl=${contentlang}&region=${contentregion}`, width: sb.width,
{ headers } height: sb.height,
); mime: sb.mimeType
p = await uploadsRes.json(); })),
}
const fe = await getdislikes(v); description: vid.description,
descriptionHtml: vid.descriptionRenderers?.description,
published: Math.floor(new Date(vid.uploadDate).getTime() / 1000),
publishedText: vid.publishedText,
keywords: vid.keywords,
this.cache[v] = { viewCount: vid.viewCount,
result: { likeCount: vid.likes,
json: videoData.json, dislikeCount: 0,
video: videoData.video,
vid, paid: vid.isPaid,
comments: commentsResponse, premium: vid.isPremium,
channel_uploads: p, isFamilyFriendly: vid.isFamilySafe,
engagement: fe.engagement, allowedRegions: vid.availableCountries,
wiki: "", genre: vid.genre,
desc: info.description, genreUrl: vid.genreUrl,
color: await getColors(
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}` author: vid.author,
).then((cols) => cols[0].hex()), authorId: vid.channelId,
color2: await getColors( authorUrl: `/channel/${vid.channelId}`,
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}` authorVerified: vid.isVerified,
).then((cols) => cols[1].hex()), authorThumbnails: this._mapThumbnails(vid.authorThumbnails),
}, subCountText: vid.subscriberCountText,
timestamp: Date.now(),
lengthSeconds: vid.lengthSeconds,
allowRatings: vid.isRatingsEnabled,
rating: 0,
isListed: !vid.isUnlisted,
liveNow: vid.isLive,
isPostLiveDvr: vid.isPostLiveDvr,
isUpcoming: vid.upcoming,
premiereTimestamp: vid.premiereTimestamp
? Math.floor(new Date(vid.premiereTimestamp).getTime() / 1000)
: null,
// keep legacy fields too:
json: legacy.json,
video: legacy.video,
comments: commentsData,
engagement: (await getdislikes(videoId)).engagement,
wiki: "",
channel_uploads: f === "true"
? (await fetch(`${this.config.invapi}/channels/${vid.channelId}?hl=${contentlang}&region=${contentregion}`, { headers }))
.then(r => r.json())
: {}
}; };
return this.cache[v].result; this.cache[videoId] = { result: resp, timestamp: Date.now() };
} catch (error) { return resp;
this.initError("Error getting video", error);
} catch (err) {
console.error("[LIBPT CORE ERROR] Error getting video", err);
return { error: err.message };
} }
} }
isvalidvideo(v) {
return v !== "assets" && v !== "cdn-cgi" && v !== "404"
? /^([a-zA-Z0-9_-]{11})/.test(v)
: false;
}
initError(args, error) {
console.error("[LIBPT CORE ERROR] " + args, error);
}
} }
const pokeTubeApiCore = new InnerTubePokeVidious({ module.exports = new InnerTubePokeVidious({
tubeApi: "https://inner-api.poketube.fun/api/", tubeApi: "https://inner-api.poketube.fun/api/",
invapi: "https://invid-api.poketube.fun/bHj665PpYhUdPWuKPfZuQGoX/api/v1", invapi: "https://invid-api.poketube.fun/bHj665PpYhUdPWuKPfZuQGoX/api/v1",
invapi_alt: invapi_alt: config.proxylocation === "EU"
config.proxylocation === "EU" ? "https://invid-api.poketube.fun/api/v1"
? "https://invid-api.poketube.fun/api/v1" : "https://iv.ggtyler.dev/api/v1",
: "https://iv.ggtyler.dev/api/v1", dislikes: "https://returnyoutubedislikeapi.com/votes?videoId=",
dislikes: "https://returnyoutubedislikeapi.com/votes?videoId=", t_url: "https://t.poketube.fun/",
t_url: "https://t.poketube.fun/",
useragent: config.useragent, useragent: config.useragent,
}); });
module.exports = pokeTubeApiCore;