Reaperxxxx commited on
Commit
2f2d147
·
verified ·
1 Parent(s): 29d79d2

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +26 -47
server.js CHANGED
@@ -61,69 +61,48 @@ app.get("/search", async (req, res) => {
61
  console.log(`Movie Found: ${movieData.title}`);
62
  await page.goto(movieData.link, { waitUntil: "domcontentloaded", timeout: 60000 });
63
 
64
- // Step 2: Extract the LAST download1.php link
65
- const downloadPageLink = await page.evaluate(() => {
66
- const baseURL = "https://www.fzmovies.net/";
67
- const links = document.querySelectorAll('a[href*="download1.php?downloadoptionskey="]');
68
- return links.length ? baseURL + links[links.length - 1].getAttribute("href") : null;
69
- });
70
-
71
- if (!downloadPageLink) {
72
  await browser.close();
73
  return res.status(404).json({ error: "Download link not found" });
74
  }
75
 
76
- await page.goto(downloadPageLink, { waitUntil: "domcontentloaded", timeout: 60000 });
77
-
78
- // Step 3: Extract "download.php" page link (LAST one)
79
- const finalDownloadPage = await page.evaluate(() => {
80
- const baseURL = "https://www.fzmovies.net/";
81
- const links = document.querySelectorAll('a[href*="download.php?downloadkey="]');
82
- return links.length ? baseURL + links[links.length - 1].getAttribute("href") : null;
83
- });
84
 
85
- if (!finalDownloadPage) {
 
 
86
  await browser.close();
87
  return res.status(404).json({ error: "Final download page not found" });
88
  }
89
 
90
- await page.goto(finalDownloadPage, { waitUntil: "domcontentloaded", timeout: 60000 });
91
-
92
- // Step 4: Extract LAST redirection link (dlink.php)
93
- const redirectedDownloadLink = await page.evaluate(() => {
94
- const links = document.querySelectorAll('a[href*="dlink.php?id="]');
95
- return links.length ? links[links.length - 1].href : null;
96
- });
97
 
98
- if (!redirectedDownloadLink) {
 
 
99
  await browser.close();
100
  return res.status(404).json({ error: "Redirected download link not found" });
101
  }
102
 
103
- // Step 5: Follow the redirection and PRESERVE session cookies
104
- let finalDownloadURL = null;
105
- let retryAttempts = 2;
106
-
107
- while (!finalDownloadURL && retryAttempts > 0) {
108
- try {
109
- const downloadPage = await context.newPage();
110
- await downloadPage.goto(redirectedDownloadLink, {
111
- waitUntil: "networkidle",
112
- timeout: 60000
113
- });
114
-
115
- // Extract latest session-authenticated download link
116
- finalDownloadURL = await downloadPage.evaluate(() => window.location.href);
117
- await downloadPage.close();
118
- } catch (err) {
119
- console.warn(`Retrying... ${retryAttempts} attempts left`);
120
- retryAttempts--;
121
- }
122
- }
123
 
124
- if (!finalDownloadURL) {
125
  await browser.close();
126
- return res.status(500).json({ error: "Failed to retrieve final download link" });
127
  }
128
 
129
  // Extract movie name from the filename in the URL
 
61
  console.log(`Movie Found: ${movieData.title}`);
62
  await page.goto(movieData.link, { waitUntil: "domcontentloaded", timeout: 60000 });
63
 
64
+ // Step 2: Extract the LAST working download button & Click it
65
+ const downloadButton = await page.$('a[href*="download1.php?downloadoptionskey="]');
66
+ if (!downloadButton) {
 
 
 
 
 
67
  await browser.close();
68
  return res.status(404).json({ error: "Download link not found" });
69
  }
70
 
71
+ await Promise.all([
72
+ downloadButton.click(),
73
+ page.waitForNavigation({ waitUntil: "domcontentloaded", timeout: 60000 })
74
+ ]);
 
 
 
 
75
 
76
+ // Step 3: Click the LAST "download.php" button
77
+ const finalDownloadButton = await page.$('a[href*="download.php?downloadkey="]');
78
+ if (!finalDownloadButton) {
79
  await browser.close();
80
  return res.status(404).json({ error: "Final download page not found" });
81
  }
82
 
83
+ await Promise.all([
84
+ finalDownloadButton.click(),
85
+ page.waitForNavigation({ waitUntil: "domcontentloaded", timeout: 60000 })
86
+ ]);
 
 
 
87
 
88
+ // Step 4: Click the LAST "dlink.php" button
89
+ const redirectedDownloadButton = await page.$('a[href*="dlink.php?id="]');
90
+ if (!redirectedDownloadButton) {
91
  await browser.close();
92
  return res.status(404).json({ error: "Redirected download link not found" });
93
  }
94
 
95
+ await Promise.all([
96
+ redirectedDownloadButton.click(),
97
+ page.waitForNavigation({ waitUntil: "networkidle", timeout: 60000 })
98
+ ]);
99
+
100
+ // Step 5: Extract Final Working Download URL
101
+ const finalDownloadURL = page.url();
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
+ if (!finalDownloadURL.includes(".mp4") && !finalDownloadURL.includes(".mkv") && !finalDownloadURL.includes(".avi") && !finalDownloadURL.includes(".mov")) {
104
  await browser.close();
105
+ return res.status(500).json({ error: "Failed to retrieve valid final download link" });
106
  }
107
 
108
  // Extract movie name from the filename in the URL