vikarshana commited on
Commit
bf86a13
·
verified ·
1 Parent(s): 9eb9558

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +20 -17
index.js CHANGED
@@ -4,7 +4,7 @@ const port = process.env.PORT || 7860
4
  const cookieParser = require('cookie-parser')
5
  const puppeteer = require('puppeteer-extra');
6
  const StealthPlugin = require('puppeteer-extra-plugin-stealth');
7
- const axios = require("axios");
8
 
9
  puppeteer.use(StealthPlugin());
10
 
@@ -96,6 +96,9 @@ app.get('/headers', async (req, res) => {
96
  });
97
 
98
 
 
 
 
99
  app.get("/api/direct", async (req, res) => {
100
  const { url } = req.query;
101
  if (!url) return res.status(400).json({ status: false, error: "Missing url parameter" });
@@ -128,25 +131,24 @@ app.get("/api/direct", async (req, res) => {
128
  return res.status(404).json({ status: false, error: "Download link not found" });
129
  }
130
 
131
- // Step 3: Set cookies (if any) from the current page
132
  const cookies = await page.cookies();
133
- await page.setCookie(...cookies);
134
-
135
- // Step 4: Intercept response to catch final redirect URL
136
- let finalUrl = null;
137
- page.on("response", (response) => {
138
- if (response.status() === 302 || response.status() === 301) {
139
- const location = response.headers()['location'];
140
- if (location && location.includes("dlws")) finalUrl = location;
 
 
 
141
  }
142
  });
143
 
144
- // Step 5: Navigate to intermediate URL with headers
145
- await page.goto(intermediate, {
146
- waitUntil: "networkidle2",
147
- timeout: 60000,
148
- });
149
-
150
  if (!finalUrl) {
151
  return res.status(500).json({ status: false, error: "Final URL not found" });
152
  }
@@ -154,8 +156,9 @@ app.get("/api/direct", async (req, res) => {
154
  res.json({
155
  status: true,
156
  intermediate,
157
- direct: finalUrl,
158
  });
 
159
  } catch (err) {
160
  res.status(500).json({ status: false, error: err.message });
161
  } finally {
 
4
  const cookieParser = require('cookie-parser')
5
  const puppeteer = require('puppeteer-extra');
6
  const StealthPlugin = require('puppeteer-extra-plugin-stealth');
7
+ const fetch = require("node-fetch");
8
 
9
  puppeteer.use(StealthPlugin());
10
 
 
96
  });
97
 
98
 
99
+ import fetch from "node-fetch"; // npm install node-fetch@2
100
+ import puppeteer from "puppeteer";
101
+
102
  app.get("/api/direct", async (req, res) => {
103
  const { url } = req.query;
104
  if (!url) return res.status(400).json({ status: false, error: "Missing url parameter" });
 
131
  return res.status(404).json({ status: false, error: "Download link not found" });
132
  }
133
 
134
+ // Step 3: Get cookies from Puppeteer page
135
  const cookies = await page.cookies();
136
+ const cookieHeader = cookies.map(c => `${c.name}=${c.value}`).join("; ");
137
+
138
+ // Step 4: Fetch intermediate URL using node-fetch with cookies
139
+ const response = await fetch(intermediate, {
140
+ method: "GET",
141
+ redirect: "manual", // we want to catch the redirect ourselves
142
+ headers: {
143
+ "Cookie": cookieHeader,
144
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
145
+ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
146
+ "Referer": url
147
  }
148
  });
149
 
150
+ // Step 5: Check for 302/301 redirect to get the final download link
151
+ const finalUrl = response.headers.get("location");
 
 
 
 
152
  if (!finalUrl) {
153
  return res.status(500).json({ status: false, error: "Final URL not found" });
154
  }
 
156
  res.json({
157
  status: true,
158
  intermediate,
159
+ direct: finalUrl
160
  });
161
+
162
  } catch (err) {
163
  res.status(500).json({ status: false, error: err.message });
164
  } finally {