mas fixes
This commit is contained in:
parent
54ca4ea3d9
commit
7871a67084
1 changed files with 36 additions and 22 deletions
40
index.js
40
index.js
|
@ -44,40 +44,56 @@ app.get('/search', async (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function getRandomHex() {
|
||||||
|
// Generate a random integer between 100 and 99999
|
||||||
|
const randomNumber = Math.floor(Math.random() * (99999 - 100 + 1)) + 100;
|
||||||
|
|
||||||
|
// Convert the number to a hexadecimal string and return it
|
||||||
|
return randomNumber.toString(16);
|
||||||
|
}
|
||||||
|
|
||||||
// Video download endpoint
|
// Video download endpoint
|
||||||
app.get('/download', (req, res) => {
|
app.get('/download', (req, res) => {
|
||||||
const videoUrl = req.query.url;
|
const videoUrl = req.query.url;
|
||||||
|
|
||||||
if (!videoUrl) {
|
if (!videoUrl) {
|
||||||
|
console.error('Error: Query parameter "url" is required.');
|
||||||
return res.status(400).json({ error: 'Query parameter "url" is required.' });
|
return res.status(400).json({ error: 'Query parameter "url" is required.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the output file path
|
// Set the output file path
|
||||||
var outputPath = path.join('/tmp/downloads', '%(title)s.%(ext)s');
|
var outputPath = path.join('/tmp/downloads', `${getRandomHex}.mp4`);
|
||||||
|
|
||||||
// Create /tmp/downloads directory if it doesn't exist
|
// Create /tmp/downloads directory if it doesn't exist
|
||||||
|
try {
|
||||||
fs.mkdirSync(path.join('/tmp/downloads'), { recursive: true });
|
fs.mkdirSync(path.join('/tmp/downloads'), { recursive: true });
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error creating directory:', err);
|
||||||
|
return res.status(500).json({ error: 'Failed to create download directory.', details: err.message });
|
||||||
|
}
|
||||||
|
|
||||||
// Use yt-dlp to download the video
|
// Use yt-dlp to download the video
|
||||||
exec(`yt-dlp -o "${outputPath}" "${videoUrl}"`, (error, stdout, stderr) => {
|
exec(`yt-dlp -o "${outputPath}" "${videoUrl}"`, (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
console.error('Error executing yt-dlp:', stderr);
|
||||||
return res.status(500).json({ error: 'An error occurred while downloading the video.', details: stderr });
|
return res.status(500).json({ error: 'An error occurred while downloading the video.', details: stderr });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the output to get the file name
|
console.log('yt-dlp output:', stdout);
|
||||||
const filename = stdout.split('\n').find(line => line.includes('Destination'))?.split(' ')[1];
|
|
||||||
|
|
||||||
if (!filename) {
|
const downloadedFilePath = outputPath;
|
||||||
return res.status(500).json({ error: 'Downloaded file not found.' });
|
|
||||||
|
console.log('Downloaded file path:', downloadedFilePath);
|
||||||
|
|
||||||
|
if (!downloadedFilePath || !fs.existsSync(downloadedFilePath)) {
|
||||||
|
console.error('Error: Downloaded file not found or invalid path.');
|
||||||
|
return res.status(500).json({ error: 'Downloaded file not found or invalid path.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Full path of the downloaded file
|
// Send the file
|
||||||
const downloadedFilePath = outputPath
|
|
||||||
|
|
||||||
// Check if the file exists and send it
|
|
||||||
if (fs.existsSync(downloadedFilePath)) {
|
|
||||||
res.download(downloadedFilePath, (err) => {
|
res.download(downloadedFilePath, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
console.error('Error sending the file:', err);
|
||||||
return res.status(500).json({ error: 'Failed to send the file.', details: err.message });
|
return res.status(500).json({ error: 'Failed to send the file.', details: err.message });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,12 +104,10 @@ app.get('/download', (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
res.status(404).json({ error: 'Downloaded file not found.' });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Server running at http://localhost:${port}`);
|
console.log(`Server running at http://localhost:${port}`);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue