Update server.js
Browse files
server.js
CHANGED
|
@@ -2,53 +2,63 @@
|
|
| 2 |
|
| 3 |
import express from "express";
|
| 4 |
import bodyParser from "body-parser";
|
| 5 |
-
import
|
| 6 |
import yts from "youtube-yts";
|
| 7 |
|
| 8 |
const app = express();
|
| 9 |
const PORT = 7860;
|
| 10 |
-
const HOST =
|
| 11 |
|
| 12 |
// Middleware
|
| 13 |
app.use(bodyParser.json());
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
// GET endpoint for downloading YouTube videos
|
| 16 |
app.get("/download", async (req, res) => {
|
| 17 |
-
const {
|
| 18 |
|
| 19 |
-
if (!
|
| 20 |
-
return res
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
try {
|
| 24 |
-
let
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
});
|
| 30 |
} else {
|
| 31 |
-
//
|
| 32 |
-
const searchResults = await yts(
|
| 33 |
if (!searchResults.videos || searchResults.videos.length === 0) {
|
| 34 |
return res.status(404).json({ error: "No videos found for the query." });
|
| 35 |
}
|
| 36 |
const firstResult = searchResults.videos[0];
|
| 37 |
-
|
| 38 |
-
playerClients: ["IOS", "WEB_CREATOR", "ANDROID", "WEB"],
|
| 39 |
-
});
|
| 40 |
}
|
| 41 |
|
| 42 |
-
//
|
| 43 |
-
const
|
| 44 |
-
if (!format) {
|
| 45 |
-
return res.status(500).json({ error: "No suitable format found." });
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
// Respond with video URL and details
|
| 49 |
res.json({
|
| 50 |
-
title
|
| 51 |
-
videoUrl:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
});
|
| 53 |
} catch (error) {
|
| 54 |
console.error(error);
|
|
@@ -56,7 +66,6 @@ app.get("/download", async (req, res) => {
|
|
| 56 |
}
|
| 57 |
});
|
| 58 |
|
| 59 |
-
|
| 60 |
app.listen(PORT, HOST, () => {
|
| 61 |
console.log(`Running on http://${HOST}:${PORT}`);
|
| 62 |
-
});
|
|
|
|
| 2 |
|
| 3 |
import express from "express";
|
| 4 |
import bodyParser from "body-parser";
|
| 5 |
+
import { ytmp4 } from "ruhend-scraper";
|
| 6 |
import yts from "youtube-yts";
|
| 7 |
|
| 8 |
const app = express();
|
| 9 |
const PORT = 7860;
|
| 10 |
+
const HOST = "0.0.0.0";
|
| 11 |
|
| 12 |
// Middleware
|
| 13 |
app.use(bodyParser.json());
|
| 14 |
|
| 15 |
+
// Function to check if input is a URL
|
| 16 |
+
const isUrl = (input) => {
|
| 17 |
+
try {
|
| 18 |
+
new URL(input);
|
| 19 |
+
return true;
|
| 20 |
+
} catch {
|
| 21 |
+
return false;
|
| 22 |
+
}
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
// GET endpoint for downloading YouTube videos
|
| 26 |
app.get("/download", async (req, res) => {
|
| 27 |
+
const { input } = req.query; // Extract single input parameter
|
| 28 |
|
| 29 |
+
if (!input) {
|
| 30 |
+
return res
|
| 31 |
+
.status(400)
|
| 32 |
+
.json({ error: "'input' query parameter must be provided." });
|
| 33 |
}
|
| 34 |
|
| 35 |
try {
|
| 36 |
+
let videoData;
|
| 37 |
+
|
| 38 |
+
if (isUrl(input)) {
|
| 39 |
+
// Input is a URL
|
| 40 |
+
videoData = await ytmp4(input);
|
|
|
|
| 41 |
} else {
|
| 42 |
+
// Input is a query, perform YouTube search
|
| 43 |
+
const searchResults = await yts(input);
|
| 44 |
if (!searchResults.videos || searchResults.videos.length === 0) {
|
| 45 |
return res.status(404).json({ error: "No videos found for the query." });
|
| 46 |
}
|
| 47 |
const firstResult = searchResults.videos[0];
|
| 48 |
+
videoData = await ytmp4(firstResult.url);
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
|
| 51 |
+
// Respond with video details
|
| 52 |
+
const { title, video, author, description, duration, views, upload, thumbnail } = videoData;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
res.json({
|
| 54 |
+
title,
|
| 55 |
+
videoUrl: video,
|
| 56 |
+
author,
|
| 57 |
+
description,
|
| 58 |
+
duration,
|
| 59 |
+
views,
|
| 60 |
+
uploadDate: upload,
|
| 61 |
+
thumbnail,
|
| 62 |
});
|
| 63 |
} catch (error) {
|
| 64 |
console.error(error);
|
|
|
|
| 66 |
}
|
| 67 |
});
|
| 68 |
|
|
|
|
| 69 |
app.listen(PORT, HOST, () => {
|
| 70 |
console.log(`Running on http://${HOST}:${PORT}`);
|
| 71 |
+
});
|