2023-06-18 21:20:52 +00:00
|
|
|
import express from "express";
|
2023-06-19 10:05:59 +00:00
|
|
|
import { Readability, isProbablyReaderable } from "@mozilla/readability";
|
2023-06-18 21:20:52 +00:00
|
|
|
import { JSDOM } from "jsdom";
|
|
|
|
import got from "got";
|
2023-06-18 22:44:17 +00:00
|
|
|
import path from "path";
|
|
|
|
import { fileURLToPath } from "url";
|
2023-06-19 11:58:21 +00:00
|
|
|
import fetch from 'node-fetch'
|
2023-06-19 12:11:22 +00:00
|
|
|
import "dotenv/config";
|
2023-06-18 21:20:52 +00:00
|
|
|
|
|
|
|
const app = express();
|
2023-06-19 10:05:59 +00:00
|
|
|
const port = 8888;
|
2023-06-18 21:20:52 +00:00
|
|
|
|
2023-06-18 22:46:43 +00:00
|
|
|
// @ts-ignore
|
2023-06-18 22:44:17 +00:00
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
2023-06-19 11:58:21 +00:00
|
|
|
app.get("/", async (req, res) => {
|
|
|
|
const searchEngine = "https://api.search.brave.com/res/v1/web/search";
|
2023-06-19 10:05:59 +00:00
|
|
|
let query = req.query.q as string;
|
2023-06-18 21:20:52 +00:00
|
|
|
|
2023-06-19 10:05:59 +00:00
|
|
|
if (!query) {
|
2023-06-18 22:44:17 +00:00
|
|
|
res.sendFile(path.join(__dirname + "/dist/index.html"));
|
2023-06-19 10:05:59 +00:00
|
|
|
return
|
2023-06-18 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-19 12:01:17 +00:00
|
|
|
const key = process.env.CYCLIC_BRAVE_KEY
|
|
|
|
|
|
|
|
if (!key) {
|
|
|
|
throw new Error("No brave key found");
|
|
|
|
}
|
|
|
|
|
2023-06-19 11:58:21 +00:00
|
|
|
fetch(`${searchEngine}?q=${query}`, {
|
2023-06-19 10:40:07 +00:00
|
|
|
headers: {
|
2023-06-19 11:58:21 +00:00
|
|
|
"Accept": "*/*",
|
|
|
|
"Accept-Encoding": "gzip, deflate, br",
|
2023-06-19 12:01:17 +00:00
|
|
|
"X-Subscription-Token": key,
|
2023-06-19 11:58:21 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((page) => {
|
|
|
|
page.json().then(response => {
|
2023-06-19 12:01:17 +00:00
|
|
|
const results: any[] = []
|
|
|
|
// @ts-ignore
|
2023-06-19 11:58:21 +00:00
|
|
|
response.web.results.forEach(result => {
|
|
|
|
results.push(`
|
|
|
|
<div>
|
|
|
|
<h2>${result.title}</h2>
|
|
|
|
<a href="https://ill-red-skunk-wig.cyclic.app/blazed?url=${result.url}">${result.url}</a>
|
2023-06-19 12:11:22 +00:00
|
|
|
<a href="https://ill-red-skunk-wig.cyclic.app/blazed?url=${result.url}">
|
2023-06-19 11:58:21 +00:00
|
|
|
<p>${result.description}</p>
|
|
|
|
</a>
|
|
|
|
</div>
|
2023-06-19 12:11:22 +00:00
|
|
|
<hr />
|
2023-06-19 11:58:21 +00:00
|
|
|
`);
|
|
|
|
})
|
2023-06-19 12:11:22 +00:00
|
|
|
res.send(`<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
|
|
<title>Blaze The Page</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
${results.join("")}
|
|
|
|
</body>
|
|
|
|
</html>`);
|
2023-06-19 10:05:59 +00:00
|
|
|
})
|
2023-06-18 21:20:52 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
});
|
2023-06-18 22:52:20 +00:00
|
|
|
|
2023-06-19 10:05:59 +00:00
|
|
|
app.get("/blazed", (req, res) => {
|
|
|
|
const pageToBlaze = req.query.url as string
|
|
|
|
|
|
|
|
got(pageToBlaze)
|
|
|
|
.then((response) => {
|
|
|
|
const dom = new JSDOM(response.body);
|
|
|
|
|
|
|
|
if (!isProbablyReaderable(dom.window.document)) {
|
|
|
|
res.send("This page can not be blazed... Sorry!")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let reader = new Readability(dom.window.document);
|
|
|
|
let article = reader.parse();
|
|
|
|
|
|
|
|
if (!article) {
|
|
|
|
res.send("Something went wrong");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send(article.content);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2023-06-18 22:52:20 +00:00
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Got request`);
|
|
|
|
});
|