Refactor file

This commit is contained in:
Ashley 2022-10-09 14:30:08 +02:00
parent 53159296d3
commit a50baf9844

View file

@ -7,8 +7,7 @@
This file is Licensed under LGPL-3.0-or-later. Poketube itself is GPL, Only this file is LGPL. This file is Licensed under LGPL-3.0-or-later. Poketube itself is GPL, Only this file is LGPL.
see a copy here:https://www.gnu.org/licenses/lgpl-3.0.txt see a copy here:https://www.gnu.org/licenses/lgpl-3.0.txt
*/ */
const fetch = require("node-fetch"); const fetch = require("node-fetch");
const { toJson } = require("xml2json"); const { toJson } = require("xml2json");
@ -20,29 +19,16 @@ const wiki = require("wikipedia");
const config = { const config = {
tubeApi: "https://tube.kuylar.dev/api/", tubeApi: "https://tube.kuylar.dev/api/",
dislikes: "https://returnyoutubedislikeapi.com/votes?videoId=", dislikes: "https://returnyoutubedislikeapi.com/votes?videoId=",
t_url: "https://t.poketube.fun/", // def matomo url t_url: "https://t.poketube.fun/" // def matomo url
}; };
// Util functions // Util functions
function IsJsonString(str) { function getJson(str) {
try { try {
JSON.parse(str); return JSON.parse(str);
} catch (e) { } catch {
return false; return null;
} }
return true;
}
function convert(value) {
return new Intl.NumberFormat("en-GB", {
notation: "compact",
}).format(value);
}
function getFirstLine(text) {
var index = text.indexOf("<br> ");
if (index === -1) index = undefined;
return text.substring(0, index);
} }
/* /*
@ -50,154 +36,90 @@ function getFirstLine(text) {
*/ */
async function channel(id, cnt) { async function channel(id, cnt) {
if (!id) return "Gib ID"; if (id == null) return "Gib ID";
if (cnt) { const videos = await fetch(
var continuation = cnt; `${config.tubeApi}channel?id=${id}&tab=videos&continuation=${cnt || ""}`
} )
if (!continuation) { .then((res) => res.text())
var continuation = ""; .then((xml) => JSON.parse(toJson(xml)));
}
// videos const about = await fetch(`${config.tubeApi}channel?id=${id}&tab=about`)
const channel = await fetch( .then((res) => res.text())
config.tubeApi + `channel?id=${id}&tab=videos&continuation=${continuation}` .then((xml) => JSON.parse(toJson(xml)));
);
const c = await channel.text();
const videos = JSON.parse(toJson(c));
// about return { videos, about };
const abtchnl = await fetch(config.tubeApi + `channel?id=${id}&tab=about`);
const ab = await abtchnl.text();
const a = JSON.parse(toJson(ab));
return {
videos: videos,
about: a,
};
} }
async function video(v) { async function video(v) {
if (!v) return "Gib ID"; if (v == null) return "Gib ID";
var badges = ""; let nightlyRes;
for (let i = 0; i < 2; i++) { for (let i = 0; i < 2; i++) {
try { try {
const nightly = await fetch( const nightly = await fetch(
`https://lighttube-nightly.kuylar.dev/api/video?v=${v}` `https://lighttube-nightly.kuylar.dev/api/video?v=${v}`
); ).then((res) => res.text());
var n = await nightly.text();
nightlyRes = nightly;
break;
} catch (err) { } catch (err) {
if (err.status === 500) { if (err.status === 500)
// retry after a bit // Retry after a second.
await new Promise((resolve) => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
} else { else return "";
return (n = "");
}
} }
} }
var nn = ""; const video = await fetch(`${config.tubeApi}video?v=${v}`)
var nnn = ""; .then((res) => res.text())
var comments = ""; .then((xml) => JSON.parse(toJson(xml)));
if (n == "") { const channel = await channel(video.Video.Channel.id);
badges, nnn, (comments = "");
}
if (IsJsonString(n)) { const summary = await wiki
if (n != "") { .summary(video.Video.Channel.Name)
nnn = JSON.parse(n); .then((summary_) => (summary_.title !== "Not found." ? summary_ : "none"));
badges = nnn.channel.badges[0];
comments = nnn.commentCount;
}
}
const video = await fetch(config.tubeApi + `video?v=${v}`); const data = await fetcher(v);
const h = await video.text(); const nightlyJsonData = nightlyRes !== "" && getJson(nightlyRes);
const k = JSON.parse(toJson(h));
const tj = await channel(k.Video.Channel.id).then((data) => data.videos); return {
const a = await channel(k.Video.Channel.id).then((data) => data.about); json: data.video.Player,
video,
const summary = await wiki.summary(k.Video.Channel.Name); engagement: data.engagement,
wiki: summary,
var w = ""; desc: channel.about.Channel.Contents.ItemSection.About.Description,
if (summary.title === "Not found.") { color: await getColors(
w = "none"; `https://i.ytimg.com/vi/${v}/maxresdefault.jpg`
} ).then((colors) => colors[0].hex()),
if (summary.title !== "Not found.") { b: nightlyJsonData !== null,
w = summary; ...(nightlyJsonData !== null
} ? {
beta: nightlyJsonData,
var fetching = await fetcher(v); badges: nightlyJsonData.channel.badges[0],
comments: nightlyJsonData.commentCount
const json = fetching.video.Player; }
: {})
if (IsJsonString(n)) { };
if (n != "") {
var returner = {
json: json,
video: k,
beta: nnn,
badges: badges,
comments: comments,
engagement: fetching.engagement,
wiki: w,
desc: a.Channel.Contents.ItemSection.About.Description,
color: await getColors(
`https://i.ytimg.com/vi/${v}/maxresdefault.jpg`
).then((colors) => colors[0].hex()),
channel: tj,
b: true,
};
}
}
if (!IsJsonString(n)) {
if (n == "") {
var returner = {
json: json,
video: k,
engagement: fetching.engagement,
wiki: w,
desc: a.Channel.Contents.ItemSection.About.Description,
channel: tj,
color: await getColors(
`https://i.ytimg.com/vi/${v}/maxresdefault.jpg`
).then((colors) => colors[0].hex()),
b: false,
};
}
}
return returner;
} }
async function search(query, cnt) { async function search(query, cnt) {
if (!query) return "Gib Query"; if (query == null) return "Gib Query";
if (cnt) { const data = await fetch(
var continuation = cnt; `${config.tubeApi}search?query=${query}&continuation=${cnt || ""}`
} )
if (!cnt) { .then((res) => res.text())
var continuation = ""; .then((xml) => JSON.parse(toJson(xml)));
}
const search = await fetch( return data;
`https://tube.kuylar.dev/api/search?query=${query}&continuation=${continuation}`
);
const text = await search.text();
const j = JSON.parse(toJson(text));
return j;
} }
module.exports = { module.exports = {
search, search,
video, video,
channel, channel
}; };