File size: 2,336 Bytes
f3ff21d
8aa50f6
859484b
d8caa8b
8aa50f6
f3ff21d
efead7f
 
e57deaf
d8caa8b
8aa50f6
 
 
 
d8caa8b
 
 
 
 
 
 
 
 
 
f3ff21d
 
 
 
 
 
 
 
 
 
 
 
859484b
8aa50f6
d8caa8b
8aa50f6
d8caa8b
 
 
 
8aa50f6
804d780
 
d8caa8b
 
 
 
 
8aa50f6
d8caa8b
 
8aa50f6
 
 
 
d8caa8b
8aa50f6
 
f3ff21d
 
 
d8caa8b
 
8aa50f6
d8caa8b
 
 
 
 
 
 
 
f3ff21d
8aa50f6
804d780
8aa50f6
 
804d780
 
 
e57deaf
 
d8caa8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Import required modules
import express from "express";
import bodyParser from "body-parser";
import { ytmp4 } from "ruhend-scraper";
import yts from "youtube-yts";
import axios from "axios"; // Add axios for making HTTP requests

const app = express();
const PORT = 7860;
const HOST = "0.0.0.0";

// Middleware
app.use(bodyParser.json());

// Function to check if input is a URL
const isUrl = (input) => {
  try {
    new URL(input);
    return true;
  } catch {
    return false;
  }
};

// Function to get the size of a video file from its URL
const getVideoSize = async (url) => {
  try {
    const response = await axios.head(url);
    const contentLength = response.headers["content-length"];
    return contentLength ? parseInt(contentLength, 10) : null;
  } catch (error) {
    console.error("Failed to fetch video size:", error.message);
    return null;
  }
};

// GET endpoint for downloading YouTube videos
app.get("/download", async (req, res) => {
  const { input } = req.query; // Extract single input parameter

  if (!input) {
    return res
      .status(400)
      .json({ error: "'input' query parameter must be provided." });
  }

  try {
    let videoData;

    if (isUrl(input)) {
      // Input is a URL
      videoData = await ytmp4(input);
    } else {
      // Input is a query, perform YouTube search
      const searchResults = await yts(input);
      if (!searchResults.videos || searchResults.videos.length === 0) {
        return res.status(404).json({ error: "No videos found for the query." });
      }
      const firstResult = searchResults.videos[0];
      videoData = await ytmp4(firstResult.url);
    }

    // Get video size
    const videoSize = await getVideoSize(videoData.video);

    // Respond with video details
    const { title, video, author, description, duration, views, upload, thumbnail } = videoData;
    res.json({
      title,
      videoUrl: video,
      author,
      description,
      duration,
      views,
      uploadDate: upload,
      thumbnail,
      size: videoSize ? `${(videoSize / 1048576).toFixed(2)} MB` : "Unknown", // Convert bytes to MB
    });
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: "Failed to process the request." });
  }
});

app.listen(PORT, HOST, () => {
  console.log(`Running on http://${HOST}:${PORT}`);
});