Refactor code :3

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

View file

@ -1,16 +1,38 @@
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>`;
}); });
} }
@ -118,13 +140,19 @@ 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) {
@ -149,12 +177,9 @@ 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;
@ -169,21 +194,43 @@ 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") { if (
res.send("error: " + inv_vid.error + " please refresh the page please qt"); 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 var uaos = req.useragent.os;
const browser = req.useragent.browser; const browser = req.useragent.browser;
const IsOldWindows = (uaos === "Windows 7" || uaos === "Windows 8") && browser === "Firefox"; 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 (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", {
@ -199,10 +246,15 @@ core.video(v).then((data) => {
date: k.Video.uploadDate, date: k.Video.uploadDate,
e, e,
a, a,
twitter,
k, k,
dm, dm,
instagram,
useragent: req.useragent, useragent: req.useragent,
verify, verify,
discord,
twitch,
reddit,
secure, secure,
process, process,
sha384, sha384,
@ -239,11 +291,16 @@ core.video(v).then((data) => {
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;