From d54639549c5d1ddbb9365e49d6ff83680c481dd2 Mon Sep 17 00:00:00 2001 From: Violet Date: Mon, 23 Oct 2023 23:31:23 -0500 Subject: [PATCH] Redirect options! --- index.js | 67 ++++++++++++++++++++++++++++++++--------------- static/index.html | 12 +++++++-- 2 files changed, 56 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index de48cee..985cf45 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ const ytdl = require('ytdl-core'), const PORT = process.env.PORT || 8080 const app = express() if (fs.existsSync(path.join(__dirname, 'cached'))) { - fs.rmSync(path.join(__dirname, 'cached'), { recursive: true, force: true}) + fs.rmSync(path.join(__dirname, 'cached'), { recursive: true, force: true }) } function formatBytes(bytes, decimals = 2) { @@ -42,6 +42,7 @@ app.get("/getLink", async (req, res) => { const url = req.query.url const format = req.query.format const quality = req.query.quality + const redir = req.query.redirect if (!ytdl.validateURL(url)) { res.header("Content-Type", "text/html") @@ -50,20 +51,26 @@ app.get("/getLink", async (req, res) => { return } - res.setHeader("Content-Type", "text/html"); + if (redir != "redirect") { + res.setHeader("Content-Type", "text/html"); + + res.write(``) + } + - res.write(``) // String(Math.floor(Math.random() * 100000)) + var ytpath = path.join(__dirname, 'cached/' + ytdl.getVideoID(url) + '.mp4') var vidinfo = await ytdl.getBasicInfo(url) - res.write(`

Starting download...

`) - + if (redir != "redirect") { + res.write(`

Starting download...

`) + } var lastp = 0 var ytvid = ytdl(url, { 'quality': quality, 'format': 'mp4' }) - .on("progress", (data, ctotal, etotal) => { + if (redir != "redirect") { + ytvid.on("progress", (data, ctotal, etotal) => { var integer = 1 var percent = (Math.ceil(ctotal / etotal * 100)) if (percent % integer == 0 && lastp != percent) { @@ -78,28 +85,46 @@ app.get("/getLink", async (req, res) => { lastp = percent } }) - .pipe(fs.createWriteStream(ytpath)) + } + ytvid.pipe(fs.createWriteStream(ytpath)) .on("close", () => { - res.write("

Downloaded!

") + if (redir != "redirect") { + res.write("

Downloaded!

") + } var proc = new ffmpeg(ytpath) // res.write(`

Converting... (May take a while, doesn't support progress updates yet)

`) var endpath = ('cached/' + ytdl.getVideoID(url) + String(Math.floor(Math.random() * 100000)) + "Converted." + format) - res.write(`

Starting Video Conversion... Conversion may be slow depending on instance.

`) + if (redir != "redirect") { + res.write(`

Starting Video Conversion... Conversion may be slow depending on instance.

`) + } else { + console.log(redir) + } proc.then(function (video) { video .setVideoDuration(vidinfo.videoDetails.lengthSeconds) .setVideoFormat(format) - .save(endpath, (error, file)=>{ - res.write(`

Converted! (Why doesn't conversion support %s?)

`) - res.write(` + .save(endpath, (error, file) => { + if (redir != "redirect") { + res.write(`

Converted! (Why doesn't conversion support %s?)

`) + res.write(` `) - res.write(`

Link to video download! (Link is deleted after ${cacheDuration} minutes!)

`) - setTimeout(()=>{ + res.write(`

Link to video download! (Link is deleted after ${cacheDuration} minutes!)

`) + if (redir == "redirectjs") { + res.write(` + + `) + } + } else { + res.redirect(`/download?path=${path.basename(file)}&filename=${filename}.${format}`) + } + setTimeout(() => { if (fs.existsSync(ytpath)) { fs.unlinkSync(ytpath) } @@ -121,23 +146,23 @@ app.get("/getLink", async (req, res) => { percent = (Math.round(ffile.length / yt.length * (100 / integer))) * integer // console.log(ffile.length) // if (ffile.length != lastc) { - res.write(`

Converting... (${formatBytes(ffile.length)})

`) - res.write(` + res.write(`

Converting... (${formatBytes(ffile.length)})

`) + res.write(` `) - lastc = percent - count += 1 + lastc = percent + count += 1 // } } - if (percent < 100) { + if (percent < 100 && redir != "redirect") { setTimeout(() => { update() }, 2000); - } + } } update() }) @@ -154,7 +179,7 @@ app.get('/download', (req, res) => { } else { res.header("Content-Type", "text/html") res.write(``) - res.write(`

Uh oh! It seems that video couldn't be found... Maybe the URL expired? Or, the link was invalid.
If you believe this was a mistake, please put a issue on Github

`, ()=>{ + res.write(`

Uh oh! It seems that video couldn't be found... Maybe the URL expired? Or, the link was invalid.
If you believe this was a mistake, please put a issue on Github

`, () => { res.end() }) } diff --git a/static/index.html b/static/index.html index 1fb3988..03ac555 100644 --- a/static/index.html +++ b/static/index.html @@ -40,11 +40,19 @@ +

Redirect:

+ +

-

Videos over a certain size may fail to download!

-

Currently video quality may not be too high, due to formats above 720p having seperated audio tracks. This is being worked on!

+

*1: The "Redirect JS" Option is the same as the "Don't redirect" option, except it sends javascript to the browser letting you redirect. BOTH OPTIONS WORK FINE WITHOUT JAVASCRIPT!

+

*2: The "Redirect (disable progress)" option doesn't use any javascript and automatically redirects to the download, but you are not given download progress updates!

+

Please consider donating or host the website yourself! Anything helps! (Check github for more info)