Update server.js
Browse files
server.js
CHANGED
|
@@ -1,30 +1,61 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
-
import express from
|
| 4 |
-
import
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
// Constants
|
| 7 |
-
const PORT = 7860;
|
| 8 |
-
const HOST = '0.0.0.0';
|
| 9 |
-
|
| 10 |
-
// App
|
| 11 |
const app = express();
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
app.get('/scanByUrl', async (req, res) => {
|
| 18 |
-
const { url } = req.query;
|
| 19 |
try {
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
} catch (error) {
|
| 24 |
-
|
|
|
|
| 25 |
}
|
| 26 |
});
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
});
|
|
|
|
| 1 |
+
// File: app.js
|
| 2 |
|
| 3 |
+
import express from "express";
|
| 4 |
+
import bodyParser from "body-parser";
|
| 5 |
+
import ytdl from "@distube/ytdl-core";
|
| 6 |
+
import yts from "youtube-yts";
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
const app = express();
|
| 9 |
+
const PORT = 3000;
|
| 10 |
+
|
| 11 |
+
// Middleware
|
| 12 |
+
app.use(bodyParser.json());
|
| 13 |
+
|
| 14 |
+
// POST endpoint for downloading YouTube videos
|
| 15 |
+
app.get("/download", async (req, res) => {
|
| 16 |
+
const { url, query } = req.body;
|
| 17 |
+
|
| 18 |
+
if (!url && !query) {
|
| 19 |
+
return res.status(400).json({ error: "Either 'url' or 'query' must be provided." });
|
| 20 |
+
}
|
| 21 |
|
|
|
|
|
|
|
| 22 |
try {
|
| 23 |
+
let videoInfo;
|
| 24 |
+
if (url) {
|
| 25 |
+
// Fetch video info using the provided URL
|
| 26 |
+
videoInfo = await ytdl.getInfo(url, {
|
| 27 |
+
playerClients: ["IOS", "WEB_CREATOR", "ANDROID", "WEB"],
|
| 28 |
+
});
|
| 29 |
+
} else {
|
| 30 |
+
// Search YouTube and get the first result
|
| 31 |
+
const searchResults = await yts(query);
|
| 32 |
+
if (!searchResults.videos || searchResults.videos.length === 0) {
|
| 33 |
+
return res.status(404).json({ error: "No videos found for the query." });
|
| 34 |
+
}
|
| 35 |
+
const firstResult = searchResults.videos[0];
|
| 36 |
+
videoInfo = await ytdl.getInfo(firstResult.url, {
|
| 37 |
+
playerClients: ["IOS", "WEB_CREATOR", "ANDROID", "WEB"],
|
| 38 |
+
});
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
// Get the best format with video and audio
|
| 42 |
+
const format = ytdl.filterFormats(videoInfo.formats, "videoandaudio")[0];
|
| 43 |
+
if (!format) {
|
| 44 |
+
return res.status(500).json({ error: "No suitable format found." });
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// Respond with video URL and details
|
| 48 |
+
res.json({
|
| 49 |
+
title: videoInfo.videoDetails.title,
|
| 50 |
+
videoUrl: format.url,
|
| 51 |
+
});
|
| 52 |
} catch (error) {
|
| 53 |
+
console.error(error);
|
| 54 |
+
res.status(500).json({ error: "Failed to process the request." });
|
| 55 |
}
|
| 56 |
});
|
| 57 |
|
| 58 |
+
// Start the server
|
| 59 |
+
app.listen(PORT, () => {
|
| 60 |
+
console.log(`Server is running on http://localhost:${PORT}`);
|
| 61 |
});
|