Add channels to search

This commit is contained in:
Bingus_Violet 2024-02-14 09:03:53 -06:00
parent b74f6ca6ee
commit ff496892d7
2 changed files with 57 additions and 11 deletions

View file

@ -30,11 +30,20 @@ function getThumbor() {
} }
function searchResultToHTML(results) { function searchResultToHTML(results) {
var videosHTML = "<h2>Videos:<br></h2>"
var channelsHTML = "<h2>Channels:<br></h2>"
var addedHTML = "" var addedHTML = ""
for (let index = 0; index < results.length; index++) { for (let index = 0; index < results.length; index++) {
const result = results[index].content || results[index]; const result = results[index].content || results[index];
if (result && result.type == "Video" && result.published) { if (result && result.type == "Video" && result.published && result.duration != "N/A") {
addedHTML += ` if (!result.description_snippet) {
if (result.snippets) {
result.description_snippet = result.snippets[0].text.runs[0]
} else {
result.description_snippet = {text: ''}
}
}
videosHTML += `
<div class="col-xxl-4 col-sm-6 resultContainer"> <div class="col-xxl-4 col-sm-6 resultContainer">
<div class="videoResult container-fluid row"> <div class="videoResult container-fluid row">
<div class="col-lg-6 thumbparent"> <div class="col-lg-6 thumbparent">
@ -46,7 +55,7 @@ function searchResultToHTML(results) {
<div class="col-lg-6"> <div class="col-lg-6">
<a class="videoLink" href="/watch?v=${result.id}"> <a class="videoLink" href="/watch?v=${result.id}">
<p style="font-size: 1.25rem;">${result.title.text || "No Title Found"}</p> <p style="font-size: 1.25rem;">${result.title.text || "No Title Found"}</p>
<p class="resultDescription">${(result.description_snippet || result.snippets[0].text.runs[0]).text}</p> <p class="resultDescription">${(result.description_snippet).text}</p>
<p style="display: block;">${result.published.text}</p> <p style="display: block;">${result.published.text}</p>
</a> </a>
</div> </div>
@ -59,9 +68,38 @@ function searchResultToHTML(results) {
</div> </div>
</div> </div>
` `
} else if (result.type == "Channel" && result.author.thumbnails[0]) {
channelsHTML += `
<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="/channel?v=${result.id}">
<img class="pfp" src="${getThumbor()}/${result.author.thumbnails[0].url}">
<p style="display: block; text-align: left;">${result.video_count.text}</p>
</a>
</div>
<div class="col-lg-6">
<a class="videoLink" href="/channel?v=${result.id}">
<p style="font-size: 1.25rem;">${result.author.name}<br>
<span class="note">${result.subscriber_count.text}</span></p>
<p class="resultDescription">${(result.description_snippet).text}</p>
</a>
</div>
</div>
</div>
`
} else {
console.log(result.type)
} }
} }
if (channelsHTML.length > 30) {
addedHTML += channelsHTML
}
if (videosHTML.length > 30) {
addedHTML += videosHTML
}
return addedHTML return addedHTML
} }
@ -83,6 +121,7 @@ app.get("/", async (req, res) => {
}) })
app.get("/search", async (req, res) => { app.get("/search", async (req, res) => {
var search = req.query.q
var innerTube = await ytjs.Innertube.create() var innerTube = await ytjs.Innertube.create()
res.setHeader("Content-Type", "text/html") res.setHeader("Content-Type", "text/html")
@ -90,19 +129,21 @@ app.get("/search", async (req, res) => {
var html = fs.readFileSync(path.join(resources, 'searchPage.html')).toString() var html = fs.readFileSync(path.join(resources, 'searchPage.html')).toString()
html = html.replaceAll("{SEARCH}", search)
res.write(html.substring(0, html.indexOf("{RESULTS}"))) res.write(html.substring(0, html.indexOf("{RESULTS}")))
var results = (await innerTube.search('test')) var results = (await innerTube.search(search, {type: "all"}))
var addedHTML = searchResultToHTML(results.results) var addedHTML = searchResultToHTML(results.results)
res.write(addedHTML + html.substring(html.indexOf("{RESULTS}") + 9), () => {res.end()}) res.write(addedHTML + html.substring(html.indexOf("{RESULTS}") + 9), () => {res.end()})
}) })
process.on('uncaughtException', (err, origin) => { // process.on('uncaughtException', (err, origin) => {
fs.writeSync( // fs.writeSync(
process.stderr.fd, // process.stderr.fd,
`Caught exception: ${err}\n` + // `Caught exception: ${err}\n` +
`Exception origin: ${origin}`, // `Exception origin: ${origin}`,
); // );
}); // });

View file

@ -196,6 +196,11 @@ hr {
margin: 20px auto; margin: 20px auto;
} }
.note {
color: darkgray;
font-size: 0.9rem
}
#channelBar { #channelBar {
background-color: rgb(15, 10, 25); background-color: rgb(15, 10, 25);
border-bottom: 2px gray solid; border-bottom: 2px gray solid;