From 540d02af2f61cef3e1956416d4b8127c8d8ba6d3 Mon Sep 17 00:00:00 2001 From: Bingus_Violet Date: Mon, 27 Nov 2023 18:32:33 +0000 Subject: [PATCH] HD & search fixes! Improved Chromium experience. --- index.js | 137 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 73 insertions(+), 64 deletions(-) diff --git a/index.js b/index.js index c55182a..a511f1d 100644 --- a/index.js +++ b/index.js @@ -61,6 +61,53 @@ app.listen(PORT, () => { console.log("Simpletube is now listening on port: " + PORT) }) +function resultHTML(result) { + + console.log(result) + + function thumbnailCheck() { + if (result.thumbnails) { + return result.thumbnails[0].url + } else { + return result.thumbnail + } + } + + function publishCheck() { + if (result.published) { + return result.published.text + } else { + return result.uploaded + } + } + + return ` +
+ +
+ ` +} + function msth(ms) { var x = ms / 1000 var seconds = Math.floor(x % 60) @@ -117,13 +164,16 @@ async function cacher(id, quality, ready) { ready(vidpath, fs.readFileSync(vidpath)) } var percent = Math.round(ct / et * 100) + if (!debounce) { + videoCache[id + quality]["download%"] = percent + } if (!debounce && percent > dp && id in videoCache && "path" in videoCache[id]) { dp = percent } }) .on("finish", () => { - if (id in videoCache) { - videoCache[id]["downloaded"] = true + if ((id + quality) in videoCache) { + videoCache[id + quality]["downloaded"] = true } }) video.pipe(fs.createWriteStream(vidpath)) @@ -176,6 +226,8 @@ async function cacher(id, quality, ready) { debounce = true start(et) } + + videoCache[id + quality]["download%"] = Math.round(ct / et * 100) }) video.pipe(ffmpegProcess.stdio[3]) @@ -243,31 +295,7 @@ app.get("/search", async (req, res) => { for (let index = 0; index < videos.length; index++) { const result = videos[index]; - addedHTML += ` -
- -
- ` + addedHTML += resultHTML(result) } } if (addedHTML == "") { @@ -314,24 +342,7 @@ app.get("/channel", async (req, res) => { for (let index = 0; index < videos.length; index++) { const result = videos[index]; if (result.title != undefined) { - addedHTML += ` -
- -
- ` + addedHTML += resultHTML(result) } else { addedHTML = "

Failed to load.

" } @@ -389,7 +400,9 @@ app.get("/video", async (req, res) => { if (range) { function ready(vidpath) { if (fs.existsSync(vidpath)) { - const fileSize = videoCache[id].size + // const fileSize = videoCache[id].size + const fileSize = fs.statSync(vidpath).size + console.log(fileSize, vidpath) const parts = range.replace(/bytes=/, "").split("-") const start = parseInt(parts[0], 10) const end = parts[1] @@ -411,9 +424,7 @@ app.get("/video", async (req, res) => { res.writeHead(206, head) - if (fs.existsSync(vidpath)) { - fs.createReadStream(vidpath, { start: start }).pipe(res) - } + fs.createReadStream(vidpath, { start: start, end: end }).pipe(res) } } @@ -424,7 +435,7 @@ app.get("/video", async (req, res) => { videoCache[id].lastUsed = Date.now() } } else { - videoCache[id] = [] + videoCache[id + quality] = [] cacher(id, quality, ready) } @@ -456,7 +467,7 @@ app.get("/video", async (req, res) => { app.get("/watch", async (req, res) => { var id = req.query.q || req.query.v || "ubFq-wV3Eic" - var quality = req.query.quality || "sd" + var quality = req.query.quality || "hd" res.setHeader("Content-Type", "text/html") @@ -482,8 +493,6 @@ app.get("/watch", async (req, res) => { } - - var html = fs.readFileSync(playerPath).toString() if (quality == "sd") { @@ -508,14 +517,14 @@ app.get("/watch", async (req, res) => { } html = html.replace("{VIDEO_DESCRIPTION}", vidInfo.description || "No Description.") - - if (!(id in videoCache && videoCache[id]["downloaded"] == true)) { + console.log(videoCache) + if (!((id + quality) in videoCache && videoCache[id + quality].downloaded == true)) { html = html.replace("{CACHE_WARNING}", `

Please note that this video has not been fully cached, and may have trouble loading!
{DOWNLOAD_PERCENT}% cached as of page load. If content fails to load after a minute, reload the page!

`) - if (id in videoCache && "download%" in videoCache[id]) { - html = html.replace("{DOWNLOAD_PERCENT}", videoCache[id]["download%"]) + if ((id + quality) in videoCache && "download%" in videoCache[id + quality]) { + html = html.replace("{DOWNLOAD_PERCENT}", videoCache[id + quality]["download%"]) } else { html = html.replace("{DOWNLOAD_PERCENT}", "0") } @@ -529,10 +538,10 @@ app.get("/watch", async (req, res) => { res.send(html) }) -process.on('uncaughtException', (err, origin) => { - fs.writeSync( - process.stderr.fd, - `Caught exception: ${err}\n` + - `Exception origin: ${origin}`, - ); -}); \ No newline at end of file +// process.on('uncaughtException', (err, origin) => { +// fs.writeSync( +// process.stderr.fd, +// `Caught exception: ${err}\n` + +// `Exception origin: ${origin}`, +// ); +// }); \ No newline at end of file