poke/src/libpoketube/init/pages-channel-and-download.js

204 lines
5.4 KiB
JavaScript
Raw Normal View History

2023-02-26 10:34:24 +01:00
const {
2022-11-18 10:42:25 +01:00
fetcher,
core,
wiki,
musicInfo,
modules,
version,
initlog,
init,
} = require("../libpoketube-initsys.js");
2022-11-09 17:52:57 +01:00
const {
IsJsonString,
convert,
getFirstLine,
capitalizeFirstLetter,
turntomins,
getRandomInt,
getRandomArbitrary,
} = require("../ptutils/libpt-coreutils.js");
2022-12-18 16:30:24 +01:00
const sha384 = modules.hash;
2022-12-09 18:13:14 +01:00
function getJson(str) {
try {
return JSON.parse(str);
} catch {
return null;
}
}
2022-11-09 17:52:57 +01:00
module.exports = function (app, config, renderTemplate) {
2022-11-18 10:42:25 +01:00
app.get("/download", async function (req, res) {
var v = req.query.v;
// video
const video = await modules.fetch(config.tubeApi + `video?v=${v}`);
const h = await video.text();
const k = JSON.parse(modules.toJson(h));
if (!v) res.redirect("/");
var fetching = await fetcher(v);
const json = fetching.video.Player;
const engagement = fetching.engagement;
renderTemplate(res, req, "download.ejs", {
engagement: engagement,
k: k,
video: json,
date: k.Video.uploadDate,
color: await modules
.getColors(`https://i.ytimg.com/vi/${v}/maxresdefault.jpg`)
.then((colors) => colors[0].hex()),
});
});
app.get("/old/watch", async function (req, res) {
var v = req.query.v;
var e = req.query.e;
if (!v) res.redirect("/");
res.redirect(`/watch?v=${v}`);
2022-11-09 17:52:57 +01:00
});
2022-11-18 10:42:25 +01:00
app.get("/search", async (req, res) => {
const query = req.query.query;
if (!query) {
return res.redirect("/");
}
2023-02-25 18:45:15 +01:00
let continuation = req.query.continuation || "";
2022-12-31 13:39:51 +01:00
2023-02-25 18:45:15 +01:00
try {
2023-03-21 20:30:04 +01:00
const searchUrl = `https://inner-api.poketube.fun/api/search?query=${encodeURIComponent(
2023-02-25 18:45:15 +01:00
query
)}&continuation=${encodeURIComponent(continuation)}`;
const searchResponse = await modules.fetch(searchUrl);
const searchText = await searchResponse.text();
const searchJson = JSON.parse(modules.toJson(searchText));
let didYouMean;
if (
searchJson.Search?.Results?.DynamicItem?.id === "didYouMeanRenderer"
) {
didYouMean = JSON.parse(searchJson.Search.Results.DynamicItem.Title);
}
2022-12-30 17:22:29 +01:00
2023-02-26 10:34:24 +01:00
const summary = await wiki
.summary(query + " ")
.then((summary_) =>
summary_.title !== "Not found." ? summary_ : "none"
);
2022-12-31 13:39:51 +01:00
2023-02-25 18:45:15 +01:00
renderTemplate(res, req, "search.ejs", {
j: searchJson,
h: didYouMean,
continuation,
q: query,
2023-02-26 10:34:24 +01:00
summary,
2023-02-25 18:45:15 +01:00
});
} catch (error) {
console.error(`Error while searching for '${query}':`, error);
res.redirect("/");
2022-12-30 17:22:29 +01:00
}
2022-11-09 17:52:57 +01:00
});
2022-11-18 10:42:25 +01:00
2023-02-25 18:45:15 +01:00
app.get("/channel/", async (req, res) => {
2022-12-21 09:25:16 +01:00
try {
2023-02-25 18:45:15 +01:00
const ID = req.query.id;
const tab = req.query.tab;
2022-11-18 10:42:25 +01:00
2023-02-25 18:06:28 +01:00
try {
2023-02-25 18:45:15 +01:00
// about
const bout = await modules.fetch(
config.tubeApi + `channel?id=${ID}&tab=about`
);
const h = await bout.text();
var boutJson = JSON.parse(modules.toJson(h));
} catch {
boutJson = " ";
2022-12-18 16:30:24 +01:00
}
2022-11-18 10:42:25 +01:00
2023-03-12 21:05:28 +01:00
const continuation = req.query.continuation
? `&continuation=${req.query.continuation}`
: "";
const continuationl = req.query.continuationl
? `&continuation=${req.query.continuationl}`
: "";
const continuations = req.query.continuations
? `&continuation=${req.query.continuations}`
: "";
2023-02-25 18:45:15 +01:00
const sort_by = req.query.sort_by || "newest";
const getChannelData = async (url) => {
try {
2023-03-12 21:05:28 +01:00
return await modules
.fetch(url)
.then((res) => res.text())
.then((txt) => getJson(txt));
2023-02-25 18:45:15 +01:00
} catch (error) {
console.error("Failed to fetch channel data from API:", error);
return null;
}
};
2023-03-12 21:05:28 +01:00
const tj = await getChannelData(
`https://invid-api.poketube.fun/api/v1/channels/videos/${ID}/?sort_by=${
req.query.sort_by || "newest"
}${continuation}`
);
const shorts = await getChannelData(
`https://invid-api.poketube.fun/api/v1/channels/${ID}/shorts?sort_by=${
req.query.sort_by || "newest"
}${continuations}`
);
const stream = await getChannelData(
`https://invid-api.poketube.fun/api/v1/channels/${ID}/streams?sort_by=${
req.query.sort_by || "newest"
}${continuationl}`
);
const c = await getChannelData(
`https://invid-api.poketube.fun/api/v1/channels/community/${ID}/`
);
2023-02-25 18:45:15 +01:00
const summary = await wiki.summary(boutJson.Channel.Metadata.Name);
const wikiSummary = summary.title !== "Not found." ? summary : "none";
const subscribers = boutJson.Channel.Metadata.Subscribers;
const about = boutJson.Channel.Contents.ItemSection.About;
const description = about.Description.toString().replace(/\n/g, " <br> ");
const dnoreplace = about.Description.toString();
renderTemplate(res, req, "channel.ejs", {
ID,
tab,
shorts,
j: boutJson,
sort: sort_by,
stream,
tj,
c,
convert,
turntomins,
dnoreplace,
continuation,
wiki: wikiSummary,
getFirstLine,
isMobile: req.useragent.isMobile,
about,
subs:
typeof subscribers === "string"
? subscribers.replace("subscribers", "")
: "None",
desc: dnoreplace === "[object Object]" ? "" : description,
});
} catch (error) {
console.error("Failed to render channel page:", error);
res.redirect("/");
}
});
2022-11-18 10:42:25 +01:00
};