Create index.js
Browse files
index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const { exec } = require('child_process');
|
| 3 |
+
|
| 4 |
+
const app = express();
|
| 5 |
+
const PORT = 7860;
|
| 6 |
+
|
| 7 |
+
app.get('/', (req, res) => {
|
| 8 |
+
res.send('🎬 Welcome to yt-dlp API (Node.js + Docker + Hugging Face)');
|
| 9 |
+
});
|
| 10 |
+
|
| 11 |
+
app.get('/download', async (req, res) => {
|
| 12 |
+
const url = req.query.url;
|
| 13 |
+
if (!url) return res.status(400).json({ error: 'Missing url parameter' });
|
| 14 |
+
|
| 15 |
+
const command = `yt-dlp -J "${url}"`;
|
| 16 |
+
|
| 17 |
+
exec(command, { maxBuffer: 1024 * 1000 }, (err, stdout, stderr) => {
|
| 18 |
+
if (err) return res.status(500).json({ error: stderr || err.message });
|
| 19 |
+
try {
|
| 20 |
+
const data = JSON.parse(stdout);
|
| 21 |
+
res.json({
|
| 22 |
+
title: data.title,
|
| 23 |
+
uploader: data.uploader,
|
| 24 |
+
duration: data.duration,
|
| 25 |
+
formats: data.formats.map(f => ({
|
| 26 |
+
format_id: f.format_id,
|
| 27 |
+
ext: f.ext,
|
| 28 |
+
resolution: f.resolution || f.height + 'p',
|
| 29 |
+
url: f.url
|
| 30 |
+
}))
|
| 31 |
+
});
|
| 32 |
+
} catch (e) {
|
| 33 |
+
res.status(500).json({ error: 'Failed to parse yt-dlp output' });
|
| 34 |
+
}
|
| 35 |
+
});
|
| 36 |
+
});
|
| 37 |
+
|
| 38 |
+
app.listen(PORT, () => {
|
| 39 |
+
console.log(`API server running on http://localhost:${PORT}`);
|
| 40 |
+
});
|