poke/p/server.js

61 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-08-15 19:41:52 +02:00
const fs = require("fs");
2022-08-15 10:54:24 +02:00
const express = require("express");
const fetch = require("node-fetch");
2022-08-15 10:27:40 +02:00
const htmlParser = require("node-html-parser");
2022-08-15 11:58:08 +02:00
const lyrics = require("./lyrics.js");
2022-08-15 10:27:40 +02:00
const app = express();
2022-08-15 10:54:24 +02:00
app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
let Proxy = async (req, res) => {
const url = "https://" + req.originalUrl.slice(10);
2022-08-15 10:27:40 +02:00
let f = await fetch(url, {
method: req.method,
});
2022-08-15 10:54:24 +02:00
if (false && f.headers.get("content-type").includes("html")) {
2022-08-15 10:27:40 +02:00
const body = await f.text();
2022-08-15 10:54:24 +02:00
if (false && !htmlParser.valid(body)) {
2022-08-15 10:27:40 +02:00
console.warn(`[ERROR] Invalid HTML at ${url}`);
f.body.pipe(res);
return;
2022-08-15 10:54:24 +02:00
}
2022-08-15 10:27:40 +02:00
const root = htmlParser.parse(body);
2022-08-15 10:54:24 +02:00
let html = root.childNodes.filter(
(x) => x.tagName && x.tagName.toLowerCase() == "html"
)[0];
if (!html) {
2022-08-15 10:27:40 +02:00
console.warn(`[ERROR] No <html> at ${url}`);
res.send(body);
return;
2022-08-15 10:54:24 +02:00
}
2022-08-15 10:27:40 +02:00
res.send(html.toString());
2022-08-15 10:54:24 +02:00
} else {
2022-08-15 10:27:40 +02:00
f.body.pipe(res);
2022-08-15 10:54:24 +02:00
}
2022-08-15 10:27:40 +02:00
};
const listener = (req, res) => {
2022-08-15 10:54:24 +02:00
Proxy(req, res);
2022-08-15 10:27:40 +02:00
};
2022-08-17 00:20:26 +02:00
app.get("/", (req, res) => res.redirect(`https://poketube.fun/watch?v=l3eww1dnd0k&trck=we_dont_lol`));
2022-08-15 11:58:08 +02:00
app.get("/api/lyrics", async (req, res) => {
const query = req.query.query;
res.json(await lyrics(query))
});
2022-08-15 10:54:24 +02:00
app.all("/*", listener);
2022-08-15 10:27:40 +02:00
2022-08-15 10:54:24 +02:00
app.listen(3000, () => {});
2022-08-15 11:58:08 +02:00