File size: 3,861 Bytes
ffaae78
 
 
 
 
9aa4e3d
 
906483e
9aa4e3d
 
 
ffaae78
9aa4e3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
906483e
 
 
 
 
 
 
 
 
 
9aa4e3d
 
906483e
 
 
 
0686eeb
 
ffaae78
 
0686eeb
 
ffaae78
 
 
0686eeb
ffaae78
54a131f
ffaae78
 
9aa4e3d
ffaae78
54a131f
d172c54
9aa4e3d
54a131f
ffaae78
 
9aa4e3d
 
ffaae78
 
9aa4e3d
 
 
 
 
 
ffaae78
9aa4e3d
 
 
ffaae78
9aa4e3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
906483e
 
 
ffaae78
9aa4e3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const express = require("express");
const { chromium } = require("playwright-core");
const cors = require("cors");
const { execSync } = require("child_process");
const axios = require("axios"); // Using Axios for better redirect handling

const app = express();
const PORT = process.env.PORT || 7860;

app.use(cors());

app.get("/download-links", async (req, res) => {
    const { url } = req.query;

    if (!url) {
        return res.status(400).json({ error: "Missing 'url' query parameter" });
    }

    try {
        console.log(`Scraping: ${url}`);
        const downloadLinks = await getVideoLinks(url);
        res.json({ downloadLinks });
    } catch (error) {
        res.status(500).json({ error: "Failed to fetch download links", details: error.message });
    }
});

async function getChromiumPath() {
    try {
        return execSync("which chromium-browser || which google-chrome || which chromium")
            .toString()
            .trim();
    } catch {
        return null; // No system Chromium found
    }
}

async function getVideoLinks(downloadPageUrl) {
    console.log(`Opening Playwright: ${downloadPageUrl}`);

    const chromiumPath = await getChromiumPath();
    console.log(`Chromium Path: ${chromiumPath || "Using Playwright's default"}`);

    const browser = await chromium.launch({
        headless: true,
        executablePath: chromiumPath || undefined,
        args: ["--no-sandbox", "--disable-blink-features=AutomationControlled", "--disable-dev-shm-usage"]
    });

    const context = await browser.newContext({
        userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
    });

    const page = await context.newPage();

    try {
        await page.goto(downloadPageUrl, { waitUntil: "domcontentloaded", timeout: 60000 });

        // Wait for the download buttons to load
        await page.waitForTimeout(5000);
        await page.waitForSelector('a[href*="ggredi.info/download.php"]', { timeout: 60000 });

        // Extract video links
        const videoLinks = await page.evaluate(() =>
            Array.from(document.querySelectorAll('a[href*="ggredi.info/download.php"]')).map(link => ({
                quality: link.innerText.trim(),
                url: link.href
            }))
        );

        await browser.close();

        // Convert links to final MP4 links
        const directDownloadLinks = {};
        const promises = videoLinks
            .filter(link => link.quality.includes("360") || link.quality.includes("720"))
            .map(async link => {
                const finalLink = await getFinalMp4Link(link.url);
                if (finalLink) {
                    directDownloadLinks[link.quality.replace(/\D/g, "") + "p"] = finalLink;
                }
            });

        await Promise.all(promises);
        return directDownloadLinks;
    } catch (error) {
        console.error("Error extracting video links:", error.message);
        await browser.close();
        return { "360p": "Not available", "720p": "Not available" };
    }
}

async function getFinalMp4Link(redirectLink) {
    try {
        console.log(`Following redirect: ${redirectLink}`);

        const response = await axios.get(redirectLink, { maxRedirects: 5 });

        const finalUrl = response.request.res.responseUrl || response.config.url;

        if (finalUrl && finalUrl.includes(".mp4")) {
            console.log(`Final MP4 Link: ${finalUrl}`);
            return finalUrl;
        } else {
            console.log("No valid MP4 link found.");
            return null;
        }
    } catch (error) {
        console.error("Error fetching final MP4 link:", error.message);
        return null;
    }
}

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