Akane710 commited on
Commit
f3ff21d
·
verified ·
1 Parent(s): 7a660b9

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +18 -2
server.js CHANGED
@@ -1,9 +1,9 @@
1
- // File: app.js
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;
@@ -22,6 +22,18 @@ const isUrl = (input) => {
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
@@ -48,6 +60,9 @@ app.get("/download", async (req, res) => {
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({
@@ -59,6 +74,7 @@ app.get("/download", async (req, res) => {
59
  views,
60
  uploadDate: upload,
61
  thumbnail,
 
62
  });
63
  } catch (error) {
64
  console.error(error);
 
1
+ // Import required modules
 
2
  import express from "express";
3
  import bodyParser from "body-parser";
4
  import { ytmp4 } from "ruhend-scraper";
5
  import yts from "youtube-yts";
6
+ import axios from "axios"; // Add axios for making HTTP requests
7
 
8
  const app = express();
9
  const PORT = 7860;
 
22
  }
23
  };
24
 
25
+ // Function to get the size of a video file from its URL
26
+ const getVideoSize = async (url) => {
27
+ try {
28
+ const response = await axios.head(url);
29
+ const contentLength = response.headers["content-length"];
30
+ return contentLength ? parseInt(contentLength, 10) : null;
31
+ } catch (error) {
32
+ console.error("Failed to fetch video size:", error.message);
33
+ return null;
34
+ }
35
+ };
36
+
37
  // GET endpoint for downloading YouTube videos
38
  app.get("/download", async (req, res) => {
39
  const { input } = req.query; // Extract single input parameter
 
60
  videoData = await ytmp4(firstResult.url);
61
  }
62
 
63
+ // Get video size
64
+ const videoSize = await getVideoSize(videoData.video);
65
+
66
  // Respond with video details
67
  const { title, video, author, description, duration, views, upload, thumbnail } = videoData;
68
  res.json({
 
74
  views,
75
  uploadDate: upload,
76
  thumbnail,
77
+ size: videoSize ? `${(videoSize / 1048576).toFixed(2)} MB` : "Unknown", // Convert bytes to MB
78
  });
79
  } catch (error) {
80
  console.error(error);