Skip to content

Commit a8fbb62

Browse files
fix(fetch-domains): fix script for fetching domains info, changed headers and interseptor (#77)
1 parent bad8148 commit a8fbb62

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ node_modules
66
# misc
77
.DS_Store
88
*.pem
9+
10+
.idea

website/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ next-env.d.ts
3636

3737
public/domains.json
3838
public/icons/
39+
40+
.idea

website/tools/fetch-domains.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ const axios = require('axios');
33
const { JSDOM } = require('jsdom');
44
const imageSize = require('image-size');
55

6-
const axiosInstance = axios.create();
7-
axiosInstance.defaults.maxRedirects = 0; // Set to 0 to prevent automatic redirects
8-
axiosInstance.defaults.timeout = 1000; // Set to 1 second
9-
axiosInstance.defaults.adapter = 'http';
10-
axiosInstance.defaults.headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
11-
axiosInstance.defaults.headers['Accept-Encoding'] = 'gzip, deflate, br';
12-
axiosInstance.defaults.headers['Accept-Language'] = 'en-US,en;q=1';
13-
axiosInstance.defaults.headers['Cache-Control'] = 'no-cache';
6+
const axiosInstance = axios.create({
7+
timeout: 8000,
8+
maxRedirects: 5,
9+
headers: {
10+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
11+
'Accept-Encoding': 'gzip, deflate, br',
12+
'Accept-Language': 'en-US,en;q=1',
13+
'Cache-Control': 'no-cache',
14+
'User-Agent': 'Mozilla/5.0 (compatible; PasskeysCrawler/1.0)'
15+
}
16+
});
17+
1418
axiosInstance.interceptors.response.use(
15-
response => response,
19+
r => r,
1620
error => {
17-
if (error.response && [301, 302].includes(error.response.status)) {
18-
let redirectUrl = error.response.headers.location;
19-
if (redirectUrl.startsWith('/')) {
20-
const { url } = error.response.config;
21-
redirectUrl = url + redirectUrl;
22-
}
23-
console.log('Redirecting to', error.response.headers.location);
24-
return axiosInstance.get(redirectUrl);
21+
const res = error.response;
22+
if (res && (res.status === 301 || res.status === 302)) {
23+
const redirectUrl = new URL(res.headers.location, res.config.url).href;
24+
return axiosInstance.get(redirectUrl, { timeout: 8000 });
2525
}
2626
return Promise.reject(error);
2727
}
@@ -224,10 +224,14 @@ async function processDomains(inputFile, outputFile, debug = false) {
224224
const results = [];
225225

226226
for (const domain of domains) {
227-
const result = await fetchDomainInfo(domain, debug);
228-
console.log(domain, result);
229-
if (result) {
230-
results.push(result);
227+
try {
228+
const result = await fetchDomainInfo(domain, debug);
229+
console.log(domain, result);
230+
if (result) {
231+
results.push(result);
232+
}
233+
} catch (error) {
234+
console.error(`Error fetching domain ${domain}:`, error);
231235
}
232236
}
233237

0 commit comments

Comments
 (0)