410 lines
12 KiB
JavaScript
410 lines
12 KiB
JavaScript
const {
|
|
fetcher,
|
|
INNERTUBE,
|
|
wiki,
|
|
musicInfo,
|
|
modules,
|
|
version,
|
|
initlog,
|
|
init,
|
|
} = require("../libpoketube-initsys.js");
|
|
const {
|
|
IsJsonString,
|
|
convert,
|
|
getFirstLine,
|
|
capitalizeFirstLetter,
|
|
turntomins,
|
|
getRandomInt,
|
|
escapeHtml,
|
|
getRandomArbitrary,
|
|
} = require("../ptutils/libpt-coreutils.js");
|
|
const media_proxy = require("../libpoketube-video.js");
|
|
const atmos = require("../../../pokeatmosurls.json");
|
|
const config = require("../../../config.json");
|
|
|
|
function linkify(text) {
|
|
// regular expression to match URLs
|
|
const urlRegex = /(https?:\/\/[^\s]+)/g;
|
|
|
|
return text.replace(urlRegex, (url) => {
|
|
// wrap the URL in an <a> tag with the URL as the href attribute
|
|
return `<a href="/api/redirect?u=${btoa(
|
|
url
|
|
.replace(/twitter\.com/g, "nitter.net")
|
|
.replace(/reddit\.com/g, "teddit.net")
|
|
.replace("https://youtube.com", "")
|
|
.replace("https://youtu.be", "")
|
|
)}" target="_blank">${url}</a>`;
|
|
});
|
|
}
|
|
|
|
function channelurlfixer(text) {
|
|
// Create a regular expression to match <a> tags with href containing "/channel/"
|
|
const regex = /<a\s+href="\/channel\/([^"]+)"/g;
|
|
// Replace matching <a> tags with the modified href attribute
|
|
const updatedDescription = text.replace(regex, '<a href="/channel?id=$1"');
|
|
return updatedDescription;
|
|
}
|
|
|
|
function endsWithYouTubeAutoGenerated(text) {
|
|
const target = " Auto-generated by YouTube. ";
|
|
return text.endsWith(target);
|
|
}
|
|
|
|
const sha384 = modules.hash;
|
|
const fetch = modules.fetch;
|
|
const htmlToText = require("html-to-text");
|
|
const encoding = require("encoding");
|
|
const delim1 =
|
|
'</div></div></div></div><div class="hwc"><div class="BNeawe tAd8D AP7Wnd"><div><div class="BNeawe tAd8D AP7Wnd">';
|
|
const delim2 =
|
|
'</div></div></div></div></div><div><span class="hwc"><div class="BNeawe uEec3 AP7Wnd">';
|
|
const url = "https://www.google.com/search?q=";
|
|
|
|
async function lyricsFinder(e = "", d = "") {
|
|
let i;
|
|
try {
|
|
i = await fetch(`${url}${encodeURIComponent(d + " " + e)}+lyrics`);
|
|
i = await i.textConverted();
|
|
[, i] = i.split(delim1);
|
|
[i] = i.split(delim2);
|
|
} catch (m) {
|
|
try {
|
|
i = await fetch(`${url}${encodeURIComponent(d + " " + e)}+song+lyrics`);
|
|
i = await i.textConverted();
|
|
[, i] = i.split(delim1);
|
|
[i] = i.split(delim2);
|
|
} catch (n) {
|
|
try {
|
|
i = await fetch(`${url}${encodeURIComponent(d + " " + e)}+song`);
|
|
i = await i.textConverted();
|
|
[, i] = i.split(delim1);
|
|
[i] = i.split(delim2);
|
|
} catch (o) {
|
|
try {
|
|
i = await fetch(`${url}${encodeURIComponent(d + " " + e)}`);
|
|
i = await i.textConverted();
|
|
[, i] = i.split(delim1);
|
|
[i] = i.split(delim2);
|
|
} catch (p) {
|
|
i = "";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
const ret = i.split("\n");
|
|
let final = "";
|
|
for (let j = 0; j < ret.length; j += 1) {
|
|
final = `${final}${htmlToText.fromString(ret[j])}\n`;
|
|
}
|
|
return String(encoding.convert(final)).trim();
|
|
}
|
|
|
|
function toObject(arr) {
|
|
var rv = {};
|
|
for (var i = 0; i < arr.length; ++i) if (arr[i] !== undefined) rv[i] = arr[i];
|
|
return rv;
|
|
}
|
|
|
|
function lightOrDark(color) {
|
|
// Variables for red, green, blue values
|
|
var r, g, b, hsp;
|
|
|
|
// Check the format of the color, HEX or RGB?
|
|
if (color.match(/^rgb/)) {
|
|
// If RGB --> store the red, green, blue values in separate variables
|
|
color = color.match(
|
|
/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/
|
|
);
|
|
|
|
r = color[1];
|
|
g = color[2];
|
|
b = color[3];
|
|
} else {
|
|
// If hex --> Convert it to RGB: http://gist.github.com/983661
|
|
color = +("0x" + color.slice(1).replace(color.length < 5 && /./g, "$&$&"));
|
|
|
|
r = color >> 16;
|
|
g = (color >> 8) & 255;
|
|
b = color & 255;
|
|
}
|
|
|
|
// HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html
|
|
hsp = Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b));
|
|
|
|
// Using the HSP value, determine whether the color is light or dark
|
|
if (hsp > 127.5) {
|
|
return "light";
|
|
} else {
|
|
return "dark";
|
|
}
|
|
}
|
|
|
|
function isDntEnabled(req) {
|
|
const dntHeader = req.header("DNT");
|
|
return dntHeader && (dntHeader === "1" || dntHeader === "true");
|
|
}
|
|
|
|
function IsInArray(array, id) {
|
|
for (var i = 0; i < array.length; i++) {
|
|
if (array[i].id === id) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function getJson(str) {
|
|
try {
|
|
return JSON.parse(str);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/* support button */
|
|
const PATREON_REGEX = /https:\/\/(?:www\.)?patreon.com\/(?<name>[\w\d_-]+)/;
|
|
|
|
/* connections */
|
|
const TWITTER_REGEX = /https:\/\/twitter.com\/(?<name>[\w\d_-]+)/;
|
|
const CORD_REGEX = /https:\/\/discord.gg\/(?<name>[\w\d_-]+)/;
|
|
const TWITCH_REGEX = /https:\/\/twitch.tv\/(?<name>[\w\d_-]+)/;
|
|
const REDDIT_REGEX = /https:\/\/reddit\.com\/r\/(?<name>[\w\d_-]+)/;
|
|
|
|
/* meta software */
|
|
const INSTAGRAM_REGEX = /https:\/\/www.instagram.com\/(?<name>[\w\d_-]+)/;
|
|
const THREADS_BY_INSTAGRAM_REGEX = /https:\/\/www.threads.net\/(?<name>[\w\d_-]+)/;
|
|
const FACEBOOK_REGEX = /https:\/\/www.facebook.com\/(?<name>[\w\d_-]+)/;
|
|
|
|
/* music */
|
|
const LNKTO_REGEX = /https:\/\/(?<subdomain>\w+).lnk.to\/(?<path>\S*)/;
|
|
|
|
module.exports = function (app, config, renderTemplate) {
|
|
app.get("/encryption", async function (req, res) {
|
|
res.json("error in parsing");
|
|
});
|
|
|
|
app.get("/old", function (req, res) {
|
|
const v = req.query.v;
|
|
|
|
renderTemplate(res, req, "poketube-old.ejs", {
|
|
v,
|
|
});
|
|
});
|
|
|
|
app.get("/watch", async (req, res) => {
|
|
const { dm, region, hl, v, e, r, f, m, quality: q, a, universe, dyx} = req.query;
|
|
|
|
if (!v) {
|
|
return res.redirect("/");
|
|
}
|
|
|
|
const regex = new RegExp("^([a-zA-Z0-9_-]{11})");
|
|
const isMatch = regex.test(v);
|
|
|
|
if (!isMatch) {
|
|
return res.redirect("/");
|
|
}
|
|
|
|
var contentlang = hl || "en-US";
|
|
var contentregion = region || "US";
|
|
|
|
const isVideoValid = await INNERTUBE.isvalidvideo(v);
|
|
if (!isVideoValid) {
|
|
return res.redirect("/?fromerror=21_video_not_valid");
|
|
}
|
|
|
|
const u = await media_proxy(v);
|
|
|
|
const secure = ["poketube.fun"].includes(req.hostname);
|
|
const verify = req.hostname === "poketube.sudovanilla.com";
|
|
|
|
|
|
if (req.hostname !== "poketube.fun") {
|
|
if (config.invapi ==="https://invid-api.poketube.fun/bHj665PpYhUdPWuKPfZuQGoX/api/v1") {
|
|
return res.status(500).send( "Error: please edit your config.json to match your own instance, and please use your own API URL. see docs.invidious.io to how to setup invidious!");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
INNERTUBE.getYouTubePlayerInfo(f, v, contentlang, contentregion).then(
|
|
(data) => {
|
|
try {
|
|
const channel_uploads = data?.channel_uploads;
|
|
const json = data?.json;
|
|
const engagement = data?.engagement;
|
|
const inv_comments = data?.comments || "Disabled";
|
|
const inv_vid = data?.vid;
|
|
const desc = data?.desc || "";
|
|
|
|
|
|
let d = false;
|
|
if (desc !== "[object Object]") {
|
|
d = desc.toString().replace(/\n/g, " <br> ");
|
|
}
|
|
|
|
const descriptionString = String(inv_vid?.description).replace(/\bx.com\b/, "twitter.com")
|
|
|
|
function extractInfo(regex) {
|
|
return descriptionString !== "[object Object]"
|
|
? (regex.exec(descriptionString) ?? {}).groups
|
|
: undefined;
|
|
}
|
|
|
|
const support = extractInfo(PATREON_REGEX);
|
|
const STUPID_ELON_MUSK_WEBSITE_HE_IS_TRYING_TO_CALL_IT_X_FOR_SOME_REASON_WHICH_IS_A_STUPID_NAME_WE_WILL_FOREVER_CALL_IT_TWITTER_AND_HE_CAN_DO_NOTHING_ABOUT_IT_LOL_FUCK_YOU_ELON_TRANS_RIGHTS_BTW = extractInfo(TWITTER_REGEX);
|
|
const linkto = extractInfo(LNKTO_REGEX);
|
|
const discord = extractInfo(CORD_REGEX);
|
|
const twitch = extractInfo(TWITCH_REGEX);
|
|
const reddit = extractInfo(REDDIT_REGEX);
|
|
|
|
/* meta software */
|
|
const instagram = extractInfo(INSTAGRAM_REGEX);
|
|
const threads_by_instagram = extractInfo(THREADS_BY_INSTAGRAM_REGEX);
|
|
|
|
const videoObject = inv_vid?.adaptiveFormats;
|
|
function findItag(adaptiveFormats) {
|
|
return;
|
|
}
|
|
|
|
const itag_hd = findItag(videoObject);
|
|
var proxyurl = config.p_url;
|
|
var vidurl = u.url;
|
|
var isvidious = u.isInvidiousURL;
|
|
var mediaproxy = config.media_proxy;
|
|
|
|
if (inv_vid?.genre === "Music") {
|
|
var vidurl = u.losslessurl;
|
|
}
|
|
|
|
var vidurl = config.videourl;
|
|
var isvidious = true;
|
|
|
|
if (req.useragent.source.includes("Pardus")) {
|
|
var vidurl = "https://iv.ggtyler.dev";
|
|
var mediaproxy = "https://nyc1.pokejan.ggtyler.dev/";
|
|
var isvidious = true;
|
|
var isSchoolProxy = "";
|
|
}
|
|
|
|
// unused
|
|
let badges = "";
|
|
let comments = "";
|
|
let nnn = "";
|
|
|
|
const dnt_val = isDntEnabled(req);
|
|
|
|
if (
|
|
inv_vid?.error ===
|
|
"The uploader has not made this video available in your country" ||
|
|
inv_vid?.error === "This video is not available"
|
|
) {
|
|
res.send(
|
|
"error: " + inv_vid.error + " please refresh the page please qt"
|
|
);
|
|
}
|
|
|
|
if (inv_vid?.error) {
|
|
renderTemplate(res, req, "404.ejs", {
|
|
v,
|
|
});
|
|
}
|
|
|
|
var uaos = req.useragent.os;
|
|
const browser = req.useragent.browser;
|
|
const IsOldWindows =
|
|
(uaos === "Windows 7" || uaos === "Windows 8") &&
|
|
browser === "Firefox";
|
|
|
|
if (req.query.from === "short") var shortsui = true;
|
|
|
|
try {
|
|
|
|
res.header('X-Robots-Tag', 'noindex, nofollow');
|
|
renderTemplate(res, req, "watch.ejs", {
|
|
color: data?.color,
|
|
color2: data?.color2,
|
|
linkify,
|
|
engagement,
|
|
linkto,
|
|
IsOldWindows,
|
|
channelurlfixer,
|
|
itag_hd,
|
|
support,
|
|
dyx,
|
|
shortsui,
|
|
u: vidurl,
|
|
isvidious: isvidious,
|
|
video: json,
|
|
date: inv_vid?.publishedText ,
|
|
e,
|
|
a,
|
|
twitter:STUPID_ELON_MUSK_WEBSITE_HE_IS_TRYING_TO_CALL_IT_X_FOR_SOME_REASON_WHICH_IS_A_STUPID_NAME_WE_WILL_FOREVER_CALL_IT_TWITTER_AND_HE_CAN_DO_NOTHING_ABOUT_IT_LOL_FUCK_YOU_ELON_TRANS_RIGHTS_BTW,
|
|
dm,
|
|
proxyurl,
|
|
media_proxy_url: mediaproxy,
|
|
instagram,
|
|
useragent: req.useragent,
|
|
config,
|
|
verify,
|
|
discord,
|
|
turntomins,
|
|
twitch,
|
|
dnt_val,
|
|
reddit,
|
|
channel_uploads,
|
|
secure,
|
|
process,
|
|
VideoData:inv_vid,
|
|
isSchoolProxy,
|
|
sha384,
|
|
lightOrDark,
|
|
isMobile: req.useragent.isMobile,
|
|
tj: data?.channel,
|
|
r,
|
|
threads:threads_by_instagram,
|
|
hostname:req.hostname,
|
|
qua: q,
|
|
inv: inv_comments,
|
|
convert,
|
|
universe,
|
|
wiki: data.wiki,
|
|
escapeHtml,
|
|
f,
|
|
t: config.t_url,
|
|
optout: m,
|
|
badges,
|
|
desc,
|
|
comments,
|
|
n: nnn,
|
|
inv_vid,
|
|
lyrics: "",
|
|
});
|
|
} catch (err) {
|
|
res.status(500);
|
|
res.header('X-Robots-Tag', 'noindex, nofollow');
|
|
renderTemplate(res, req, "video-error.ejs", {
|
|
v,
|
|
err_reason:err
|
|
});
|
|
console.log(err)
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
return res.redirect(`/watch?v=${req.query.v}&fx=1&err=${error}`);
|
|
}
|
|
}
|
|
);
|
|
});
|
|
|
|
app.get("/lite", async (req, res) => {
|
|
const { dm, region, hl, v, e, r, f, m, quality: q, a, universe, } = req.query;
|
|
|
|
renderTemplate(res, req, "lite.ejs", {
|
|
videoid:v,
|
|
});
|
|
|
|
});
|
|
|
|
};
|