diff --git a/index.js b/index.js index 390a466..0c825ba 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,7 @@ const cssPath = path.join(staticPath, 'mainStyle.css') const resources = path.join(__dirname, 'resources') const cachePath = path.join(__dirname, 'cache') +// const cachePath = "/tmp/cache/SimpleTube" const playerPath = path.join(resources, 'player.html') const searchPath = path.join(resources, 'searchPage.html') @@ -28,6 +29,7 @@ if (fs.existsSync(cachePath)) { fs.mkdirSync(cachePath) + var videoCache = {} var app = express() @@ -40,30 +42,97 @@ app.listen(PORT, () => { console.log("Simpletube is now listening on port: " + PORT) }) +function cacher(id, ready) { + console.log(id) + vidpath = path.join(__dirname, `cache/${id}.mp4`) + + var debounce = true + + var dp = 0 + var video = ytdl(id, { filter: 'videoandaudio', quality: "highest", format: 'mp4' }) + .on("progress", (chunk, ct, et) => { + + if (debounce && (ct / et) > 0.015) { + debounce = false + videoCache[id] = { + "path": vidpath, + "size": et, + "downloaded": false, + "download%": 0, + "lastUsed": new Date().getTime() + } + + ready(vidpath, fs.readFileSync(vidpath)) + } + var percent = Math.round(ct / et * 100) + if (!debounce && percent > dp && id in videoCache && "path" in videoCache[id]) { + dp = percent + videoCache[id]["download%"] = dp + } + }) + .on("finish", () => { + if (id in videoCache) { + videoCache[id]["downloaded"] = true + } + }) + return video +} + app.get("/search", async (req, res) => { var search = req.query.q || "How to search on SimpleTube" res.setHeader("Content-Type", "text/html") - youtube.search(search).then((results) => { + youtube.search(search, { type: "all" }).then((results) => { var videos = results.videos + var html = fs.readFileSync(searchPath).toString() - + html = html.replace("{SEARCH}", search) var addedHTML = "" + var channels = results.channels + + if (channels.length > 0) { + + addedHTML += "
${result.title || "No Title Found"}
${result.description.substring(0, 75) + "..." || "No Description"}
@@ -71,7 +140,7 @@ app.get("/search", async (req, res) => { @@ -79,13 +148,12 @@ app.get("/search", async (req, res) => {