poke/src/libpoketube/init/pages-404-and-main.js
2022-12-25 20:51:31 +00:00

101 lines
2.3 KiB
JavaScript

const {
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 sha384 = modules.hash;
function getJson(str) {
try {
return JSON.parse(str);
} catch {
return null;
}
}
module.exports = function (app, config, renderTemplate) {
app.get("/discover", async function (req, res) {
const trends = await modules.fetch(config.tubeApi + `trending`);
const h = await trends.text();
const k = getJson(modules.toJson(h));
if (req.query.tab)
var tab = `/?type=${capitalizeFirstLetter(req.query.tab)}`;
if (!req.query.tab) var tab = "";
const invtrend = await modules
.fetch(`https://inv.vern.cc/api/v1/trending${tab}`)
.then((res) => res.text());
const t = getJson(invtrend);
if (req.query.mobilesearch) {
var query = req.query.mobilesearch;
tab = "search";
if (req.query.continuation) {
var continuation = req.query.continuation;
}
if (!req.query.continuation) {
var continuation = "";
}
const search = await modules.fetch(
`https://tube-srv.ashley143.gay/api/search?query=${query}&continuation=${continuation}`
);
const text = await search.text();
var j = getJson(modules.toJson(text));
}
renderTemplate(res, req, "main.ejs", {
k: k,
tab: req.query.tab,
isMobile: req.useragent.isMobile,
mobilesearch: req.query.mobilesearch,
inv: t,
turntomins,
continuation,
j,
});
});
app.get("/:v*?", async function (req, res) {
let rendermainpage = () => {
if (req.useragent.isMobile) {
return res.redirect(`/discover`);
} else {
return renderTemplate(res, req, "landing.ejs");
}
};
if (req.params.v) {
if (/[a-zA-Z0-9]+/.test(req.param.v)) {
const isvld = await core.isvalidvideo(req.params.v);
if (isvld) {
return res.redirect(`/watch?v=${req.params.v}`);
} else {
return rendermainpage();
}
}
} else {
return rendermainpage();
}
});
};