Akane710 commited on
Commit
d8caa8b
·
verified ·
1 Parent(s): 1207724

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +36 -27
server.js CHANGED
@@ -2,53 +2,63 @@
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 = 7860;
10
- const HOST = '0.0.0.0';
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 { url, query } = req.query; // Extract parameters from query
18
 
19
- if (!url && !query) {
20
- return res.status(400).json({ error: "Either 'url' or 'query' must be provided as a query parameter." });
 
 
21
  }
22
 
23
  try {
24
- let videoInfo;
25
- if (url) {
26
- // Fetch video info using the provided URL
27
- videoInfo = await ytdl.getInfo(url, {
28
- playerClients: ["IOS", "WEB_CREATOR", "ANDROID", "WEB"],
29
- });
30
  } else {
31
- // Search YouTube and get the first result
32
- const searchResults = await yts(query);
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
- videoInfo = await ytdl.getInfo(firstResult.url, {
38
- playerClients: ["IOS", "WEB_CREATOR", "ANDROID", "WEB"],
39
- });
40
  }
41
 
42
- // Get the best format with video and audio
43
- const format = ytdl.filterFormats(videoInfo.formats, "videoandaudio")[0];
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: videoInfo.videoDetails.title,
51
- videoUrl: format.url,
 
 
 
 
 
 
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
+ });