Update src/libpoketube/init/pages-404-and-main.js
This commit is contained in:
parent
d6b90620ea
commit
cec653d921
@ -34,21 +34,18 @@ function getJson(str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function (app, config, renderTemplate) {
|
module.exports = function (app, config, renderTemplate) {
|
||||||
app.get("/app", async function (req, res) {
|
app.get("/app", async function (req, res) {
|
||||||
const { fetch } = await import("undici");
|
const { fetch } = await import("undici");
|
||||||
|
|
||||||
// redirect mobiles to /app?tab=search
|
if (req.useragent?.isMobile) {
|
||||||
if (req.useragent && req.useragent.isMobile) {
|
|
||||||
return res.redirect("/app?tab=search");
|
return res.redirect("/app?tab=search");
|
||||||
}
|
}
|
||||||
|
|
||||||
// no more trending fetch
|
|
||||||
let tab = "";
|
let tab = "";
|
||||||
if (req.query.tab) {
|
if (req.query.tab) {
|
||||||
tab = `/?type=${capitalizeFirstLetter(req.query.tab)}`;
|
tab = `/?type=${capitalizeFirstLetter(req.query.tab)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// t is now empty string
|
|
||||||
const t = " ";
|
const t = " ";
|
||||||
|
|
||||||
const p = "";
|
const p = "";
|
||||||
@ -58,15 +55,19 @@ module.exports = function (app, config, renderTemplate) {
|
|||||||
const normalizeSearchData = (data) => {
|
const normalizeSearchData = (data) => {
|
||||||
if (!data) return { results: [] };
|
if (!data) return { results: [] };
|
||||||
if (Array.isArray(data)) return { results: data };
|
if (Array.isArray(data)) return { results: data };
|
||||||
if (Array.isArray(data.results)) return { results: data.results, meta: data.meta || {} };
|
if (Array.isArray(data.results))
|
||||||
if (Array.isArray(data.items)) return { results: data.items, meta: data.meta || {} };
|
return { results: data.results, meta: data.meta || {} };
|
||||||
if (Array.isArray(data.videos)) return { results: data.videos, meta: data.meta || {} };
|
if (Array.isArray(data.items))
|
||||||
|
return { results: data.items, meta: data.meta || {} };
|
||||||
|
if (Array.isArray(data.videos))
|
||||||
|
return { results: data.videos, meta: data.meta || {} };
|
||||||
return { results: [], meta: { note: "unrecognized search payload shape" } };
|
return { results: [], meta: { note: "unrecognized search payload shape" } };
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const query =
|
const query =
|
||||||
(typeof req.query.mobilesearch === "string" && req.query.mobilesearch.trim()) ??
|
(typeof req.query.mobilesearch === "string" &&
|
||||||
|
req.query.mobilesearch.trim()) ??
|
||||||
(typeof req.query.query === "string" && req.query.query.trim()) ??
|
(typeof req.query.query === "string" && req.query.query.trim()) ??
|
||||||
(typeof req.query.q === "string" && req.query.q.trim()) ??
|
(typeof req.query.q === "string" && req.query.q.trim()) ??
|
||||||
"";
|
"";
|
||||||
@ -74,35 +75,39 @@ module.exports = function (app, config, renderTemplate) {
|
|||||||
const continuation = (req.query.continuation ?? "1").toString();
|
const continuation = (req.query.continuation ?? "1").toString();
|
||||||
|
|
||||||
if (query) {
|
if (query) {
|
||||||
const searchUrl = `${config.invapi}/search?q=${encodeURIComponent(query)}&type=video&page=${encodeURIComponent(
|
const searchUrl = `${config.invapi}/search?q=${encodeURIComponent(
|
||||||
continuation
|
query
|
||||||
)}`;
|
)}&type=video&page=${encodeURIComponent(continuation)}`;
|
||||||
|
|
||||||
const resSearch = await fetch(searchUrl, {
|
const r = await fetch(searchUrl, {
|
||||||
headers: { "User-Agent": config.useragent },
|
headers: { "User-Agent": config.useragent },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!resSearch.ok) {
|
if (!r.ok) {
|
||||||
j = {
|
j = {
|
||||||
results: [],
|
results: [],
|
||||||
error: true,
|
error: true,
|
||||||
meta: { status: resSearch.status, statusText: resSearch.statusText, url: searchUrl },
|
meta: { status: r.status, statusText: r.statusText, url: searchUrl },
|
||||||
};
|
};
|
||||||
console.error("[mobilesearch] HTTP error", j.meta);
|
console.error("[mobilesearch] HTTP error", j.meta);
|
||||||
} else {
|
} else {
|
||||||
const ct = resSearch.headers.get("content-type") || "";
|
const ct = r.headers.get("content-type") || "";
|
||||||
let data;
|
let data;
|
||||||
|
|
||||||
if (ct.includes("application/json")) {
|
if (ct.includes("application/json")) {
|
||||||
data = await resSearch.json();
|
data = await r.json();
|
||||||
} else {
|
} else {
|
||||||
const txt = await resSearch.text();
|
const txt = await r.text();
|
||||||
data = await Promise.resolve(getJson(txt));
|
data = await Promise.resolve(getJson(txt));
|
||||||
}
|
}
|
||||||
|
|
||||||
j = normalizeSearchData(data);
|
j = normalizeSearchData(data);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
j = { results: [], error: true, meta: { reason: "missing query" } };
|
j = { results: [], error: true, meta: { reason: "missing query" } };
|
||||||
console.warn("[mobilesearch] Missing query param (mobilesearch/q/query)");
|
console.warn(
|
||||||
|
"[mobilesearch] Missing query parameter (mobilesearch/q/query)"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
j.meta = { ...(j.meta || {}), continuation };
|
j.meta = { ...(j.meta || {}), continuation };
|
||||||
@ -110,7 +115,10 @@ module.exports = function (app, config, renderTemplate) {
|
|||||||
j = {
|
j = {
|
||||||
results: [],
|
results: [],
|
||||||
error: true,
|
error: true,
|
||||||
meta: { reason: "exception", message: String((err && err.message) || err) },
|
meta: {
|
||||||
|
reason: "exception",
|
||||||
|
message: String((err && err.message) || err),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
console.error("[mobilesearch] Exception:", err);
|
console.error("[mobilesearch] Exception:", err);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user