poke/src/libpoketube/init/pages-video.js

567 lines
15 KiB
JavaScript
Raw Normal View History

2023-09-06 22:50:43 +02:00
const {
fetcher,
2024-02-16 16:52:22 +01:00
INNERTUBE,
2023-09-06 22:50:43 +02:00
wiki,
musicInfo,
modules,
version,
initlog,
init,
} = require("../libpoketube-initsys.js");
const {
IsJsonString,
convert,
getFirstLine,
capitalizeFirstLetter,
turntomins,
getRandomInt,
getRandomArbitrary,
} = require("../ptutils/libpt-coreutils.js");
2023-01-10 17:00:45 +01:00
const media_proxy = require("../libpoketube-video.js");
2023-02-15 18:27:32 +01:00
const atmos = require("../../../pokeatmosurls.json");
2023-12-10 11:09:25 +01:00
const config = require("../../../config.json");
2022-11-09 17:54:31 +01:00
2023-04-24 19:01:04 +02:00
function linkify(text) {
2023-09-06 22:50:43 +02:00
// regular expression to match URLs
const urlRegex = /(https?:\/\/[^\s]+)/g;
return text.replace(urlRegex, (url) => {
2023-04-24 19:01:04 +02:00
// wrap the URL in an <a> tag with the URL as the href attribute
2023-12-11 23:13:28 +01:00
return `<a href="/api/redirect?u=${btoa(
url
2023-09-06 22:50:43 +02:00
.replace(/twitter\.com/g, "nitter.net")
.replace(/reddit\.com/g, "teddit.net")
.replace("https://youtube.com", "")
.replace("https://youtu.be", "")
)}" target="_blank">${url}</a>`;
});
}
2023-04-24 19:01:04 +02:00
2023-09-13 21:06:20 +02:00
function channelurlfixer(text) {
2023-12-11 23:13:28 +01:00
// Create a regular expression to match <a> tags with href containing "/channel/"
const regex = /<a\s+href="\/channel\/([^"]+)"/g;
// Replace matching <a> tags with the modified href attribute
const updatedDescription = text.replace(regex, '<a href="/channel?id=$1"');
return updatedDescription;
}
2023-09-13 21:06:20 +02:00
2023-09-13 19:26:55 +02:00
function endsWithYouTubeAutoGenerated(text) {
const target = " Auto-generated by YouTube. ";
return text.endsWith(target);
}
2022-11-09 17:54:31 +01:00
const sha384 = modules.hash;
2022-12-08 19:26:04 +01:00
const fetch = modules.fetch;
const htmlToText = require("html-to-text");
const encoding = require("encoding");
const delim1 =
'</div></div></div></div><div class="hwc"><div class="BNeawe tAd8D AP7Wnd"><div><div class="BNeawe tAd8D AP7Wnd">';
const delim2 =
'</div></div></div></div></div><div><span class="hwc"><div class="BNeawe uEec3 AP7Wnd">';
const url = "https://www.google.com/search?q=";
async function lyricsFinder(e = "", d = "") {
let i;
try {
i = await fetch(`${url}${encodeURIComponent(d + " " + e)}+lyrics`);
i = await i.textConverted();
[, i] = i.split(delim1);
[i] = i.split(delim2);
} catch (m) {
try {
i = await fetch(`${url}${encodeURIComponent(d + " " + e)}+song+lyrics`);
i = await i.textConverted();
[, i] = i.split(delim1);
[i] = i.split(delim2);
} catch (n) {
try {
i = await fetch(`${url}${encodeURIComponent(d + " " + e)}+song`);
i = await i.textConverted();
[, i] = i.split(delim1);
[i] = i.split(delim2);
} catch (o) {
try {
i = await fetch(`${url}${encodeURIComponent(d + " " + e)}`);
i = await i.textConverted();
[, i] = i.split(delim1);
[i] = i.split(delim2);
} catch (p) {
i = "";
}
}
}
}
const ret = i.split("\n");
let final = "";
for (let j = 0; j < ret.length; j += 1) {
final = `${final}${htmlToText.fromString(ret[j])}\n`;
}
return String(encoding.convert(final)).trim();
}
2022-11-24 21:26:41 +01:00
2023-02-15 18:27:32 +01:00
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i) if (arr[i] !== undefined) rv[i] = arr[i];
return rv;
}
2022-12-16 23:18:03 +01:00
function lightOrDark(color) {
// Variables for red, green, blue values
var r, g, b, hsp;
// Check the format of the color, HEX or RGB?
if (color.match(/^rgb/)) {
// If RGB --> store the red, green, blue values in separate variables
color = color.match(
/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/
2022-11-24 21:26:41 +01:00
);
2022-12-16 23:18:03 +01:00
r = color[1];
g = color[2];
b = color[3];
} else {
// If hex --> Convert it to RGB: http://gist.github.com/983661
color = +("0x" + color.slice(1).replace(color.length < 5 && /./g, "$&$&"));
r = color >> 16;
g = (color >> 8) & 255;
b = color & 255;
}
2022-11-24 21:26:41 +01:00
2022-12-16 23:18:03 +01:00
// HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html
hsp = Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b));
2022-11-24 21:26:41 +01:00
2022-12-16 23:18:03 +01:00
// Using the HSP value, determine whether the color is light or dark
if (hsp > 127.5) {
return "light";
} else {
return "dark";
}
2022-11-24 21:26:41 +01:00
}
2024-02-09 21:37:04 +01:00
function isDntEnabled(req) {
const dntHeader = req.header('DNT');
return dntHeader && (dntHeader === '1' || dntHeader === 'true');
}
2022-12-25 17:12:56 +01:00
function IsInArray(array, id) {
for (var i = 0; i < array.length; i++) {
if (array[i].id === id) return true;
}
return false;
}
2023-01-07 14:38:36 +01:00
function getJson(str) {
try {
return JSON.parse(str);
} catch {
return false;
}
}
2023-09-06 22:50:43 +02:00
/* support button */
2023-09-07 19:14:47 +02:00
const PATREON_REGEX = /https:\/\/(?:www\.)?patreon.com\/(?<name>[\w\d_-]+)/;
2023-02-20 16:31:11 +01:00
2023-09-06 22:50:43 +02:00
/* connections */
const X_REGEX = /https:\/\/twitter.com\/(?<name>[\w\d_-]+)/;
const CORD_REGEX = /https:\/\/discord.gg\/(?<name>[\w\d_-]+)/;
const TWITCH_REGEX = /https:\/\/twitch.tv\/(?<name>[\w\d_-]+)/;
const REDDIT_REGEX = /https:\/\/reddit\.com\/r\/(?<name>[\w\d_-]+)/;
const INSTAGRAM_REGEX = /https:\/\/www.instagram.com\/(?<name>[\w\d_-]+)/;
2022-11-09 17:54:31 +01:00
module.exports = function (app, config, renderTemplate) {
app.get("/encryption", async function (req, res) {
2023-09-06 22:50:43 +02:00
res.json("error in parsing");
});
2023-09-06 22:50:43 +02:00
app.get("/old", function (req, res) {
2023-08-29 11:08:00 +02:00
const v = req.query.v;
renderTemplate(res, req, "poketube-old.ejs", {
v,
});
});
2023-09-06 22:50:43 +02:00
2023-02-25 18:20:38 +01:00
app.get("/watch", async (req, res) => {
2024-01-19 13:51:15 +01:00
const { dm, region, hl, v, e, r, f, m, quality: q, a, universe, } = req.query;
2023-02-25 18:20:38 +01:00
if (!v) {
return res.redirect("/");
}
2023-12-11 23:13:28 +01:00
var contentlang = hl || "en-US";
var contentregion = region || "US";
2023-02-25 18:20:38 +01:00
2024-02-16 16:52:22 +01:00
const isVideoValid = await INNERTUBE.isvalidvideo(v);
2023-02-25 18:20:38 +01:00
if (!isVideoValid) {
2023-04-19 20:55:04 +02:00
return res.redirect("/?fromerror=21_video_not_valid");
2023-02-25 18:20:38 +01:00
}
2023-01-07 15:52:46 +01:00
2023-01-10 17:00:45 +01:00
const u = await media_proxy(v);
2023-12-11 23:13:28 +01:00
2023-09-06 22:50:43 +02:00
const secure = ["poketube.fun"].includes(req.hostname);
2023-11-04 09:36:36 +01:00
const verify = req.hostname === "poketube.sudovanilla.com";
2024-02-01 12:30:51 +01:00
2024-03-14 18:23:14 +01:00
INNERTUBE.getYouTubeApiVideo(f, v, contentlang, contentregion).then((data) => {
2023-02-25 18:20:38 +01:00
try {
2023-04-20 00:04:36 +02:00
const k = data?.video;
2024-02-01 12:30:51 +01:00
const channel_uploads = data?.channel_uploads
2023-07-01 21:07:50 +02:00
const json = data?.json;
2023-08-09 20:46:14 +02:00
const engagement = data?.engagement;
const inv_comments = data?.comments || "Disabled";
const inv_vid = data?.vid;
const desc = data?.desc || "";
2023-12-11 23:13:28 +01:00
2023-02-25 18:20:38 +01:00
let d = false;
if (desc !== "[object Object]") {
d = desc.toString().replace(/\n/g, " <br> ");
}
2024-02-01 12:30:51 +01:00
2023-01-31 21:36:13 +01:00
2023-09-13 19:26:55 +02:00
const descriptionString = String(inv_vid?.description);
2023-12-11 23:13:28 +01:00
2023-09-06 22:50:43 +02:00
function extractInfo(regex) {
return descriptionString !== "[object Object]"
? (regex.exec(descriptionString) ?? {}).groups
: undefined;
}
const support = extractInfo(PATREON_REGEX);
const twitter = extractInfo(X_REGEX);
const discord = extractInfo(CORD_REGEX);
const twitch = extractInfo(TWITCH_REGEX);
const reddit = extractInfo(REDDIT_REGEX);
const instagram = extractInfo(INSTAGRAM_REGEX);
2024-01-19 13:51:15 +01:00
2024-02-01 12:30:51 +01:00
2024-01-19 13:51:15 +01:00
var proxyurl = config.p_url;
2023-12-11 23:13:28 +01:00
var vidurl = u.url;
var isvidious = u.isInvidiousURL;
2024-01-05 17:16:08 +01:00
var mediaproxy = config.media_proxy
2024-01-19 13:51:15 +01:00
2024-02-01 12:30:51 +01:00
2023-12-11 23:13:28 +01:00
if (inv_vid?.genre === "Music") {
var vidurl = u.losslessurl;
}
2024-02-01 11:10:04 +01:00
var vidurl = "https://eu-proxy.poketube.fun";
var isvidious = true;
2024-01-28 09:27:43 +01:00
2023-12-11 23:13:28 +01:00
if (req.useragent.source.includes("Pardus")) {
2024-02-07 06:27:20 +01:00
var vidurl = "https://iv.ggtyler.dev";
2024-01-05 17:16:08 +01:00
var mediaproxy = "https://media-proxy.ashley0143.xyz"
2024-02-07 06:27:20 +01:00
var isvidious = true;
2024-02-07 09:00:42 +01:00
var isSchoolProxy = "";
2023-11-04 09:36:36 +01:00
}
2024-01-05 15:03:36 +01:00
2024-02-09 21:37:04 +01:00
// unused
2023-02-25 18:20:38 +01:00
let badges = "";
let comments = "";
let nnn = "";
2023-09-06 22:50:43 +02:00
2024-02-09 21:37:04 +01:00
const dnt_val = isDntEnabled(req)
2023-09-06 22:50:43 +02:00
if (
inv_vid?.error ===
"The uploader has not made this video available in your country" ||
inv_vid?.error === "This video is not available"
) {
res.send(
"error: " + inv_vid.error + " please refresh the page please qt"
);
}
var uaos = req.useragent.os;
2023-05-21 22:21:13 +02:00
const browser = req.useragent.browser;
2024-01-19 13:51:15 +01:00
const IsOldWindows =
(uaos === "Windows 7" || uaos === "Windows 8") &&
browser === "Firefox";
2023-05-21 22:21:13 +02:00
2023-09-06 22:50:43 +02:00
if (uaos === "Windows XP" || uaos === "Windows Vista")
res.redirect("/lite?v=" + req.query.v);
2023-06-13 20:26:11 +02:00
2023-12-11 23:13:28 +01:00
if (req.query.from === "short") var shortsui = true;
2023-08-05 22:42:05 +02:00
try {
2023-09-06 22:50:43 +02:00
renderTemplate(res, req, "poketube.ejs", {
color: data.color,
color2: data.color2,
linkify,
engagement,
IsOldWindows,
2023-09-13 21:06:20 +02:00
channelurlfixer,
2023-09-06 22:50:43 +02:00
support,
2023-12-07 20:52:19 +01:00
shortsui,
2023-11-04 09:36:36 +01:00
u: vidurl,
2023-12-11 23:13:28 +01:00
isvidious: isvidious,
2023-09-06 22:50:43 +02:00
video: json,
date: k.Video.uploadDate,
e,
a,
twitter,
k,
dm,
2024-01-19 13:51:15 +01:00
proxyurl,
2024-01-05 17:16:08 +01:00
media_proxy_url: mediaproxy,
2023-09-06 22:50:43 +02:00
instagram,
useragent: req.useragent,
verify,
discord,
2024-02-01 11:10:04 +01:00
turntomins,
2023-09-06 22:50:43 +02:00
twitch,
2024-02-09 21:37:04 +01:00
dnt_val,
2023-09-06 22:50:43 +02:00
reddit,
2024-02-01 12:30:51 +01:00
channel_uploads,
2023-09-06 22:50:43 +02:00
secure,
process,
2024-01-05 17:16:08 +01:00
isSchoolProxy,
2023-09-06 22:50:43 +02:00
sha384,
lightOrDark,
isMobile: req.useragent.isMobile,
tj: data.channel,
r,
qua: q,
inv: inv_comments,
convert,
universe,
wiki: data.wiki,
f,
t: config.t_url,
optout: m,
badges,
desc,
comments,
n: nnn,
inv_vid,
lyrics: "",
});
2023-08-05 22:42:05 +02:00
} catch {
2023-09-06 22:50:43 +02:00
return;
2023-08-05 22:42:05 +02:00
}
2023-09-06 22:50:43 +02:00
} catch (error) {
console.error(error);
2023-12-11 23:13:28 +01:00
return res.redirect(`/watch?v=${req.query.v}&fx=1&err=${error}`);
2023-01-07 15:52:46 +01:00
}
2023-09-06 22:50:43 +02:00
});
2022-11-30 19:49:09 +01:00
});
app.get("/lite", async function (req, res) {
2023-02-25 18:20:38 +01:00
const { v, e, r, f, t, quality: q } = req.query;
2022-11-30 19:49:09 +01:00
2023-02-25 18:20:38 +01:00
try {
2024-02-01 17:49:29 +01:00
var mediaproxy = config.media_proxy
2024-03-29 16:16:03 +01:00
var contentlang "en-US";
var contentregion = "US";
2023-02-25 18:20:38 +01:00
2023-09-06 22:50:43 +02:00
const {
video: k,
json,
engagement,
comments: inv_comments,
vid: inv_vid,
2024-03-29 16:16:03 +01:00
} = await INNERTUBE?.getYouTubeApiVideo(f, v, contentlang, contentregion);
2023-02-25 18:20:38 +01:00
2024-03-29 16:16:03 +01:00
const data = await INNERTUBE?.getYouTubeApiVideo(f, v, contentlang, contentregion);
2023-02-25 18:20:38 +01:00
const color = data.color;
const color2 = data.color2;
2023-11-12 18:26:24 +01:00
const desc = data?.desc;
2023-02-25 18:20:38 +01:00
const isMobile = req.useragent.isMobile;
const wiki = data.wiki;
const { channel: tj } = data;
2023-04-28 19:51:25 +02:00
const u = await media_proxy(v);
2023-02-25 18:20:38 +01:00
const d = desc.toString().replace(/\n/g, " <br> ");
const comments = inv_comments || "Disabled";
2024-02-01 17:49:29 +01:00
const channel_uploads = data?.channel_uploads
2023-02-25 18:20:38 +01:00
const templateData = {
color,
color2,
engagement,
2024-02-01 17:49:29 +01:00
channel_uploads,
2023-09-06 22:50:43 +02:00
u: u.url,
2023-02-25 18:20:38 +01:00
video: json,
date: k.Video.uploadDate,
e,
k,
process,
sha384,
lightOrDark,
isMobile,
tj,
2024-02-01 17:49:29 +01:00
media_proxy_url: mediaproxy,
2023-02-25 18:20:38 +01:00
r,
qua: q,
2023-09-06 22:50:43 +02:00
isvidious: u.isInvidiousURL,
2023-02-25 18:20:38 +01:00
inv: comments,
2024-02-01 17:49:29 +01:00
turntomins,
2023-02-25 18:20:38 +01:00
convert,
2023-04-24 19:01:04 +02:00
linkify,
2023-02-25 18:20:38 +01:00
wiki,
f,
t: config.t_url,
optout: t,
badges: "",
desc,
comments,
n: "",
inv_vid,
lyrics: "",
};
renderTemplate(res, req, "lite.ejs", templateData);
} catch (error) {
console.error(error);
2023-04-19 20:55:04 +02:00
res.redirect("/?err=" + error);
}
});
2022-11-09 17:54:31 +01:00
app.get("/music", async function (req, res) {
/*
* QUERYS
* v = Video ID
* e = Embed
* r = Recommended videos
* f = Recent videos from channel
* t = Piwik OptOut
* q = quality obv
*/
var v = req.query.v;
var e = req.query.e;
var r = req.query.r;
var f = req.query.f;
var t = req.query.t;
2022-11-09 17:54:31 +01:00
const info = await modules.fetch("http://ip-api.com/json/");
const n = await info.text();
const ip = JSON.parse(n);
2022-11-09 17:54:31 +01:00
2023-01-05 16:49:55 +01:00
if (!v) {
res.redirect("/discover?tab=music");
} else {
2023-01-07 15:52:46 +01:00
var fetching = await fetcher(v);
2022-11-09 17:54:31 +01:00
2023-01-07 15:52:46 +01:00
const json = fetching.video.Player;
2023-01-07 15:52:46 +01:00
const video = await modules.fetch(config.tubeApi + `video?v=${v}`);
2023-01-07 15:52:46 +01:00
const h = await video.text();
const k = JSON.parse(modules.toJson(h));
2023-01-07 15:52:46 +01:00
if (!json.Channel.Name.endsWith(" - Topic")) {
res.redirect(`/watch?v=${v}`);
}
2022-12-16 23:18:03 +01:00
2023-02-15 18:27:32 +01:00
if (req.useragent.isMobile) {
res.redirect(`/watch?v=${v}`);
}
2023-01-07 15:52:46 +01:00
//video
var url = `https://tube.kuylar.dev/proxy/media/${v}/22`;
// encryption
2023-02-15 18:27:32 +01:00
var url_e =
2023-01-07 15:52:46 +01:00
url +
"?e=" +
sha384(k.Video.Channel.id) +
sha384(k.Video.Channel.id) +
"Piwik" +
sha384(config.t_url);
2023-02-15 18:27:32 +01:00
const stringed = toObject(atmos);
2023-02-25 18:20:38 +01:00
const search = (what) => atmos.find((element) => element.id === what);
const mos = search(v);
/*
2023-02-17 22:32:37 +01:00
this is only for the alac codec being used
* Copyright (c) 2023 Apple Inc. All rights reserved.
*
* @APPLE_APACHE_LICENSE_HEADER_START@
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @APPLE_APACHE_LICENSE_HEADER_END@
*/
2023-02-25 18:20:38 +01:00
2023-02-15 18:27:32 +01:00
if (mos) {
var url_e =
mos.url +
"?e=" +
sha384(k.Video.Channel.id) +
sha384(k.Video.Channel.id) +
"Piwik" +
sha384(config.t_url);
} else {
}
2023-01-07 15:52:46 +01:00
// channel info
const engagement = fetching.engagement;
const channel = await modules.fetch(
config.tubeApi + `channel?id=${k.Video.Channel.id}&tab=videos`
);
const c = await channel.text();
const tj = JSON.parse(modules.toJson(c));
2023-01-19 21:41:35 +01:00
try {
2023-01-21 19:00:51 +01:00
// info
const song = await musicInfo.searchSong(
{
title: k.Video.Title,
artist: json.Channel.Name.replace("- Topic", ""),
},
1000
);
if (!song) {
res.redirect(`/watch?v=${v}`);
}
2023-01-19 21:41:35 +01:00
const lyrics = await lyricsFinder(song.artist + song.title);
if (lyrics == undefined)
ly = "This Is Where I'd Put The songs lyrics. IF IT HAD ONE ";
2023-01-19 21:41:35 +01:00
var ly = "";
if (lyrics) {
ly = lyrics.replace(/\n/g, " <br> ");
}
2023-01-19 21:41:35 +01:00
renderTemplate(res, req, "poketube-music.ejs", {
url: url_e,
info: song,
color: await modules
2023-01-21 19:34:57 +01:00
.getColors(song.artwork)
2023-01-19 21:41:35 +01:00
.then((colors) => colors[0].hex()),
engagement: engagement,
process: process,
ip: ip,
video: json,
date: modules.moment(k.Video.uploadDate).format("LL"),
e: e,
k: k,
sha384: sha384,
isMobile: req.useragent.isMobile,
tj: tj,
r: r,
f: f,
t: config.t_url,
optout: t,
lyrics: ly,
});
} catch {
return res.redirect("/?fromerror=43");
2023-01-07 15:52:46 +01:00
}
2023-01-05 16:49:55 +01:00
}
2022-11-09 17:54:31 +01:00
});
2024-02-07 09:00:42 +01:00
};