netakhoj-api / webextract.mjs
Rakeshops71
Deploy app with LFS for large files
3eedfc9
Raw
History Blame Contribute Delete
1.38 kB
import fetch from 'node-fetch';
export async function fetchHTML(url, retries = 2, timeout = 15000) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
const response = await fetch(url, {
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"Cache-Control": "max-age=0",
"Referer": url.includes('prsindia.org') ? "https://prsindia.org/" : "https://www.myneta.info/"
},
signal: controller.signal,
redirect: 'follow',
compress: true
});
clearTimeout(timeoutId);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const html = await response.text();
return html;
} catch (error) {
if (attempt === retries) {
throw error;
}
await new Promise(resolve => setTimeout(resolve, 500 * attempt));
}
}
}