reformat file

This commit is contained in:
Ashley 2022-09-29 21:15:42 +02:00
parent 99501793c2
commit 84f8759522

156
server.js
View file

@ -93,18 +93,17 @@ function IsJsonString(str) {
} }
function convert(value) { function convert(value) {
return new Intl.NumberFormat('en-GB', { return new Intl.NumberFormat("en-GB", {
notation: "compact" notation: "compact",
}).format(value); }).format(value);
} }
function getFirstLine(text) { function getFirstLine(text) {
var index = text.indexOf("<br> "); var index = text.indexOf("<br> ");
if (index === -1) index = undefined; if (index === -1) index = undefined;
return text.substring(0, index); return text.substring(0, index);
} }
///////////////////////////////// /////////////////////////////////
app.use(function (req, res, next) { app.use(function (req, res, next) {
@ -174,21 +173,32 @@ app.get("/watch", async function (req, res) {
const info = await fetch("http://ip-api.com/json/"); const info = await fetch("http://ip-api.com/json/");
const jj = await info.text(); const jj = await info.text();
const ip = JSON.parse(jj); const ip = JSON.parse(jj);
var badges = ""; var badges = "";
// try few times, thanks kuylar // try few times, thanks kuylar
for (let i = 0; i < 5; i++) { for (let i = 0; i < 3; 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}`
); );
var n = await nightly.text(); var n = await nightly.text();
if (n == undefined) {
if(n == undefined) { for (let i = 0; i < 6; i++) {
for(let i=0;i<3;i+=1){try{const nightly=await fetch(`https://lighttube-nightly.kuylar.dev/api/video?v=${ v }`);var n=await nightly.text()}catch(err){if(err.status===500){await new Promise((resolve)=>setTimeout(resolve,1000))}else{return(n="")}}} try {
} const nightly = await fetch(
`https://lighttube-nightly.kuylar.dev/api/video?v=${v}`
);
var n = await nightly.text();
} catch (err) {
if (err.status === 500) {
await new Promise((resolve) => setTimeout(resolve, 1000));
} else {
return (n = "");
}
}
}
}
} catch (err) { } catch (err) {
if (err.status === 500) { if (err.status === 500) {
// retry after a bit // retry after a bit
@ -204,14 +214,14 @@ app.get("/watch", async function (req, res) {
var comments = ""; var comments = "";
if (n == "") { if (n == "") {
badges, nnn, comments = ""; badges, nnn, (comments = "");
} }
if (IsJsonString(n)) { if (IsJsonString(n)) {
if (n != "") { if (n != "") {
nnn = JSON.parse(n); nnn = JSON.parse(n);
badges = nnn.channel.badges[0]; badges = nnn.channel.badges[0];
comments = nnn.commentCount; comments = nnn.commentCount;
} }
} }
@ -244,7 +254,7 @@ app.get("/watch", async function (req, res) {
); );
const c = await channel.text(); const c = await channel.text();
const tj = JSON.parse(toJson(c)); const tj = JSON.parse(toJson(c));
// about // about
const abtchnl = await fetch( const abtchnl = await fetch(
config.tubeApi + `channel?id=${k.Video.Channel.id}&tab=about` config.tubeApi + `channel?id=${k.Video.Channel.id}&tab=about`
@ -255,7 +265,7 @@ app.get("/watch", async function (req, res) {
const desc = a.Channel.Contents.ItemSection.About.Description; const desc = a.Channel.Contents.ItemSection.About.Description;
var d = desc.toString().replace(/\n/g, " <br> "); var d = desc.toString().replace(/\n/g, " <br> ");
if (d === "[object Object]") { if (d === "[object Object]") {
var d = false; var d = false;
} }
@ -270,34 +280,36 @@ app.get("/watch", async function (req, res) {
w = summary; w = summary;
} }
/*********** /***********
* URL = Video URl * URL = Video URl
* color= embed color * color= embed color
* engagement = engagement data * engagement = engagement data
* video = video json info * video = video json info
* date = upload date * date = upload date
* e = embed * e = embed
* k = player * k = player
* process = process information * process = process information
* sha384 = encryption * sha384 = encryption
* isMobile = to check if its mobile or not * isMobile = to check if its mobile or not
* tj = channel videos page * tj = channel videos page
* r = recommended videos * r = recommended videos
* qua = quality obv * qua = quality obv
* ip = ip info * ip = ip info
* convert = formats a number * convert = formats a number
* wiki = wikipedia info * wiki = wikipedia info
* f = recent videos from this channel * f = recent videos from this channel
* t = default piwik url * t = default piwik url
* optout = piwik optout * optout = piwik optout
* badges = channel badges * badges = channel badges
* desc = channel description * desc = channel description
* comments = comment size * comments = comment size
* nnn = nigthly stuff * nnn = nigthly stuff
*/ */
renderTemplate(res, req, "poketube.ejs", { renderTemplate(res, req, "poketube.ejs", {
url: url_e, url: url_e,
color: await getColors( `https://i.ytimg.com/vi/${v}/maxresdefault.jpg` ).then((colors) => colors[0].hex()), color: await getColors(
`https://i.ytimg.com/vi/${v}/maxresdefault.jpg`
).then((colors) => colors[0].hex()),
engagement: engagement, engagement: engagement,
video: json, video: json,
date: moment(k.Video.uploadDate).format("LL"), date: moment(k.Video.uploadDate).format("LL"),
@ -316,7 +328,7 @@ app.get("/watch", async function (req, res) {
t: config.t_url, t: config.t_url,
optout: t, optout: t,
badges: badges, badges: badges,
desc:desc, desc: desc,
comments: comments, comments: comments,
n: nnn, n: nnn,
lyrics: "", lyrics: "",
@ -349,12 +361,10 @@ app.get("/music", async function (req, res) {
var fetching = await fetcher(v); var fetching = await fetcher(v);
const json = fetching.video.Player; const json = fetching.video.Player;
const h = await video.text(); const h = await video.text();
const k = JSON.parse(toJson(h)); const k = JSON.parse(toJson(h));
if (!json.Channel.Name.endsWith(" - Topic")) { if (!json.Channel.Name.endsWith(" - Topic")) {
res.redirect(`/watch?v=${v}`); res.redirect(`/watch?v=${v}`);
} }
@ -362,7 +372,6 @@ app.get("/music", async function (req, res) {
//video //video
var url = `https://tube.kuylar.dev/proxy/media/${v}/18`; var url = `https://tube.kuylar.dev/proxy/media/${v}/18`;
// encryption // encryption
const url_e = const url_e =
url + url +
@ -372,7 +381,6 @@ app.get("/music", async function (req, res) {
"Piwik" + "Piwik" +
sha384(config.t_url); sha384(config.t_url);
// channel info // channel info
const engagement = fetching.engagement; const engagement = fetching.engagement;
const channel = await fetch( const channel = await fetch(
@ -471,10 +479,16 @@ app.get("/search", async (req, res) => {
const { toJson } = require("xml2json"); const { toJson } = require("xml2json");
const query = req.query.query; const query = req.query.query;
if(req.query.continuation){ var continuation = req.query.continuation } if (req.query.continuation) {
if(!req.query.continuation){ var continuation = "" } var continuation = req.query.continuation;
}
if (!req.query.continuation) {
var continuation = "";
}
const search = await fetch(`https://tube.kuylar.dev/api/search?query=${query}&continuation=${continuation}`); const search = await fetch(
`https://tube.kuylar.dev/api/search?query=${query}&continuation=${continuation}`
);
const text = await search.text(); const text = await search.text();
const j = JSON.parse(toJson(text)); const j = JSON.parse(toJson(text));
@ -498,18 +512,20 @@ app.get("/channel/", async (req, res) => {
const h = await bout.text(); const h = await bout.text();
const k = JSON.parse(toJson(h)); const k = JSON.parse(toJson(h));
if (req.query.continuation) {
if(req.query.continuation){ var continuation = req.query.continuation } var continuation = req.query.continuation;
if(!req.query.continuation){ var continuation = "" } }
if (!req.query.continuation) {
var continuation = "";
}
//videos //videos
const channel = await fetch(config.tubeApi + `channel?id=${ID}&tab=videos&Continuation=${continuation}`); const channel = await fetch(
config.tubeApi + `channel?id=${ID}&tab=videos&Continuation=${continuation}`
);
const c = await channel.text(); const c = await channel.text();
const tj = JSON.parse(toJson(c)); const tj = JSON.parse(toJson(c));
const summary = await wiki.summary(k.Channel.Metadata.Name); const summary = await wiki.summary(k.Channel.Metadata.Name);
var w = ""; var w = "";
@ -527,22 +543,21 @@ app.get("/channel/", async (req, res) => {
if (d === "[object Object]") { if (d === "[object Object]") {
var d = ""; var d = "";
} }
var dnoreplace = description.toString() var dnoreplace = description.toString();
if (dnoreplace === "[object Object]") { if (dnoreplace === "[object Object]") {
var dnoreplace = ""; var dnoreplace = "";
} }
renderTemplate(res, req, "channel.ejs", { renderTemplate(res, req, "channel.ejs", {
ID: ID, ID: ID,
tab: tab, tab: tab,
j: k, j: k,
tj: tj, tj: tj,
dnoreplace:dnoreplace, dnoreplace: dnoreplace,
continuation:continuation, continuation: continuation,
wiki: w, wiki: w,
getFirstLine:getFirstLine, getFirstLine: getFirstLine,
isMobile: req.useragent.isMobile, isMobile: req.useragent.isMobile,
about: k.Channel.Contents.ItemSection.About, about: k.Channel.Contents.ItemSection.About,
subs: subs:
@ -578,7 +593,6 @@ app.get("/js/:id", (req, res) => {
res.sendFile(__dirname + `/js/${req.params.id}`); res.sendFile(__dirname + `/js/${req.params.id}`);
}); });
///////////// API ///////////// ///////////// API /////////////
app.get("/embed/:v", async function (req, res) { app.get("/embed/:v", async function (req, res) {