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.
see a copy here:https://www.gnu.org/licenses/lgpl-3.0.txt
*/
*/
const fetch = require("node-fetch");
const { toJson } = require("xml2json");
@ -20,29 +19,16 @@ const wiki = require("wikipedia");
const config = {
tubeApi: "https://tube.kuylar.dev/api/",
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
function IsJsonString(str) {
function getJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
return JSON.parse(str);
} catch {
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) {
if (!id) return "Gib ID";
if (id == null) return "Gib ID";
if (cnt) {
var continuation = cnt;
}
if (!continuation) {
var continuation = "";
}
const videos = await fetch(
`${config.tubeApi}channel?id=${id}&tab=videos&continuation=${cnt || ""}`
)
.then((res) => res.text())
.then((xml) => JSON.parse(toJson(xml)));
// videos
const channel = await fetch(
config.tubeApi + `channel?id=${id}&tab=videos&continuation=${continuation}`
);
const c = await channel.text();
const videos = JSON.parse(toJson(c));
const about = await fetch(`${config.tubeApi}channel?id=${id}&tab=about`)
.then((res) => res.text())
.then((xml) => JSON.parse(toJson(xml)));
// 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,
};
return { videos, about };
}
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++) {
try {
const nightly = await fetch(
`https://lighttube-nightly.kuylar.dev/api/video?v=${v}`
);
var n = await nightly.text();
).then((res) => res.text());
nightlyRes = nightly;
break;
} catch (err) {
if (err.status === 500) {
// retry after a bit
if (err.status === 500)
// Retry after a second.
await new Promise((resolve) => setTimeout(resolve, 1000));
} else {
return (n = "");
}
else return "";
}
}
var nn = "";
var nnn = "";
var comments = "";
const video = await fetch(`${config.tubeApi}video?v=${v}`)
.then((res) => res.text())
.then((xml) => JSON.parse(toJson(xml)));
if (n == "") {
badges, nnn, (comments = "");
}
const channel = await channel(video.Video.Channel.id);
if (IsJsonString(n)) {
if (n != "") {
nnn = JSON.parse(n);
badges = nnn.channel.badges[0];
comments = nnn.commentCount;
}
}
const summary = await wiki
.summary(video.Video.Channel.Name)
.then((summary_) => (summary_.title !== "Not found." ? summary_ : "none"));
const video = await fetch(config.tubeApi + `video?v=${v}`);
const data = await fetcher(v);
const h = await video.text();
const k = JSON.parse(toJson(h));
const nightlyJsonData = nightlyRes !== "" && getJson(nightlyRes);
const tj = await channel(k.Video.Channel.id).then((data) => data.videos);
const a = await channel(k.Video.Channel.id).then((data) => data.about);
const summary = await wiki.summary(k.Video.Channel.Name);
var w = "";
if (summary.title === "Not found.") {
w = "none";
}
if (summary.title !== "Not found.") {
w = summary;
}
var fetching = await fetcher(v);
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;
return {
json: data.video.Player,
video,
engagement: data.engagement,
wiki: summary,
desc: channel.about.Channel.Contents.ItemSection.About.Description,
color: await getColors(
`https://i.ytimg.com/vi/${v}/maxresdefault.jpg`
).then((colors) => colors[0].hex()),
b: nightlyJsonData !== null,
...(nightlyJsonData !== null
? {
beta: nightlyJsonData,
badges: nightlyJsonData.channel.badges[0],
comments: nightlyJsonData.commentCount
}
: {})
};
}
async function search(query, cnt) {
if (!query) return "Gib Query";
if (query == null) return "Gib Query";
if (cnt) {
var continuation = cnt;
}
if (!cnt) {
var continuation = "";
}
const data = await fetch(
`${config.tubeApi}search?query=${query}&continuation=${cnt || ""}`
)
.then((res) => res.text())
.then((xml) => JSON.parse(toJson(xml)));
const search = await fetch(
`https://tube.kuylar.dev/api/search?query=${query}&continuation=${continuation}`
);
const text = await search.text();
const j = JSON.parse(toJson(text));
return j;
return data;
}
module.exports = {
search,
video,
channel,
channel
};