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) {
|
2023-02-25 18:46:42 +01:00
|
|
|
let tab = "";
|
|
|
|
if (req.query.tab) {
|
|
|
|
tab = `/?type=${capitalizeFirstLetter(req.query.tab)}`;
|
|
|
|
}
|
2022-11-09 17:48:29 +01:00
|
|
|
|
2023-05-16 20:07:17 +02:00
|
|
|
const invtrend = await modules.fetch(
|
|
|
|
`https://invid-api.poketube.fun/api/v1/trending${tab}`
|
|
|
|
);
|
2023-02-25 18:46:42 +01:00
|
|
|
const t = getJson(await invtrend.text());
|
2022-11-09 17:48:29 +01:00
|
|
|
|
2023-02-25 18:46:42 +01:00
|
|
|
let j = null;
|
2022-11-10 14:27:26 +01:00
|
|
|
if (req.query.mobilesearch) {
|
2023-02-25 18:46:42 +01:00
|
|
|
const query = req.query.mobilesearch;
|
|
|
|
const continuation = req.query.continuation || "";
|
2023-05-16 20:07:17 +02:00
|
|
|
const search = await modules.fetch(
|
|
|
|
`https://inner-api.poketube.fun/api/search?query=${query}&continuation=${continuation}`
|
|
|
|
);
|
2022-11-10 14:27:26 +01:00
|
|
|
const text = await search.text();
|
2023-02-25 18:46:42 +01:00
|
|
|
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", {
|
|
|
|
tab: req.query.tab,
|
|
|
|
isMobile: req.useragent.isMobile,
|
|
|
|
mobilesearch: req.query.mobilesearch,
|
|
|
|
inv: t,
|
|
|
|
turntomins,
|
2023-02-25 18:46:42 +01:00
|
|
|
continuation: req.query.continuation,
|
2022-11-10 14:27:26 +01:00
|
|
|
j,
|
|
|
|
});
|
|
|
|
});
|
2023-05-21 22:19:50 +02:00
|
|
|
|
|
|
|
app.get("/:v*?", async function (req, res) {
|
|
|
|
const uaos = req.useragent.os;
|
|
|
|
const browser = req.useragent.browser;
|
2023-06-02 17:54:54 +02:00
|
|
|
const isOldWindows = (uaos === "Windows 7" || uaos === "Windows 8") && browser === "Firefox";
|
2022-11-09 17:48:29 +01:00
|
|
|
|
2023-05-21 22:19:50 +02:00
|
|
|
const rendermainpage = () => {
|
|
|
|
if (req.useragent.isMobile) {
|
|
|
|
return res.redirect("/discover");
|
2023-05-16 20:07:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return renderTemplate(res, req, "landing.ejs", {
|
2023-05-21 22:19:50 +02:00
|
|
|
isOldWindows,
|
2023-05-16 20:07:17 +02:00
|
|
|
});
|
2023-05-21 22:19:50 +02:00
|
|
|
};
|
2023-05-16 20:07:17 +02:00
|
|
|
|
2023-05-21 22:19:50 +02:00
|
|
|
if (req.params.v && /[a-zA-Z0-9]+/.test(req.params.v)) {
|
|
|
|
const isvld = await core.isvalidvideo(req.params.v);
|
|
|
|
if (isvld) {
|
|
|
|
return res.redirect(`/watch?v=${req.params.v}`);
|
2022-11-10 14:27:26 +01:00
|
|
|
}
|
2023-05-21 22:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rendermainpage();
|
|
|
|
});
|
2023-05-16 20:07:17 +02:00
|
|
|
|
2022-11-10 14:27:26 +01:00
|
|
|
};
|