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

235 lines
6.1 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");
2023-08-21 23:11:46 +02:00
const { curly } = require("node-libcurl");
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;
}
}
2023-09-09 18:49:38 +02:00
const ChannelTabs = {
community: "Y29tbXVuaXR5",
shorts:"c2hvcnRz",
videos:"dmlkZW9z",
streams:"c3RyZWFtcw=="
}
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) {
2023-03-26 14:30:07 +02:00
try {
2023-04-28 22:39:55 +02:00
var v = req.query.v;
2022-11-18 10:42:25 +01:00
2023-04-28 22:39:55 +02:00
renderTemplate(res, req, "download.ejs", {
2023-05-16 20:14:11 +02:00
v,
2023-04-28 22:39:55 +02:00
color: await modules
.getColors(`https://i.ytimg.com/vi/${v}/maxresdefault.jpg`)
.then((colors) => colors[0].hex()),
});
2023-05-16 20:14:11 +02:00
} catch {
2023-04-28 22:39:55 +02:00
res.redirect("/");
2023-03-26 14:30:07 +02:00
}
2022-11-18 10:42:25 +01:00
});
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;
2023-05-16 20:14:11 +02:00
var uaos = req.useragent.os;
var IsOldWindows;
if (uaos == "Windows 7" && req.useragent.browser == "Firefox") {
IsOldWindows = true;
} else if (uaos == "Windows 8" && req.useragent.browser == "Firefox") {
IsOldWindows = true;
} else {
IsOldWindows = false;
}
const poketube_universe_value = "poketube_smart_search";
2023-09-09 18:49:38 +02:00
if (query?.includes("youtube.com/watch?v=")) {
2023-05-07 13:28:54 +02:00
try {
2023-06-18 18:00:40 +02:00
var videoid = query?.split("v=");
2023-05-16 20:14:11 +02:00
res.redirect("/watch?v=" + videoid[1]);
2023-05-07 13:28:54 +02:00
} catch {
return;
}
}
2023-05-16 20:14:11 +02:00
2023-09-09 18:49:38 +02:00
if (query && query.startsWith("!") && query.length > 2) {
res.redirect("https://lite.duckduckgo.com/lite/?q=" + query);
2023-09-07 15:13:40 +02:00
}
2023-08-09 20:44:59 +02:00
2022-11-18 10:42:25 +01:00
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-09-07 15:13:40 +02:00
const headers = {};
2023-08-21 23:11:46 +02:00
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)}`;
2023-08-21 23:11:46 +02:00
const { data } = await curly.get(searchUrl, {
httpHeader: Object.entries(headers).map(([k, v]) => `${k}: ${v}`),
});
const searchText = modules.toJson(data);
const searchJson = getJson(searchText);
2023-02-25 18:45:15 +01:00
let didYouMean;
2023-09-09 18:49:38 +02:00
if ( searchJson.Search?.Results?.DynamicItem?.id === "didYouMeanRenderer" ) { didYouMean = JSON.parse(searchJson.Search.Results.DynamicItem.Title);
2023-02-25 18:45:15 +01:00
}
2022-12-30 17:22:29 +01:00
2023-02-25 18:45:15 +01:00
renderTemplate(res, req, "search.ejs", {
j: searchJson,
2023-05-16 20:14:11 +02:00
IsOldWindows,
2023-02-25 18:45:15 +01:00
h: didYouMean,
continuation,
q: query,
2023-09-07 15:13:40 +02: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) => {
2023-08-24 21:22:16 +02:00
const { fetch } = await import("undici");
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;
2023-04-28 22:39:55 +02:00
const cache = {};
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
2023-09-07 15:13:40 +02:00
const bout = await fetch(config.tubeApi + `channel?id=${ID}&tab=about`);
2023-02-25 18:45:15 +01:00
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-08-24 21:22:16 +02:00
return await fetch(url)
2023-03-12 21:05:28 +01:00
.then((res) => res.text())
.then((txt) => getJson(txt));
2023-02-25 18:45:15 +01:00
} catch (error) {
return null;
}
};
2023-09-09 18:49:38 +02:00
2023-09-07 15:13:40 +02:00
const apiUrl = "https://invid-api.poketube.fun/api/v1/channels/";
2023-09-09 18:49:38 +02:00
const channelUrl = `${apiUrl}${atob(ChannelTabs.videos)}/${ID}/?sort_by=${sort_by}${continuation}`;
const shortsUrl = `${apiUrl}${ID}/${atob(ChannelTabs.shorts)}?sort_by=${sort_by}${continuations}`;
const streamUrl = `${apiUrl}${ID}/${atob(ChannelTabs.streams)}?sort_by=${sort_by}${continuationl}`;
const communityUrl = `${apiUrl}${atob(ChannelTabs.community)}/${ID}/?hl=en-US`;
2023-09-07 15:13:40 +02:00
const channelINVUrl = `${apiUrl}${ID}/`;
var [tj, shorts, stream, c, cinv] = await Promise.all([
getChannelData(channelUrl),
getChannelData(shortsUrl),
getChannelData(streamUrl),
getChannelData(communityUrl),
getChannelData(channelINVUrl),
]);
2023-04-28 22:39:55 +02:00
cache[ID] = {
result: {
tj,
shorts,
stream,
c,
2023-08-18 18:27:48 +02:00
cinv,
2023-04-28 22:39:55 +02:00
boutJson,
},
timestamp: Date.now(),
};
if (cache[ID] && Date.now() - cache[ID].timestamp < 3600000) {
var { tj, shorts, stream, c, boutJson } = cache[ID].result;
}
2023-09-07 15:13:40 +02:00
2023-08-09 20:44:59 +02:00
const subscribers = boutJson.Channel?.Metadata.Subscribers;
2023-09-27 20:16:09 +02:00
const about = boutJson?.Channel?.Contents?.ItemSection?.About;
2023-09-13 19:28:54 +02:00
const description = about?.Description.toString().replace(/\n/g, " <br> ");
const dnoreplace = about?.Description.toString();
2023-02-25 18:45:15 +01:00
renderTemplate(res, req, "channel.ejs", {
ID,
tab,
shorts,
j: boutJson,
sort: sort_by,
stream,
tj,
c,
2023-08-18 18:27:48 +02:00
cinv,
2023-02-25 18:45:15 +01:00
convert,
turntomins,
dnoreplace,
continuation,
2023-08-09 20:44:59 +02:00
wiki: "",
2023-02-25 18:45:15 +01:00
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
};