Refactor file

This commit is contained in:
Ashley 2022-10-09 14:30:08 +02:00
parent 5ae14bc68b
commit cf46f15a0c

View file

@ -8,7 +8,6 @@
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 = "";
var nnn = "";
var comments = "";
if (n == "") {
badges, nnn, (comments = "");
}
if (IsJsonString(n)) {
if (n != "") {
nnn = JSON.parse(n);
badges = nnn.channel.badges[0];
comments = nnn.commentCount;
} }
} }
const video = await fetch(config.tubeApi + `video?v=${v}`); const video = await fetch(`${config.tubeApi}video?v=${v}`)
.then((res) => res.text())
.then((xml) => JSON.parse(toJson(xml)));
const h = await video.text(); const channel = await channel(video.Video.Channel.id);
const k = JSON.parse(toJson(h));
const tj = await channel(k.Video.Channel.id).then((data) => data.videos); const summary = await wiki
const a = await channel(k.Video.Channel.id).then((data) => data.about); .summary(video.Video.Channel.Name)
.then((summary_) => (summary_.title !== "Not found." ? summary_ : "none"));
const summary = await wiki.summary(k.Video.Channel.Name); const data = await fetcher(v);
var w = ""; const nightlyJsonData = nightlyRes !== "" && getJson(nightlyRes);
if (summary.title === "Not found.") {
w = "none";
}
if (summary.title !== "Not found.") {
w = summary;
}
var fetching = await fetcher(v); return {
json: data.video.Player,
const json = fetching.video.Player; video,
engagement: data.engagement,
if (IsJsonString(n)) { wiki: summary,
if (n != "") { desc: channel.about.Channel.Contents.ItemSection.About.Description,
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( color: await getColors(
`https://i.ytimg.com/vi/${v}/maxresdefault.jpg` `https://i.ytimg.com/vi/${v}/maxresdefault.jpg`
).then((colors) => colors[0].hex()), ).then((colors) => colors[0].hex()),
channel: tj, b: nightlyJsonData !== null,
b: true, ...(nightlyJsonData !== null
? {
beta: nightlyJsonData,
badges: nightlyJsonData.channel.badges[0],
comments: nightlyJsonData.commentCount
}
: {})
}; };
}
}
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
}; };