Add some content

This commit is contained in:
Danny Spina 2023-06-20 09:32:07 +02:00
parent 33b9407b66
commit b8ad4078de

View File

@ -4,7 +4,7 @@ import { JSDOM } from "jsdom";
import got from "got"; import got from "got";
import path from "path"; import path from "path";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
import fetch from 'node-fetch' import fetch from "node-fetch";
import "dotenv/config"; import "dotenv/config";
const app = express(); const app = express();
@ -21,10 +21,10 @@ app.get("/", async (req, res) => {
if (!query) { if (!query) {
res.sendFile(path.join(__dirname + "/dist/index.html")); res.sendFile(path.join(__dirname + "/dist/index.html"));
return return;
} }
const key = process.env.CYCLIC_BRAVE_KEY const key = process.env.CYCLIC_BRAVE_KEY;
if (!key) { if (!key) {
throw new Error("No brave key found"); throw new Error("No brave key found");
@ -32,33 +32,37 @@ app.get("/", async (req, res) => {
fetch(`${searchEngine}?q=${query}`, { fetch(`${searchEngine}?q=${query}`, {
headers: { headers: {
"Accept": "*/*", Accept: "*/*",
"Accept-Encoding": "gzip, deflate, br", "Accept-Encoding": "gzip, deflate, br",
"X-Subscription-Token": key, "X-Subscription-Token": key,
}, },
}) })
.then((page) => { .then((page) => {
page.json().then(response => { page.json().then((response) => {
const results: any[] = [] const results: any[] = [];
const url = process.env.DEV_MODE
? "http://localhost:8888/blazed"
: "https://blaze.cyclic.app/blazed";
// @ts-ignore // @ts-ignore
response.web.results.forEach(result => { response.web.results.forEach((result) => {
results.push(` results.push(`
<div> <div>
<a href="${url}?url=${result.url}">
<h2>${result.title}</h2> <h2>${result.title}</h2>
<span>${result.meta_url.hostname}</span>
<a href="https://blaze.cyclic.app/blazed?url=${result.url}">
<p>${result.description}</p>
</a> </a>
<span>${result.meta_url.hostname}</span>
<p>${result.description}</p>
</div> </div>
<hr /> <hr />
`); `);
}) });
res.send(`<html> res.send(`<html>
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1"> <meta name="viewport" content="width=device-width,initial-scale=1">
<title>Blaze The Page</title> <title>Blaze - ${query}</title>
<style> <style>
body {font-family:sans-serif}
h2 {margin-bottom:0} h2 {margin-bottom:0}
span {font-size:.9rem} span {font-size:.9rem}
</style> </style>
@ -67,7 +71,7 @@ app.get("/", async (req, res) => {
${results.join("")} ${results.join("")}
</body> </body>
</html>`); </html>`);
}) });
}) })
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);
@ -75,14 +79,14 @@ app.get("/", async (req, res) => {
}); });
app.get("/blazed", (req, res) => { app.get("/blazed", (req, res) => {
const pageToBlaze = req.query.url as string const pageToBlaze = req.query.url as string;
got(pageToBlaze) got(pageToBlaze)
.then((response) => { .then((response) => {
const dom = new JSDOM(response.body); const dom = new JSDOM(response.body);
if (!isProbablyReaderable(dom.window.document)) { if (!isProbablyReaderable(dom.window.document)) {
res.send("This page can not be blazed... Sorry!") res.sendFile(path.join(__dirname + "/dist/not_blazed.html"));
return; return;
} }
@ -99,11 +103,15 @@ got(pageToBlaze)
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);
}); });
}) });
app.get("/info", (_, res) => { app.get("/info", (_, res) => {
res.sendFile(path.join(__dirname + "/dist/info.html")); res.sendFile(path.join(__dirname + "/dist/info.html"));
}) });
app.get("/ooops", (_, res) => {
res.sendFile(path.join(__dirname + "/dist/info_not_blazed.html"));
});
app.listen(port, () => { app.listen(port, () => {
console.log(`Got request`); console.log(`Got request`);