Refactor code :3

This commit is contained in:
Ashley 2023-09-06 20:50:43 +00:00
parent a6116e6f9d
commit 7a28bf1e48

View file

@ -1,18 +1,40 @@
const { fetcher, core, wiki, musicInfo, modules, version, initlog, init, } = require("../libpoketube-initsys.js"); const {
const { IsJsonString, convert, getFirstLine, capitalizeFirstLetter, turntomins, getRandomInt, getRandomArbitrary} = require("../ptutils/libpt-coreutils.js"); fetcher,
core,
wiki,
musicInfo,
modules,
version,
initlog,
init,
} = require("../libpoketube-initsys.js");
const {
IsJsonString,
convert,
getFirstLine,
capitalizeFirstLetter,
turntomins,
getRandomInt,
getRandomArbitrary,
} = require("../ptutils/libpt-coreutils.js");
const media_proxy = require("../libpoketube-video.js"); const media_proxy = require("../libpoketube-video.js");
const atmos = require("../../../pokeatmosurls.json"); const atmos = require("../../../pokeatmosurls.json");
function linkify(text) { function linkify(text) {
// regular expression to match URLs // regular expression to match URLs
const urlRegex = /(https?:\/\/[^\s]+)/g; const urlRegex = /(https?:\/\/[^\s]+)/g;
return text.replace(urlRegex, (url) => { return text.replace(urlRegex, (url) => {
// wrap the URL in an <a> tag with the URL as the href attribute // 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>`; 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>`;
});
}
const sha384 = modules.hash; const sha384 = modules.hash;
const fetch = modules.fetch; const fetch = modules.fetch;
@ -118,23 +140,29 @@ function getJson(str) {
} }
} }
/* support button */
const PATREON_REGEX = /https:\/\/www.patreon.com\/(?<name>[\w\d_-]+)/; const PATREON_REGEX = /https:\/\/www.patreon.com\/(?<name>[\w\d_-]+)/;
/* connections */
const X_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_-]+)/;
const INSTAGRAM_REGEX = /https:\/\/www.instagram.com\/(?<name>[\w\d_-]+)/;
module.exports = function (app, config, renderTemplate) { module.exports = function (app, config, renderTemplate) {
app.get("/encryption", async function (req, res) { app.get("/encryption", async function (req, res) {
res.json("error in parsing");
res.json("error in parsing");
}); });
app.get("/old", function (req, res) { app.get("/old", function (req, res) {
const v = req.query.v; const v = req.query.v;
renderTemplate(res, req, "poketube-old.ejs", { renderTemplate(res, req, "poketube-old.ejs", {
v, v,
}); });
}); });
app.get("/watch", async (req, res) => { app.get("/watch", async (req, res) => {
const { dm, v, e, r, f, m, quality: q, a, universe } = req.query; const { dm, v, e, r, f, m, quality: q, a, universe } = req.query;
@ -149,13 +177,10 @@ module.exports = function (app, config, renderTemplate) {
const u = await media_proxy(v); const u = await media_proxy(v);
const secure = [ const secure = ["poketube.fun"].includes(req.hostname);
"poketube.fun"
].includes(req.hostname);
const verify = req.hostname === "pt.zzls.xyz"; const verify = req.hostname === "pt.zzls.xyz";
core.video(v).then((data) => {
core.video(v).then((data) => {
try { try {
const k = data?.video; const k = data?.video;
const json = data?.json; const json = data?.json;
@ -169,81 +194,113 @@ core.video(v).then((data) => {
d = desc.toString().replace(/\n/g, " <br> "); d = desc.toString().replace(/\n/g, " <br> ");
} }
const support = (String(inv_vid.description) !== "[object Object]") ? (PATREON_REGEX.exec(inv_vid.description) ?? {}).groups : undefined; const descriptionString = String(inv_vid.description);
function extractInfo(regex) {
return descriptionString !== "[object Object]"
? (regex.exec(descriptionString) ?? {}).groups
: undefined;
}
const support = extractInfo(PATREON_REGEX);
const twitter = extractInfo(X_REGEX);
const discord = extractInfo(CORD_REGEX);
const twitch = extractInfo(TWITCH_REGEX);
const reddit = extractInfo(REDDIT_REGEX);
const instagram = extractInfo(INSTAGRAM_REGEX);
let badges = ""; let badges = "";
let comments = ""; let comments = "";
let nnn = ""; let nnn = "";
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");
}
var uaos = req.useragent.os
const browser = req.useragent.browser;
const IsOldWindows = (uaos === "Windows 7" || uaos === "Windows 8") && browser === "Firefox";
if (uaos === "Windows XP" || uaos === "Windows Vista") res.redirect("/lite?v=" + req.query.v); 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"
);
}
var uaos = req.useragent.os;
const browser = req.useragent.browser;
const IsOldWindows =
(uaos === "Windows 7" || uaos === "Windows 8") &&
browser === "Firefox";
if (uaos === "Windows XP" || uaos === "Windows Vista")
res.redirect("/lite?v=" + req.query.v);
try { try {
renderTemplate(res, req, "poketube.ejs", { renderTemplate(res, req, "poketube.ejs", {
color: data.color, color: data.color,
color2: data.color2, color2: data.color2,
linkify, linkify,
engagement, engagement,
IsOldWindows, IsOldWindows,
support, support,
u:u.url, u: u.url,
isvidious:u.isInvidiousURL, isvidious: u.isInvidiousURL,
video: json, video: json,
date: k.Video.uploadDate, date: k.Video.uploadDate,
e, e,
a, a,
k, twitter,
dm, k,
useragent:req.useragent, dm,
verify, instagram,
secure, useragent: req.useragent,
process, verify,
sha384, discord,
lightOrDark, twitch,
isMobile: req.useragent.isMobile, reddit,
tj: data.channel, secure,
r, process,
qua: q, sha384,
inv: inv_comments, lightOrDark,
convert, isMobile: req.useragent.isMobile,
universe, tj: data.channel,
wiki: data.wiki, r,
f, qua: q,
t: config.t_url, inv: inv_comments,
optout: m, convert,
badges, universe,
desc, wiki: data.wiki,
comments, f,
n: nnn, t: config.t_url,
inv_vid, optout: m,
lyrics: "", badges,
}); desc,
comments,
n: nnn,
inv_vid,
lyrics: "",
});
} catch { } catch {
return; return;
} }
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return res.redirect("/?fromerror=41_generic_error"); return res.redirect("/?fromerror=41_generic_error");
} }
}); });
}); });
app.get("/lite", async function (req, res) { app.get("/lite", async function (req, res) {
const { v, e, r, f, t, quality: q } = req.query; const { v, e, r, f, t, quality: q } = req.query;
try { try {
const info = await modules.fetch("http://ip-api.com/json/"); const info = await modules.fetch("http://ip-api.com/json/");
const ip = await info.json(); const ip = await info.json();
const {video: k,json,engagement,comments: inv_comments,vid: inv_vid,} = await core.video(v); const {
video: k,
json,
engagement,
comments: inv_comments,
vid: inv_vid,
} = await core.video(v);
const data = await core.video(v); const data = await core.video(v);
const color = data.color; const color = data.color;
@ -260,7 +317,7 @@ core.video(v).then((data) => {
color, color,
color2, color2,
engagement, engagement,
u:u.url, u: u.url,
video: json, video: json,
date: k.Video.uploadDate, date: k.Video.uploadDate,
e, e,
@ -272,7 +329,7 @@ core.video(v).then((data) => {
tj, tj,
r, r,
qua: q, qua: q,
isvidious:u.isInvidiousURL, isvidious: u.isInvidiousURL,
inv: comments, inv: comments,
ip, ip,
convert, convert,