File size: 3,733 Bytes
3cc8c60
08fc82f
5994da8
 
5786384
d59e7d2
 
 
59429a4
5786384
d59e7d2
5786384
 
5994da8
 
d59e7d2
3cc8c60
df578dd
59429a4
08fc82f
df578dd
3cc8c60
5994da8
d59e7d2
 
5994da8
 
 
38faa33
 
5786384
5994da8
38faa33
 
 
3cc8c60
22ee2b4
d59e7d2
5786384
d59e7d2
 
 
 
5994da8
 
 
5786384
5994da8
 
5786384
5994da8
5786384
5994da8
 
5786384
 
 
 
 
 
 
 
 
 
 
 
 
5994da8
 
 
 
59429a4
 
 
 
 
 
 
 
 
 
5994da8
 
 
5786384
5994da8
 
d59e7d2
 
3cc8c60
d59e7d2
 
 
5786384
d59e7d2
 
 
 
df578dd
5786384
 
 
 
 
59429a4
5994da8
5786384
5994da8
df578dd
5786384
df578dd
 
5786384
3cc8c60
d59e7d2
 
 
5786384
5994da8
d59e7d2
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const express = require("express");
const { chromium } = require("playwright-core");
const fs = require("fs");
const path = require("path");
const axios = require("axios");

const app = express();
const PORT = 7860;
const DOMAIN = "https://reikerxx-ytm.hf.space";
const DOWNLOADS_DIR = path.join(__dirname, "downloads");

if (!fs.existsSync(DOWNLOADS_DIR)) {
    fs.mkdirSync(DOWNLOADS_DIR, { recursive: true });
}

async function getDownloadLink(youtubeUrl) {
    const browser = await chromium.launch({
        headless: true,
        executablePath: "/usr/bin/chromium",
        args: ["--no-sandbox", "--disable-setuid-sandbox"]
    });
    const page = await browser.newPage();
    let downloadLink = null;

    try {
        await page.goto("https://ogmp3.com", { waitUntil: "domcontentloaded" });
        await page.fill("#url", youtubeUrl);

        page.on("response", async (response) => {
            const url = response.url();
            if (url.includes("safestytmp3.cc") && url.includes("/download/")) {
                downloadLink = url;
            }
        });

        await page.click("#convert-button");
        await page.waitForTimeout(15000);
    } catch (error) {
        console.error("Error extracting download link:", error);
    } finally {
        await browser.close();
    }

    return downloadLink;
}

async function downloadMP3(url) {
    try {
        const response = await axios({
            url,
            method: "GET",
            responseType: "stream",
        });

        let fileName = `download_${Date.now()}.mp3`;
        const contentDisposition = response.headers["content-disposition"];
        if (contentDisposition) {
            const match = contentDisposition.match(/filename="?(.+?)"?$/);
            if (match) {
                fileName = match[1];
            }
        }
        if (!fileName.endsWith(".mp3")) {
            fileName += ".mp3";
        }

        const filePath = path.join(DOWNLOADS_DIR, fileName);
        const writer = fs.createWriteStream(filePath);
        response.data.pipe(writer);

        return new Promise((resolve, reject) => {
            writer.on("finish", () => {
                setTimeout(() => {
                    fs.unlink(filePath, (err) => {
                        if (!err) {
                            console.log(`Deleted: ${fileName}`);
                        }
                    });
                }, 300000);
                resolve(fileName);
            });
            writer.on("error", reject);
        });
    } catch (error) {
        console.error("Error downloading MP3:", error.message);
        return null;
    }
}

app.get("/api/q", async (req, res) => {
    const youtubeUrl = req.query.url;

    if (!youtubeUrl) {
        return res.status(400).json({ success: false, error: "Missing YouTube URL" });
    }

    console.log(`Processing: ${youtubeUrl}`);

    try {
        const downloadLink = await getDownloadLink(youtubeUrl);

        if (downloadLink) {
            const fileName = await downloadMP3(downloadLink);
            if (fileName) {
                res.json({ success: true, file: `${DOMAIN}/downloads/${fileName}` });
            } else {
                res.status(500).json({ success: false, error: "Failed to download MP3" });
            }
        } else {
            res.status(404).json({ success: false, error: "No MP3 link found" });
        }
    } catch (error) {
        console.error("Error processing YouTube link:", error);
        res.status(500).json({ success: false, error: "Internal Server Error" });
    }
});

app.use("/downloads", express.static(DOWNLOADS_DIR));

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