Refactor code :3

This commit is contained in:
Ashley 2023-02-25 17:06:28 +00:00
parent 6283abef1e
commit 3b00505ed5

View file

@ -120,7 +120,8 @@ module.exports = function (app, config, renderTemplate) {
} }
}); });
app.get("/channel/", async (req, res) => { app.get("/channel/", async (req, res) => {
try {
const ID = req.query.id; const ID = req.query.id;
const tab = req.query.tab; const tab = req.query.tab;
@ -130,79 +131,65 @@ module.exports = function (app, config, renderTemplate) {
config.tubeApi + `channel?id=${ID}&tab=about` config.tubeApi + `channel?id=${ID}&tab=about`
); );
const h = await bout.text(); const h = await bout.text();
var k = JSON.parse(modules.toJson(h)); var boutJson = JSON.parse(modules.toJson(h));
} catch { } catch {
k = " "; boutJson = " ";
} }
const continuation = req.query.continuation || "";
const continuationl = req.query.continuationl || "";
const continuations = req.query.continuations || "";
const sort_by = req.query.sort_by || "newest";
const getChannelData = async (url) => {
try { try {
const response = await modules.fetch(url);
// continuation stuff - whoa cool return JSON.parse(await response.text());
let continuation = req.query.continuation ? `&continuation=${req.query.continuation}` : ""; } catch (error) {
let continuationl = req.query.continuationl ? `&continuation=${req.query.continuationl}` : ""; console.error("Failed to fetch channel data from API:", error);
let continuations = req.query.continuations ? `&continuation=${req.query.continuations}` : ""; return null;
// videos - i dont think this is readable at all but o welp if it works it works:tm:
// https://github.com/iv-org/invidious/blob/05258d56bdc3f4de1f0da0c0dbd2d540f68cbdd5/src/invidious/channels/videos.cr
const tj = await modules.fetch(`https://inv.zzls.xyz/api/v1/channels/videos/${ID}/?sort_by=${req.query.sort_by || "newest"}` + continuation).then((res) => res.text()).then((txt) => getJson(txt)).catch(" ")
const shorts = await modules.fetch(`https://inv.zzls.xyz/api/v1/channels/${ID}/shorts?sort_by=${req.query.sort_by || "newest"}` + continuations).then((res) => res.text()).then((txt) => getJson(txt)).catch(" ")
const stream = await modules.fetch(`https://inv.zzls.xyz/api/v1/channels/${ID}/streams?sort_by=${req.query.sort_by || "newest"}` + continuationl).then((res) => res.text()).then((txt) => getJson(txt)).catch(" ")
// community tab - protobuf Egljb21tdW5pdHk%3D
const c = await modules.fetch(`https://inv.zzls.xyz/api/v1/channels/community/${ID}/`).then((res) => res.text()) .then((txt) => getJson(txt));
const summary = await wiki.summary(k.Channel.Metadata.Name);
var w = "";
if (summary.title === "Not found.") {
w = "none";
} }
if (summary.title !== "Not found.") {
w = summary;
} }
const { Subscribers: subscribers } = k.Channel.Metadata; const [tj, shorts, stream, c] = await Promise.all([
const description = k.Channel.Contents.ItemSection.About.Description; getChannelData(`https://inv.zzls.xyz/api/v1/channels/videos/${ID}/?sort_by=${sort_by}&continuation=${continuation}`),
getChannelData(`https://inv.zzls.xyz/api/v1/channels/${ID}/shorts?sort_by=${sort_by}&continuation=${continuations}`),
getChannelData(`https://inv.zzls.xyz/api/v1/channels/${ID}/streams?sort_by=${sort_by}&continuation=${continuationl}`),
getChannelData(`https://inv.zzls.xyz/api/v1/channels/community/${ID}/`),
]);
var d = description.toString().replace(/\n/g, " <br> "); const summary = await wiki.summary(boutJson.Channel.Metadata.Name);
const wikiSummary = summary.title !== "Not found." ? summary : "none";
if (d === "[object Object]") { const subscribers = boutJson.Channel.Metadata.Subscribers;
var d = ""; const about = boutJson.Channel.Contents.ItemSection.About;
} const description = about.Description.toString().replace(/\n/g, " <br> ");
const dnoreplace = about.Description.toString();
var dnoreplace = description.toString();
if (dnoreplace === "[object Object]") {
var dnoreplace = "";
}
renderTemplate(res, req, "channel.ejs", { renderTemplate(res, req, "channel.ejs", {
ID, ID,
tab, tab,
shorts, shorts,
j: k, j: boutJson,
sort:req.query.sort_by, sort: sort_by,
stream, stream,
tj, tj,
c, c,
convert, convert,
turntomins, turntomins,
dnoreplace: dnoreplace, dnoreplace,
continuation: continuation, continuation,
wiki: w, wiki: wikiSummary,
getFirstLine: getFirstLine, getFirstLine,
isMobile: req.useragent.isMobile, isMobile: req.useragent.isMobile,
about: k.Channel.Contents.ItemSection.About, about,
subs: subs: typeof subscribers === "string" ? subscribers.replace("subscribers", "") : "None",
typeof subscribers === "string" desc: dnoreplace === "[object Object]" ? "" : description,
? subscribers.replace("subscribers", "")
: "None",
desc: d,
}); });
} catch { } catch (error) {
console.error("Failed to render channel page:", error);
res.redirect("/"); res.redirect("/");
} }
}); });
}; };