const express = require("express"), path = require("path"), fs = require("fs"), ytjs = require('youtubei.js') const PORT = process.env.PORT || 8080 var app = express() var resources = path.join(__dirname, 'resources') var staticpath = path.join(__dirname, 'static') app.listen(PORT, () => { console.log("SimpleTube is now listening on port " + PORT) }) app.use(express.static(staticpath)) var thumbCount = 0 var thumborInstances = [ "https://thumbor-production-0e82.up.railway.app/", "https://enormous-book-production.up.railway.app/", "https://unusual-back-production.up.railway.app/", "https://axiomatic-hair-production.up.railway.app/" ] function getThumbor() { thumbCount += 1 return thumborInstances[thumbCount % thumborInstances.length] + "unsafe" } function searchResultToHTML(results) { var addedHTML = "" for (let index = 0; index < results.length; index++) { const result = results[index].content; if (result && result.type == "Video" && result.published.text) { addedHTML += `
${result.author.name}
` } } return addedHTML } app.get("/", async (req, res) => { var innerTube = await ytjs.Innertube.create() res.setHeader("Content-Type", "text/html") res.setHeader("X-Accel-Buffering", "no") var html = fs.readFileSync(path.join(resources, 'mainPage.html')).toString() res.write(html.substring(0, html.indexOf("{RESULTS}"))) var results = (await innerTube.getHomeFeed()).contents.contents var addedHTML = searchResultToHTML(results) res.write(addedHTML + html.substring(html.indexOf("{RESULTS}") + 9), () => {res.end()}) }) process.on('uncaughtException', (err, origin) => { fs.writeSync( process.stderr.fd, `Caught exception: ${err}\n` + `Exception origin: ${origin}`, ); });