this fucking suck ass

This commit is contained in:
loicbersier 2020-06-11 22:16:56 +02:00
parent dc0db93490
commit 468112c1f6
2 changed files with 153 additions and 159 deletions

View file

@ -100,12 +100,6 @@ class DownloadController {
return; return;
} }
// Youtube-dl quality settings
if (data.quality == 'small')
option = 'worst'
else
option = 'best'
// If alt download ( Quality settings and file format option doesn't work here ) // If alt download ( Quality settings and file format option doesn't work here )
if (data.alt) { if (data.alt) {
let altFolder; let altFolder;
@ -133,86 +127,83 @@ class DownloadController {
if (ws) { if (ws) {
ws.socket.emit('end', altFolder.slice(17)); ws.socket.emit('end', altFolder.slice(17));
} }
return;
}); });
} else { } else {
if (data.url.match( /^.*(youtu.be\/|list=)([^#\&\?]*).*/)) { if (data.url.match( /^.*(youtu.be\/|list=)([^#\&\?]*).*/)) {
playlistDownload(data) playlistDownload(data);
} else { } else {
// Download as mp4 if possible videoDownload(data);
let video = youtubedl(data.url, ['--format=mp4', '-f', option]);
video.on('error', function(err) {
console.error(err);
if (ws) {
ws.socket.emit('error', err.toString());
}
return;
})
let ext;
let size = 0
video.on('info', function(info) {
size = info.size
// Set file name
ext = info.ext;
let title = info.title.slice(0,50);
DLFile = `${title.replace(/\s/g, '_')}.${ext}`;
DLFile = DLFile.replace(/[()]|[/]|[\\]|[?]|[!]/g, '_');
// If no title use the ID
if (title == '_') title = `_${info.id}`;
// If user want to hide from the feed
if (data.feed == 'on')
DLFile = `hidden/${title}.${ext}`;
video.pipe(fs.createWriteStream(`./public/uploads/${DLFile}`));
});
let pos = 0
video.on('data', function data(chunk) {
pos += chunk.length
// `size` should not be 0 here.
if (size) {
let percent = (pos / size * 100).toFixed(2)
if (ws) {
ws.socket.emit('progress', percent);
}
}
})
video.on('end', function() {
console.log('end');
if (ws) {
ws.socket.emit('message', 'end');
}
if (data.format == 'mp4' || data.format == 'webm') {
// If user requested mp4 directly attach the file
if (ws) {
ws.socket.emit('end', DLFile);
}
return;
} else {
// If user requested an audio format, convert it
ffmpeg(`./public/uploads/${DLFile}`)
.noVideo()
.audioChannels('2')
.audioFrequency('44100')
.audioBitrate('320k')
.format(data.format)
.save(`./public/uploads/${DLFile.replace(`.${ext}`, `.${data.format}`)}`)
.on('progress', (progress) => {
wb.broadcast(progress.percent)
})
.on('end', () => {
fs.unlinkSync(`./public/uploads/${DLFile}`);
if (ws) {
ws.socket.emit('end', DLFile.replace(`.${ext}`, `.${data.format}`));
}
});
}
});
} }
}
function videoDownload(data) {
// Download as mp4 if possible
let video = youtubedl(data.url, ['-f', data.quality]);
video.on('error', function(err) {
console.error(err);
if (ws) {
ws.socket.emit('error', err.toString());
}
})
let ext;
let size = 0
video.on('info', function(info) {
size = info.size
// Set file name
ext = info.ext;
let title = info.title.slice(0,50);
DLFile = `${title.replace(/\s/g, '_')}.${ext}`;
DLFile = DLFile.replace(/[()]|[/]|[\\]|[?]|[!]|[#]/g, '_');
// If no title use the ID
if (title == '_') title = `_${info.id}`;
// If user want to hide from the feed
if (data.feed == 'on')
DLFile = `hidden/${DLFile}`;
video.pipe(fs.createWriteStream(`./public/uploads/${DLFile}`));
});
let pos = 0
video.on('data', function data(chunk) {
pos += chunk.length
// `size` should not be 0 here.
if (size) {
let percent = (pos / size * 100).toFixed(2)
if (ws) {
ws.socket.emit('progress', percent);
}
}
})
video.on('end', function() {
if (data.format == 'mp3' || data.format == 'flac') {
// If user requested an audio format, convert it
ffmpeg(`./public/uploads/${DLFile}`)
.noVideo()
.audioChannels('2')
.audioFrequency('44100')
.audioBitrate('320k')
.format(data.format)
.save(`./public/uploads/${DLFile.replace(`.${ext}`, `.${data.format}`)}`)
.on('progress', (progress) => {
ws.socket.emit(progress.percent)
})
.on('end', () => {
fs.unlinkSync(`./public/uploads/${DLFile}`);
if (ws) {
ws.socket.emit('end', `./public/uploads/${DLFile.replace(`.${ext}`, `.${data.format}`)}`);
}
});
} else {
if (ws) {
ws.socket.emit('end', `./public/uploads/${DLFile}`);
}
}
});
} }
function playlistDownload(data) { function playlistDownload(data) {
@ -223,7 +214,6 @@ class DownloadController {
if (ws) { if (ws) {
ws.socket.emit('error', err.toString()); ws.socket.emit('error', err.toString());
} }
return;
}); });
let ext; let ext;
@ -235,15 +225,16 @@ class DownloadController {
ext = info.ext; ext = info.ext;
let title = info.title.slice(0,50); let title = info.title.slice(0,50);
DLFile = `${title.replace(/\s/g, '_')}.${ext}`; DLFile = `${title.replace(/\s/g, '_')}.${ext}`;
DLFile = DLFile.replace(/[()]|[/]|[\\]|[?]|[!]/g, '_'); DLFile = DLFile.replace(/[()]|[/]|[\\]|[?]|[!]|[#]/g, '_');
// If no title use the ID // If no title use the ID
if (title == '_') title = `_${info.id}`; if (title == '_') title = `_${info.id}`;
// If user want to hide from the feed // If user want to hide from the feed
if (data.feed == 'on') if (data.feed == 'on')
DLFile = `hidden/playlist/${title}.${ext}`; DLFile = `hidden/playlist/${DLFile}`;
video.pipe(fs.createWriteStream(`./public/uploads/playlist/${DLFile}`));
video.pipe(fs.createWriteStream(`./public/uploads/playlist/${DLFile}`));
}); });

View file

@ -65,12 +65,12 @@
<div class="field is-horizontal"> <div class="field is-horizontal">
<div class="control"> <div class="control">
<label class="radio" for="small"> <label class="radio" for="small">
<input class="radio" type="radio" name="quality" id="small" value="small"> <input class="radio" type="radio" name="quality" id="small" value="worst">
{{ antl.formatMessage('messages.LQ') }} {{ antl.formatMessage('messages.LQ') }}
</label> </label>
<label class="radio" for="high"> <label class="radio" for="high">
<input class="radio" type="radio" name="quality" id="high" value="high" checked> <input class="radio" type="radio" name="quality" id="high" value="best" checked>
{{ antl.formatMessage('messages.HQ') }} {{ antl.formatMessage('messages.HQ') }}
</label> </label>
@ -221,77 +221,8 @@
<script src="https://unpkg.com/@adonisjs/websocket-client"></script> <script src="https://unpkg.com/@adonisjs/websocket-client"></script>
<script> <script>
const ws = adonis.Ws(); const ws = adonis.Ws();
function submitDownload() {
document.getElementById('msg').innerHTML = '<div class="notification is-success fadein" id="notif"></button>{{ antl.formatMessage('messages.dlStart') }}</div>';
setTimeout(() => {
fadeout('notif')
}, 2000);
FD = new FormData(document.getElementsByName('download-form')[0]);
fetch('/', {
method: 'POST',
body: FD
});
document.getElementById('progression').innerHTML = '<progress class="progress is-primary" max="100" id="progress">0%</progress>'
}
function fadeout(id) {
document.getElementById(id).classList.add('fadeout');
setTimeout(() => {
let element = document.getElementById(id);
element.parentNode.removeChild(element);
}, 2000);
}
function toClipboard(text) {
navigator.clipboard.writeText(text)
.catch(err => {
console.error(err);
document.getElementById('msg').innerHTML = '<div class="notification is-error fadein" id="notif">{{ antl.formatMessage('messages.errorCopy') }}</div>';
setTimeout(() => {
fadeout('notif')
}, 5000);
});
document.getElementById('msg').innerHTML = '<div class="notification is-success fadein" id="notif">{{ antl.formatMessage('messages.successCopy') }}</div>';
setTimeout(() => {
fadeout('notif')
}, 5000);
}
// If alt download block other settings since they don't work anyway
document.getElementById('alt').onclick = function() {
if(document.getElementById('alt').checked) {
document.getElementById('small').disabled = true;
document.getElementById('small').checked = false;
document.getElementById('mp3').disabled = true;
document.getElementById('mp3').checked = false;
document.getElementById('flac').disabled = true;
document.getElementById('flac').checked = false;
document.getElementById('mp4').checked = true;
document.getElementById('high').checked = true;
} else {
document.getElementById('small').disabled = false;
document.getElementById('mp3').disabled = false;
document.getElementById('flac').disabled = false;
}
}
// If user press enter do same thing as if pressing the button
let input = document.getElementById("URL");
input.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("button").click();
}
});
ws.connect(); ws.connect();
let channel = ws.subscribe('progress') let channel = ws.subscribe('progress');
ws.on('open', () => { ws.on('open', () => {
console.log('is open'); console.log('is open');
@ -315,12 +246,84 @@
}); });
channel.on('end', (message) => { channel.on('end', (message) => {
let frm = document.getElementsByName('download-form')[0];
frm.reset();
link = document.createElement("a"); link = document.createElement("a");
link.setAttribute("href", message); //replace "file" with link to file you want to download link.setAttribute("href", message.substring(9));
link.setAttribute("download", message);// replace "file" here too if (message.includes('/hidden/')) {
link.setAttribute("download", message.substring(24));
} else {
link.setAttribute("download", message.substring(17));
}
link.click(); //virtually click <a> element to initiate download link.click(); //virtually click <a> element to initiate download
document.getElementById('progression').innerHTML = ''; document.getElementById('progression').innerHTML = '';
}); });
async function submitDownload() {
document.getElementById('msg').innerHTML = '<div class="notification is-success fadein" id="notif"></button>{{ antl.formatMessage('messages.dlStart') }}</div>';
setTimeout(() => {
fadeout('notif')
}, 2000);
FD = new FormData(document.getElementsByName('download-form')[0]);
fetch('/', {
method: 'POST',
body: FD
});
document.getElementById('progression').innerHTML = '<progress class="progress is-primary" max="100" id="progress">0%</progress>'
}
async function fadeout(id) {
document.getElementById(id).classList.add('fadeout');
setTimeout(() => {
let element = document.getElementById(id);
element.parentNode.removeChild(element);
}, 2000);
}
async function toClipboard(text) {
navigator.clipboard.writeText(text)
.catch(err => {
console.error(err);
document.getElementById('msg').innerHTML = '<div class="notification is-error fadein" id="notif">{{ antl.formatMessage('messages.errorCopy') }}</div>';
setTimeout(() => {
fadeout('notif')
}, 5000);
});
document.getElementById('msg').innerHTML = '<div class="notification is-success fadein" id="notif">{{ antl.formatMessage('messages.successCopy') }}</div>';
setTimeout(() => {
fadeout('notif')
}, 5000);
}
// If alt download block other settings since they don't work anyway
document.getElementById('alt').onclick = async function() {
if(document.getElementById('alt').checked) {
document.getElementById('small').disabled = true;
document.getElementById('small').checked = false;
document.getElementById('mp3').disabled = true;
document.getElementById('mp3').checked = false;
document.getElementById('flac').disabled = true;
document.getElementById('flac').checked = false;
document.getElementById('mp4').checked = true;
document.getElementById('high').checked = true;
} else {
document.getElementById('small').disabled = false;
document.getElementById('mp3').disabled = false;
document.getElementById('flac').disabled = false;
}
}
// If user press enter do same thing as if pressing the button
let input = document.getElementById("URL");
input.addEventListener("keyup", async function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("button").click();
}
});
</script> </script>
</html> </html>