mirror of
https://github.com/SrIzan10/youtubeDownloader.git
synced 2026-05-01 11:06:58 +00:00
add url invalid message
This commit is contained in:
@@ -13,6 +13,9 @@ app.listen(PORT, () => {
|
||||
app.get('/downloadmp3', async (req, res, next) => {
|
||||
try {
|
||||
var url = req.query.url;
|
||||
if(!ytdl.validateURL(url)) {
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
let title = 'audio';
|
||||
|
||||
await ytdl.getBasicInfo(url, {
|
||||
@@ -35,17 +38,20 @@ app.get('/downloadmp3', async (req, res, next) => {
|
||||
|
||||
app.get('/downloadmp4', async (req, res, next) => {
|
||||
try {
|
||||
let URL = req.query.url;
|
||||
let url = req.query.url;
|
||||
if(!ytdl.validateURL(url)) {
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
let title = 'video';
|
||||
|
||||
await ytdl.getBasicInfo(URL, {
|
||||
await ytdl.getBasicInfo(url, {
|
||||
format: 'mp4'
|
||||
}, (err, info) => {
|
||||
title = info.player_response.videoDetails.title.replace(/[^\x00-\x7F]/g, "");
|
||||
});
|
||||
|
||||
res.header('Content-Disposition', `attachment; filename="${title}.mp4"`);
|
||||
ytdl(URL, {
|
||||
ytdl(url, {
|
||||
format: 'mp4',
|
||||
}).pipe(res);
|
||||
|
||||
|
||||
28
script.js
28
script.js
@@ -8,17 +8,33 @@ Btn.addEventListener('click', () => {
|
||||
alert('Enter YouTube URL');
|
||||
} else {
|
||||
if (select.value == 'mp3') {
|
||||
redirectMp3(URLinput.value);
|
||||
downloadMp3(URLinput.value);
|
||||
} else if (select.value == 'mp4') {
|
||||
redirectMp4(URLinput.value);
|
||||
downloadMp4(URLinput.value);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function redirectMp3(query) {
|
||||
window.location.href = `${serverURL}/downloadmp3?url=${query}`;
|
||||
async function downloadMp3(query) {
|
||||
const res = await fetch(`${serverURL}/downloadmp3?url=${query}`);
|
||||
if(res.status == 200) {
|
||||
var a = document.createElement('a');
|
||||
a.href = `${serverURL}/downloadmp3?url=${query}`;
|
||||
a.setAttribute('download', '');
|
||||
a.click();
|
||||
} else if(res.status == 400) {
|
||||
alert("Invalid url");
|
||||
}
|
||||
}
|
||||
|
||||
function redirectMp4(query) {
|
||||
window.location.href = `${serverURL}/downloadmp4?url=${query}`;
|
||||
async function downloadMp4(query) {
|
||||
const res = await fetch(`${serverURL}/downloadmp4?url=${query}`);
|
||||
if(res.status == 200) {
|
||||
var a = document.createElement('a');
|
||||
a.href = `${serverURL}/downloadmp4?url=${query}`;
|
||||
a.setAttribute('download', '');
|
||||
a.click();
|
||||
} else if(res.status == 400) {
|
||||
alert('Invalid url');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user