mirror of
https://codeberg.org/ashley/poke
synced 2025-06-03 04:59:41 +00:00
add useragent : p1
This commit is contained in:
parent
906f7b237e
commit
b5e09f60eb
@ -12,20 +12,7 @@ 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")
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class representing PokeTube's core functionality.
|
|
||||||
*/
|
|
||||||
class InnerTubePokeVidious {
|
class InnerTubePokeVidious {
|
||||||
/**
|
|
||||||
* Create an instance of InnerTubePokeVidious.
|
|
||||||
* @param {object} config - Configuration object for InnerTubePokeVidious.
|
|
||||||
* @param {string} config.tubeApi - Tube API URL.
|
|
||||||
* @param {string} config.invapi - Invid API URL.
|
|
||||||
* @param {string} config.invapi_alt - Invid API URL - ALT .
|
|
||||||
* @param {string} config.dislikes - Dislikes API URL.
|
|
||||||
* @param {string} config.t_url - Matomo URL.
|
|
||||||
*/
|
|
||||||
constructor(config) {
|
constructor(config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.cache = {};
|
this.cache = {};
|
||||||
@ -36,17 +23,12 @@ class InnerTubePokeVidious {
|
|||||||
this.ANDROID_API_KEY = "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w"
|
this.ANDROID_API_KEY = "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w"
|
||||||
this.ANDROID_APP_VERSION = "19.14.42"
|
this.ANDROID_APP_VERSION = "19.14.42"
|
||||||
this.ANDROID_VERSION = "13"
|
this.ANDROID_VERSION = "13"
|
||||||
this.useragent = "com.google.android.youtube/19.14.42 (Linux; U; Android 12; US) gzip"
|
this.useragent = config.useragent || "PokeTube/2.0.0 (GNU/Linux; Android 14; Trisquel 11; poketube-vidious; like FreeTube)"
|
||||||
this.INNERTUBE_CONTEXT_CLIENT_VERSION = "1"
|
this.INNERTUBE_CONTEXT_CLIENT_VERSION = "1"
|
||||||
this.region = "region=US";
|
this.region = "region=US";
|
||||||
this.sqp = "-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw";
|
this.sqp = "-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch JSON from API response.
|
|
||||||
* @param {string} str - String response from the API.
|
|
||||||
* @returns {object|null} Parsed JSON object or null if parsing failed.
|
|
||||||
*/
|
|
||||||
getJson(str) {
|
getJson(str) {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(str);
|
return JSON.parse(str);
|
||||||
@ -55,92 +37,68 @@ class InnerTubePokeVidious {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the provided object has the required properties.
|
|
||||||
* @param {object} obj - Object to check.
|
|
||||||
* @returns {boolean} True if the object has the required properties, false otherwise.
|
|
||||||
*/
|
|
||||||
checkUnexistingObject(obj) {
|
checkUnexistingObject(obj) {
|
||||||
if (obj) {
|
return obj && "authorId" in obj;
|
||||||
if ("authorId" in obj) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch video information.
|
|
||||||
* @param {string} v - Video ID.
|
|
||||||
* @returns {Promise<object>} Promise resolving to the video information.
|
|
||||||
*/
|
|
||||||
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";
|
||||||
|
|
||||||
// Check if result is already cached
|
|
||||||
if (this.cache[v] && Date.now() - this.cache[v].timestamp < 3600000) {
|
if (this.cache[v] && Date.now() - this.cache[v].timestamp < 3600000) {
|
||||||
return this.cache[v].result;
|
return this.cache[v].result;
|
||||||
}
|
}
|
||||||
const headers = {};
|
|
||||||
|
|
||||||
let desc = "";
|
const headers = {
|
||||||
|
"User-Agent": this.useragent,
|
||||||
|
};
|
||||||
|
|
||||||
const fetchWithRetry = async (url, options = {}, retries = 3) => {
|
const fetchWithRetry = async (url, options = {}, retries = 3) => {
|
||||||
for (let attempt = 0; attempt < retries; attempt++) {
|
for (let attempt = 0; attempt < retries; attempt++) {
|
||||||
const res = await fetch(url, options);
|
const res = await fetch(url, {
|
||||||
if (res.status === 500 && attempt < retries - 1) {
|
...options,
|
||||||
continue; // retry on 500
|
headers: {
|
||||||
}
|
...options.headers,
|
||||||
|
...headers,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (res.status === 500 && attempt < retries - 1) continue;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
// If all retries fail, return last response
|
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [invComments, videoInfo, videoData] = await Promise.all([
|
const [invComments, videoInfo, videoData] = await Promise.all([
|
||||||
fetchWithRetry(`${this.config.invapi}/comments/${v}?hl=${contentlang}®ion=${contentregion}&h=${btoa(Date.now())}`).then((res) => res.text()),
|
fetchWithRetry(`${this.config.invapi}/comments/${v}?hl=${contentlang}®ion=${contentregion}&h=${btoa(Date.now())}`).then(res => res.text()),
|
||||||
fetchWithRetry(`${this.config.invapi}/videos/${v}?hl=${contentlang}®ion=${contentregion}&h=${btoa(Date.now())}`).then((res) => res.text()),
|
fetchWithRetry(`${this.config.invapi}/videos/${v}?hl=${contentlang}®ion=${contentregion}&h=${btoa(Date.now())}`).then(res => res.text()),
|
||||||
curly
|
curly.get(`${this.config.tubeApi}video?v=${v}`, {
|
||||||
.get(`${this.config.tubeApi}video?v=${v}`, {
|
|
||||||
httpHeader: Object.entries(headers).map(([k, v]) => `${k}: ${v}`),
|
httpHeader: Object.entries(headers).map(([k, v]) => `${k}: ${v}`),
|
||||||
})
|
}).then(res => {
|
||||||
.then((res) => {
|
|
||||||
const json = toJson(res.data);
|
const json = toJson(res.data);
|
||||||
const video = this.getJson(json);
|
const video = this.getJson(json);
|
||||||
return { json, video };
|
return { json, video };
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const comments = this.getJson(invComments);
|
||||||
|
const vid = this.getJson(videoInfo);
|
||||||
|
const { json, video } = videoData;
|
||||||
|
|
||||||
const comments = await this.getJson(invComments);
|
let p = {};
|
||||||
|
if (f === "true") {
|
||||||
|
const uploads = await fetchWithRetry(`${this.config.invapi}/channels/${vid.authorId}?hl=${contentlang}®ion=${contentregion}`);
|
||||||
|
p = this.getJson(await uploads.text());
|
||||||
|
}
|
||||||
|
|
||||||
const vid = await this.getJson(videoInfo);
|
if (!vid) {
|
||||||
const { json, video } = videoData;
|
console.log(`Sorry nya, we couldn't find any information about that video qwq`);
|
||||||
|
}
|
||||||
|
|
||||||
var channel_uploads = { };
|
if (this.checkUnexistingObject(vid)) {
|
||||||
if (f == "true") {
|
const fe = await getdislikes(v);
|
||||||
channel_uploads = await fetch(
|
|
||||||
`${this.config.invapi}/channels/${vid.authorId}?hl=${contentlang}®ion=${contentregion}`
|
|
||||||
);
|
|
||||||
var p = this.getJson(await channel_uploads.text());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!vid) {
|
|
||||||
console.log(
|
|
||||||
`Sorry nya, we couldn't find any information about that video qwq`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.checkUnexistingObject(vid)) {
|
|
||||||
const fe = await getdislikes(v);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const headers = {};
|
|
||||||
|
|
||||||
// Store result in cache
|
|
||||||
this.cache[v] = {
|
this.cache[v] = {
|
||||||
result: {
|
result: {
|
||||||
json: json?.video,
|
json: json?.video,
|
||||||
@ -151,59 +109,38 @@ class InnerTubePokeVidious {
|
|||||||
engagement: fe.engagement,
|
engagement: fe.engagement,
|
||||||
wiki: "",
|
wiki: "",
|
||||||
desc: "",
|
desc: "",
|
||||||
color: await getColors(
|
color: await getColors(`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}`).then(colors => colors[0].hex()),
|
||||||
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}`
|
color2: await getColors(`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}`).then(colors => colors[1].hex()),
|
||||||
).then((colors) => colors[0].hex()),
|
|
||||||
color2: await getColors(
|
|
||||||
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}`
|
|
||||||
).then((colors) => colors[1].hex()),
|
|
||||||
},
|
},
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.cache[v].result;
|
return this.cache[v].result;
|
||||||
} catch (error) {
|
|
||||||
this.initError("Error getting video", error);
|
|
||||||
}
|
}
|
||||||
}
|
} catch (error) {
|
||||||
} catch {
|
this.initError("Error getting video", error);
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a video ID is valid.
|
|
||||||
* @param {string} v - Video ID.
|
|
||||||
* @returns {boolean} True if the video ID is valid, false otherwise.
|
|
||||||
*/
|
|
||||||
isvalidvideo(v) {
|
|
||||||
if (v != "assets" && v != "cdn-cgi" && v != "404") {
|
|
||||||
const regex = new RegExp("^([a-zA-Z0-9_-]{11})");
|
|
||||||
const isMatch = regex.test(v);
|
|
||||||
return isMatch;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
isvalidvideo(v) {
|
||||||
* Initialize an error.
|
if (v != "assets" && v != "cdn-cgi" && v != "404") {
|
||||||
* @param {string} args - Error message.
|
return /^([a-zA-Z0-9_-]{11})/.test(v);
|
||||||
* @param {Error} error - Error object.
|
}
|
||||||
*/
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
initError(args, error) {
|
initError(args, error) {
|
||||||
console.error("[LIBPT CORE ERROR]" + args, error);
|
console.error("[LIBPT CORE ERROR] " + args, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create an instance of InnerTubePokeVidious with the provided config
|
|
||||||
const pokeTubeApiCore = new InnerTubePokeVidious({
|
const pokeTubeApiCore = 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: config.proxylocation === "EU" ? "https://invid-api.poketube.fun/api/v1" : "https://iv.ggtyler.dev/api/v1",
|
invapi_alt: config.proxylocation === "EU" ? "https://invid-api.poketube.fun/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,
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = pokeTubeApiCore;
|
module.exports = pokeTubeApiCore;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user