this fucking suck ass

websocket
loicbersier 4 years ago
parent dc0db93490
commit 468112c1f6

@ -100,12 +100,6 @@ class DownloadController {
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 (data.alt) {
let altFolder;
@ -133,86 +127,83 @@ class DownloadController {
if (ws) {
ws.socket.emit('end', altFolder.slice(17));
}
return;
});
} else {
if (data.url.match( /^.*(youtu.be\/|list=)([^#\&\?]*).*/)) {
playlistDownload(data)
playlistDownload(data);
} else {
// Download as mp4 if possible
let video = youtubedl(data.url, ['--format=mp4', '-f', option]);
videoDownload(data);
}
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);
}
}
})
function videoDownload(data) {
// Download as mp4 if possible
let video = youtubedl(data.url, ['-f', data.quality]);
video.on('end', function() {
console.log('end');
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('message', 'end');
ws.socket.emit('progress', percent);
}
if (data.format == 'mp4' || data.format == 'webm') {
// If user requested mp4 directly attach the file
}
})
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', DLFile);
ws.socket.emit('end', `./public/uploads/${DLFile.replace(`.${ext}`, `.${data.format}`)}`);
}
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}`));
}
});
});
} else {
if (ws) {
ws.socket.emit('end', `./public/uploads/${DLFile}`);
}
});
}
}
});
}
function playlistDownload(data) {
@ -223,7 +214,6 @@ class DownloadController {
if (ws) {
ws.socket.emit('error', err.toString());
}
return;
});
let ext;
@ -235,15 +225,16 @@ class DownloadController {
ext = info.ext;
let title = info.title.slice(0,50);
DLFile = `${title.replace(/\s/g, '_')}.${ext}`;
DLFile = DLFile.replace(/[()]|[/]|[\\]|[?]|[!]/g, '_');
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/playlist/${title}.${ext}`;
DLFile = `hidden/playlist/${DLFile}`;
video.pipe(fs.createWriteStream(`./public/uploads/playlist/${DLFile}`));
video.pipe(fs.createWriteStream(`./public/uploads/playlist/${DLFile}`));
});

@ -65,12 +65,12 @@
<div class="field is-horizontal">
<div class="control">
<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') }}
</label>
<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') }}
</label>
@ -221,15 +221,52 @@
<script src="https://unpkg.com/@adonisjs/websocket-client"></script>
<script>
const ws = adonis.Ws();
ws.connect();
let channel = ws.subscribe('progress');
ws.on('open', () => {
console.log('is open');
});
ws.on('close', () => {
console.log('is closed');
});
channel.on('progress', (message) => {
document.getElementById('progress').innerHTML = message + '%';
document.getElementById('progress').value = message;
});
channel.on('error', (message) => {
console.log(message);
document.getElementById('msg').innerHTML = '<div class="notification is-danger fadein" id="error">' + message + '</div>';
setTimeout(() => {
fadeout('error')
}, 5000);
});
channel.on('end', (message) => {
let frm = document.getElementsByName('download-form')[0];
frm.reset();
function submitDownload() {
link = document.createElement("a");
link.setAttribute("href", message.substring(9));
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
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
@ -238,7 +275,7 @@
document.getElementById('progression').innerHTML = '<progress class="progress is-primary" max="100" id="progress">0%</progress>'
}
function fadeout(id) {
async function fadeout(id) {
document.getElementById(id).classList.add('fadeout');
setTimeout(() => {
let element = document.getElementById(id);
@ -246,7 +283,7 @@
}, 2000);
}
function toClipboard(text) {
async function toClipboard(text) {
navigator.clipboard.writeText(text)
.catch(err => {
console.error(err);
@ -262,7 +299,7 @@
}
// If alt download block other settings since they don't work anyway
document.getElementById('alt').onclick = function() {
document.getElementById('alt').onclick = async function() {
if(document.getElementById('alt').checked) {
document.getElementById('small').disabled = true;
document.getElementById('small').checked = false;
@ -282,45 +319,11 @@
// If user press enter do same thing as if pressing the button
let input = document.getElementById("URL");
input.addEventListener("keyup", function(event) {
input.addEventListener("keyup", async function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("button").click();
}
});
ws.connect();
let channel = ws.subscribe('progress')
ws.on('open', () => {
console.log('is open');
});
ws.on('close', () => {
console.log('is closed');
});
channel.on('progress', (message) => {
document.getElementById('progress').innerHTML = message + '%';
document.getElementById('progress').value = message;
});
channel.on('error', (message) => {
console.log(message);
document.getElementById('msg').innerHTML = '<div class="notification is-danger fadein" id="error">' + message + '</div>';
setTimeout(() => {
fadeout('error')
}, 5000);
});
channel.on('end', (message) => {
link = document.createElement("a");
link.setAttribute("href", message); //replace "file" with link to file you want to download
link.setAttribute("download", message);// replace "file" here too
link.click(); //virtually click <a> element to initiate download
document.getElementById('progression').innerHTML = '';
});
</script>
</html>

Loading…
Cancel
Save