SimpleTube/index.js

108 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-10-26 12:35:34 -05:00
const express = require("express"),
path = require("path"),
fs = require("fs"),
2024-02-13 16:04:23 -06:00
ytjs = require('youtubei.js')
2023-10-26 12:35:34 -05:00
const PORT = process.env.PORT || 8080
var app = express()
2024-02-13 16:04:23 -06:00
var resources = path.join(__dirname, 'resources')
var staticpath = path.join(__dirname, 'static')
2023-10-26 12:35:34 -05:00
app.listen(PORT, () => {
2024-02-13 16:04:23 -06:00
console.log("SimpleTube is now listening on port " + PORT)
2023-10-26 12:35:34 -05:00
})
2024-02-13 16:04:23 -06:00
app.use(express.static(staticpath))
2024-02-13 16:04:23 -06:00
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/"
]
2024-02-13 16:04:23 -06:00
function getThumbor() {
thumbCount += 1
return thumborInstances[thumbCount % thumborInstances.length] + "unsafe"
}
2024-02-13 16:04:23 -06:00
function searchResultToHTML(results) {
var addedHTML = ""
for (let index = 0; index < results.length; index++) {
2024-02-13 18:02:32 -06:00
const result = results[index].content || results[index];
if (result && result.type == "Video" && result.published) {
2024-02-13 16:04:23 -06:00
addedHTML += `
<div class="col-xxl-4 col-sm-6 resultContainer">
<div class="videoResult container-fluid row">
<div class="col-lg-6 thumbparent">
<a class="videoLink" href="/watch?v=${result.id}">
<img class="thumbnail" src="${getThumbor()}/${result.thumbnails[0].url}">
<p style="display: block; text-align: left;">${result.duration.text}</p>
</a>
</div>
<div class="col-lg-6">
<a class="videoLink" href="/watch?v=${result.id}">
<p style="font-size: 1.25rem;">${result.title.text || "No Title Found"}</p>
2024-02-13 18:02:32 -06:00
<p class="resultDescription">${(result.description_snippet || result.snippets[0].text.runs[0]).text}</p>
2024-02-13 16:04:23 -06:00
<p style="display: block;">${result.published.text}</p>
</a>
</div>
<div style="display: inline-block; width: 100%;">
<a style="color: white; margin: 10px; display: inline-block;" href="/channel?q=${result.author.id}">
<img src="${getThumbor()}/${result.author.thumbnails[0].url}" class="minipfp">
${result.author.name}
</a>
</div>
</div>
</div>
2024-02-13 16:04:23 -06:00
`
2023-11-19 23:52:48 -06:00
}
2023-11-19 22:40:21 -06:00
}
2024-02-13 16:04:23 -06:00
return addedHTML
2023-10-29 23:28:17 -05:00
}
2023-11-27 22:39:06 -06:00
app.get("/", async (req, res) => {
2024-02-13 16:04:23 -06:00
var innerTube = await ytjs.Innertube.create()
2023-11-27 22:39:06 -06:00
2023-10-28 19:08:28 -05:00
res.setHeader("Content-Type", "text/html")
2023-10-26 12:35:34 -05:00
res.setHeader("X-Accel-Buffering", "no")
2024-02-13 18:02:32 -06:00
2024-02-13 16:04:23 -06:00
var html = fs.readFileSync(path.join(resources, 'mainPage.html')).toString()
2023-11-20 10:55:41 -06:00
2024-02-13 16:04:23 -06:00
res.write(html.substring(0, html.indexOf("{RESULTS}")))
2023-10-27 00:38:23 -05:00
2024-02-13 16:04:23 -06:00
var results = (await innerTube.getHomeFeed()).contents.contents
2023-10-27 00:38:23 -05:00
2024-02-13 16:04:23 -06:00
var addedHTML = searchResultToHTML(results)
2023-10-26 12:35:34 -05:00
2024-02-13 16:04:23 -06:00
res.write(addedHTML + html.substring(html.indexOf("{RESULTS}") + 9), () => {res.end()})
2023-10-26 12:35:34 -05:00
})
2024-02-13 18:02:32 -06:00
app.get("/search", 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, 'searchPage.html')).toString()
res.write(html.substring(0, html.indexOf("{RESULTS}")))
var results = (await innerTube.search('test'))
var addedHTML = searchResultToHTML(results.results)
res.write(addedHTML + html.substring(html.indexOf("{RESULTS}") + 9), () => {res.end()})
})
2023-11-27 22:39:06 -06:00
process.on('uncaughtException', (err, origin) => {
fs.writeSync(
process.stderr.fd,
`Caught exception: ${err}\n` +
`Exception origin: ${origin}`,
);
});