File size: 5,872 Bytes
7a94f89
 
 
 
a469773
 
bbc4967
9de8359
 
 
 
 
a469773
7a94f89
a469773
7a94f89
 
 
a469773
 
7a94f89
 
 
 
a469773
7a94f89
a469773
7a94f89
 
9de8359
7a94f89
 
 
 
cd1f16f
 
7a94f89
 
9de8359
7a94f89
 
 
cd1f16f
7a94f89
 
 
 
a469773
 
 
 
7a94f89
 
 
a469773
7a94f89
 
 
 
 
9de8359
7a94f89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b029ec
 
7a94f89
 
a469773
 
7a94f89
6426504
7a94f89
 
 
 
 
 
6426504
7a94f89
e4e188f
7a94f89
 
 
6426504
7a94f89
 
 
6426504
7a94f89
 
 
 
 
618efa5
a469773
7a94f89
9de8359
 
 
7a94f89
618efa5
7a94f89
 
6426504
7a94f89
6426504
7a94f89
 
 
 
 
 
 
 
 
a469773
7a94f89
 
a469773
7a94f89
 
a469773
 
7a94f89
 
 
 
 
 
 
a469773
 
 
7a94f89
 
a469773
 
 
7a94f89
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const express = require('express');
const { chromium } = require('playwright-core');
const axios = require('axios');
const cheerio = require('cheerio');

const app = express();
const PORT = 7860;

// Disable SSL verification for Axios
const axiosInstance = axios.create({
    httpsAgent: new (require('https').Agent)({ rejectUnauthorized: false })
});

app.get('/getDownload', async (req, res) => {
    try {
        const { moviename, episode } = req.query;
        if (!moviename) {
            return res.status(400).json({ error: "moviename query parameter is required" });
        }

        const searchQuery = moviename.trim();
        const episodeFilter = episode ? episode.trim() : null;
        const searchUrl = `https://nkiri.com/?s=${encodeURIComponent(searchQuery)}`;
        let firstMovieLink = null;

        console.log("\n🔍 Searching for:", searchQuery);

        try {
            // Attempt with Axios
            const { data: searchPage } = await axiosInstance.get(searchUrl);
            const $search = cheerio.load(searchPage);
            firstMovieLink = $search('article.post a').attr('href') || $search('h2.entry-title a').attr('href');
        } catch (error) {
            console.log("\n⚠️ Axios failed, switching to Playwright...");
        }

        if (!firstMovieLink) {
            // Fallback to Playwright if Axios fails
            const browser = await chromium.launch({ headless: true, args: ['--ignore-certificate-errors'] });
            const page = await browser.newPage();
            await page.goto(searchUrl, { waitUntil: 'networkidle' });
            await page.waitForSelector('article.post a, h2.entry-title a', { timeout: 10000 });

            firstMovieLink = await page.evaluate(() => {
                let linkElement = document.querySelector('article.post a') || document.querySelector('h2.entry-title a');
                return linkElement ? linkElement.href : null;
            });

            await browser.close();
        }

        if (!firstMovieLink) {
            return res.status(404).json({ error: "No movie found" });
        }

        console.log("\n✅ First Movie Page Found:", firstMovieLink);

        // ===========================
        // 2️⃣ GET DOWNLOAD LINKS FROM THE MOVIE PAGE
        // ===========================
        const { data: moviePage } = await axiosInstance.get(firstMovieLink);
        const $movie = cheerio.load(moviePage);

        let downloadLinks = [];
        const videoExtensions = ['.mkv', '.mp4', '.mov', '.avi'];

        $movie('a').each((i, el) => {
            const link = $movie(el).attr('href');
            if (
                link &&
                (link.includes('downloadwella.com') ||
                    link.includes('wetafiles.com') ||
                    videoExtensions.some(ext => link.endsWith(ext)))
            ) {
                downloadLinks.push(link);
            }
        });

        if (downloadLinks.length === 0) {
            return res.status(404).json({ error: "No valid download links found" });
        }

        let selectedLink = downloadLinks[0]; // Default to first link

        if (downloadLinks.length > 1 && episodeFilter) {
            const filteredLinks = downloadLinks.filter(link => link.includes(`E${episodeFilter}`) || link.includes(`e${episodeFilter}`));
            if (filteredLinks.length > 0) {
                selectedLink = filteredLinks[0]; // Use episode-specific link if found
            }
        }

        console.log("\n✅ Selected Download Link:", selectedLink);

        // Extract movie name from filename
        const filename = selectedLink.split('/').pop();
        const movieTitle = filename.replace(/\.(mkv|mp4|mov|avi).*$/, '').replace(/[\.\-_\(\)]/g, ' ').trim();

        // ===========================
        // 3️⃣ HANDLE FINAL DOWNLOAD LINK (WITH PLAYWRIGHT)
        // ===========================

        if (videoExtensions.some(ext => selectedLink.endsWith(ext))) {
            return res.json({
                movie: movieTitle,
                finalDownloadUrl: selectedLink
            });
        }

        // Open Playwright browser
        const browser = await chromium.launch({ headless: true, args: ['--ignore-certificate-errors'] });
        const context = await browser.newContext({ ignoreHTTPSErrors: true });
        const page = await context.newPage();
        await page.goto(selectedLink, { waitUntil: 'networkidle' });

        // Wait for the "Create Download Link" button
        await page.waitForSelector('#downloadbtn', { timeout: 15000 });

        console.log("\n✅ Download button found, clicking...");

        // Capture final download link
        let finalDownloadUrl = null;
        page.on('response', async (response) => {
            const requestUrl = response.url();
            if (requestUrl.includes('/d/') && videoExtensions.some(ext => requestUrl.endsWith(ext))) {
                finalDownloadUrl = requestUrl;
                console.log("\n✅ FINAL DOWNLOAD LINK:", finalDownloadUrl);
            }
        });

        // Click the button to generate final link
        await page.evaluate(() => document.querySelector('#downloadbtn').click());

        // Wait for some time to capture responses
        await new Promise(resolve => setTimeout(resolve, 10000));
        await browser.close();

        if (!finalDownloadUrl) {
            return res.status(404).json({ error: "Failed to extract final download link" });
        }

        res.json({
            movie: movieTitle,
            finalDownloadUrl
        });

    } catch (error) {
        console.error("\n❌ Error:", error.message);
        res.status(500).json({ error: "Internal server error" });
    }
});

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