import express from "express"; import logRequest from "./logger.js"; import { parse } from "node-html-parser"; import { format } from "util"; const config = { FUCKING_ENDPOINT: process.env.FUCKING_ENDPOINT ?? "https://publish.twitter.com/oembed?url=", TWITTER_BASE_URL: process.env.TWITTER_BASE_URL ?? "https://x.com/", title: process.env.APP_TITLE ?? "biter", description: process.env.APP_TITLE ?? "biter twitter proxy", }; const app = express(); app.set("view engine", "ejs"); app.set("views", "views"); app.use(logRequest); app.use(express.static("static")); app.get("/*/status/*", async (req, res) => { let target = config.FUCKING_ENDPOINT + encodeURIComponent(config.TWITTER_BASE_URL + req.url.slice(1)); let twitter_res; try { twitter_res = await fetch(target); } catch (err) { return res .status(502) .send("Error from Twitter API: " + format("%s", err)); } if (twitter_res.status == 200) { let body; try { body = await twitter_res.json(); } catch (err) { return res .status(502) .send("Error from Twitter API: " + format("%s", err)); } let html = parse(body.html); let block = html.firstChild; let content = block.childNodes[0].innerText; let author = block.childNodes[1].toString(); let [_, authorName, authorHandle] = author.match( /— (.+) \(@([\w\d_]+)\)/ ); let authorUrl = config.TWITTER_BASE_URL + authorHandle; res.render("displayTweet", { title: config.title, description: config.description, authorName, authorHandle, authorUrl, content, url: config.TWITTER_BASE_URL + req.url.slice(1), }); } else { return res.sendStatus(502); } }); app.get("/", (req, res) => res.render("index", { title: config.title, description: config.description, }) ); let server = app.listen(process.env.PORT ?? 3000, () => { let address = server.address(); let ip = address.address; if (address.family === "IPv6") ip = `[${ip}]`; console.log(`listening on http://${ip}:${address.port}/`); });