Reaperxxxx commited on
Commit
618efa5
·
verified ·
1 Parent(s): 4b029ec

Update server.js (#1)

Browse files

- Update server.js (e57a4100b29aa3391a6a150249ac53d5f1407583)

Files changed (1) hide show
  1. server.js +23 -5
server.js CHANGED
@@ -100,17 +100,35 @@ app.get("/search", async (req, res) => {
100
  return res.status(404).json({ error: "Redirected download link not found" });
101
  }
102
 
103
- // Step 5: Follow the redirection and extract the final file URL
 
 
104
  const downloadPage = await context.newPage();
105
- await downloadPage.goto(redirectedDownloadLink, { waitUntil: "networkidle", timeout: 60000 });
 
 
 
 
 
 
 
 
 
106
 
107
- // Capture final file download link from response headers
108
- const response = await downloadPage.waitForResponse(response => response.status() === 200);
109
 
110
- let finalDownloadURL = response.url();
 
 
 
111
 
112
  await downloadPage.close();
113
 
 
 
 
 
 
114
  // Extract movie name from filename in URL
115
  let cleanedMovieTitle = movieData.title
116
  .replace(/[_\.-]/g, " ") // Replace underscores and hyphens with spaces
 
100
  return res.status(404).json({ error: "Redirected download link not found" });
101
  }
102
 
103
+ console.log(`Redirecting to final download page: ${redirectedDownloadLink}`);
104
+
105
+ // Step 5: Follow the redirection, intercept network requests, and extract the REAL file URL
106
  const downloadPage = await context.newPage();
107
+ let finalDownloadURL = null;
108
+
109
+ // Capture the final URL using network interception
110
+ downloadPage.on("response", async (response) => {
111
+ const url = response.url();
112
+ if (url.match(/\.(mp4|mkv|avi|mov)$/i)) {
113
+ console.log(`Final File Found: ${url}`);
114
+ finalDownloadURL = url;
115
+ }
116
+ });
117
 
118
+ await downloadPage.goto(redirectedDownloadLink, { waitUntil: "domcontentloaded", timeout: 60000 });
 
119
 
120
+ // If no final URL is found, check the current page URL (sometimes it's a direct link)
121
+ if (!finalDownloadURL) {
122
+ finalDownloadURL = downloadPage.url();
123
+ }
124
 
125
  await downloadPage.close();
126
 
127
+ if (!finalDownloadURL) {
128
+ await browser.close();
129
+ return res.status(500).json({ error: "Failed to retrieve final download link" });
130
+ }
131
+
132
  // Extract movie name from filename in URL
133
  let cleanedMovieTitle = movieData.title
134
  .replace(/[_\.-]/g, " ") // Replace underscores and hyphens with spaces