poke/src/libpoketube/init/pages-404-and-main.js

102 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-11-10 14:27:26 +01:00
const {
fetcher,
core,
wiki,
musicInfo,
modules,
version,
initlog,
init,
} = require("../libpoketube-initsys.js");
2022-11-09 17:48:29 +01:00
const {
IsJsonString,
convert,
getFirstLine,
capitalizeFirstLetter,
turntomins,
getRandomInt,
getRandomArbitrary,
} = require("../ptutils/libpt-coreutils.js");
const sha384 = modules.hash;
2022-12-18 15:00:51 +01:00
function getJson(str) {
try {
2022-12-18 16:02:03 +01:00
return JSON.parse(str);
2022-12-18 15:00:51 +01:00
} catch {
return null;
}
}
2022-11-09 17:48:29 +01:00
module.exports = function (app, config, renderTemplate) {
2022-11-10 14:27:26 +01:00
app.get("/discover", async function (req, res) {
const trends = await modules.fetch(config.tubeApi + `trending`);
const h = await trends.text();
2022-12-18 15:00:51 +01:00
const k = getJson(modules.toJson(h));
2022-11-09 17:48:29 +01:00
2022-11-10 14:27:26 +01:00
if (req.query.tab)
var tab = `/?type=${capitalizeFirstLetter(req.query.tab)}`;
2022-11-09 17:48:29 +01:00
2022-11-10 14:27:26 +01:00
if (!req.query.tab) var tab = "";
2022-11-09 17:48:29 +01:00
2022-11-10 14:27:26 +01:00
const invtrend = await modules
.fetch(`https://vid.puffyan.us/api/v1/trending${tab}`)
.then((res) => res.text());
2022-11-09 17:48:29 +01:00
2022-12-18 15:00:51 +01:00
const t = getJson(invtrend);
2022-11-09 17:48:29 +01:00
2022-11-10 14:27:26 +01:00
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 = "";
}
2022-11-09 17:48:29 +01:00
2022-11-10 14:27:26 +01:00
const search = await modules.fetch(
2022-12-15 17:22:03 +01:00
`https://tube-srv.ashley143.gay/api/search?query=${query}&continuation=${continuation}`
2022-11-10 14:27:26 +01:00
);
2022-11-09 17:48:29 +01:00
2022-11-10 14:27:26 +01:00
const text = await search.text();
2022-12-18 15:00:51 +01:00
var j = getJson(modules.toJson(text));
2022-11-09 17:48:29 +01:00
}
2022-11-10 14:27:26 +01:00
renderTemplate(res, req, "main.ejs", {
k: k,
tab: req.query.tab,
isMobile: req.useragent.isMobile,
mobilesearch: req.query.mobilesearch,
inv: t,
turntomins,
continuation,
j,
});
});
2022-11-09 17:48:29 +01:00
2022-11-10 14:27:26 +01:00
app.get("/:v*?", async function (req, res) {
let rendermainpage = () => {
if (req.useragent.isMobile) {
return res.redirect(`/discover`);
} else {
return renderTemplate(res, req, "landing.ejs");
}
};
2022-11-09 17:48:29 +01:00
2022-11-10 14:27:26 +01:00
if (req.params.v) {
2022-12-18 12:56:33 +01:00
if (/[a-zA-Z0-9]+/.test(req.param.v)) {
const isvld = await core.isvalidvideo(req.params.v);
2022-11-09 17:48:29 +01:00
2022-12-18 12:56:33 +01:00
if (isvld) {
return res.redirect(`/watch?v=${req.params.v}`);
} else {
return rendermainpage();
}
2022-11-10 14:27:26 +01:00
}
} else {
return rendermainpage();
}
});
};