-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
82 lines (62 loc) · 2.48 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
username : minnukota381
Description : This code is an Express.js server application designed to allow users to download YouTube playlists.
*/
const express = require('express');
const path = require('path');
const { spawn } = require('child_process');
const fs = require('fs');
const app = express();
const port = 3000;
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.render('indexx');
});
app.get('/download-playlist', async (req, res) => {
const playlistURL = req.query.url;
if (!playlistURL) {
return res.status(400).send('URL is required');
}
try {
const playlistId = new URLSearchParams(new URL(playlistURL).search).get('list');
const downloadDir = path.join(__dirname, 'downloads', playlistId);
if (!fs.existsSync(downloadDir)) {
fs.mkdirSync(downloadDir, { recursive: true });
}
const ytDlpPath = 'C:\\Program Files\\yt-dlp\\yt-dlp.exe';
const ytDlpProcess = spawn(ytDlpPath, ['-o', `${downloadDir}/%(title)s.%(ext)s`, '-f', 'best', playlistURL]);
ytDlpProcess.stdout.on('data', (data) => {
console.log(`yt-dlp stdout: ${data}`);
});
ytDlpProcess.stderr.on('data', (data) => {
console.error(`yt-dlp stderr: ${data}`);
});
ytDlpProcess.on('close', async (code) => {
if (code !== 0) {
console.error(`yt-dlp process exited with code ${code}`);
return res.status(500).send('Failed to download playlist');
}
res.header('Content-Disposition', `attachment; filename="${playlistId}.zip"`);
const archive = archiver('zip');
archive.on('error', (err) => {
console.error('Error:', err);
res.status(500).send('Failed to create archive');
});
archive.pipe(res);
const files = fs.readdirSync(downloadDir);
for (const file of files) {
archive.file(path.join(downloadDir, file), { name: file });
}
archive.finalize();
await fs.promises.rm(downloadDir, { recursive: true, force: true });
});
} catch (error) {
console.error('Error:', error);
res.status(500).send('Failed to download playlist');
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});