mirror of
https://github.com/cupcakearmy/blaze.git
synced 2024-12-22 16:26:26 +00:00
85 lines
2.0 KiB
TypeScript
85 lines
2.0 KiB
TypeScript
import express from "express";
|
|
import { Readability, isProbablyReaderable } from "@mozilla/readability";
|
|
import { JSDOM } from "jsdom";
|
|
import got from "got";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import fetch from 'node-fetch'
|
|
|
|
const app = express();
|
|
const port = 8888;
|
|
|
|
// @ts-ignore
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
app.get("/", async (req, res) => {
|
|
const searchEngine = "https://api.search.brave.com/res/v1/web/search";
|
|
let query = req.query.q as string;
|
|
|
|
if (!query) {
|
|
res.sendFile(path.join(__dirname + "/dist/index.html"));
|
|
return
|
|
}
|
|
|
|
fetch(`${searchEngine}?q=${query}`, {
|
|
headers: {
|
|
"Accept": "*/*",
|
|
"Accept-Encoding": "gzip, deflate, br",
|
|
"X-Subscription-Token": process.env.CYCLIC_BRAVE_KEY,
|
|
},
|
|
})
|
|
.then((page) => {
|
|
page.json().then(response => {
|
|
const results = []
|
|
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>
|
|
<a href="${result.url}">
|
|
<p>${result.description}</p>
|
|
</a>
|
|
</div>
|
|
`);
|
|
})
|
|
res.send(results.join(''))
|
|
})
|
|
})
|
|
.catch((err) => {
|
|
console.log(err);
|
|
});
|
|
});
|
|
|
|
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);
|
|
});
|
|
})
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Got request`);
|
|
});
|