Как обойти ошибку err_invalid_url в модуле request-promise
Делаю парсинг сайтов, но если url содержит кирилицу, вылетает ошибка err_invalid_url и сервер падает, причем идет проверка и на редирект, как можно исправить это или как предотвратить редирект, если сайт редиректит на другой домен?
А так же есть ли способ ускорить проверку без потери точности данных?
import fs from 'fs';
import readline from 'readline';
import rp from 'request-promise';
var sites = []
const file = readline.createInterface({
input: fs.createReadStream('50000.txt'),
output: process.stdout,
terminal: false
});
file.on('line', (line) => {
sites.push(line.substring(0, line.toLowerCase().indexOf(".ru") + 3))
});
var sitesList = []
var inc = 0
function setSitesList() {
console.log("site #" + inc)
if (sites.length === 0) return;
for (var i = 0; i < sites.length; i++) {
//if (i > 999) break;
sitesList.push(sites.shift())
}
var interval = sitesList.length
for (var i = 0; i < interval; i++) {
getStatusUrl(sitesList.shift())
}
}
file.on("close", setSitesList)
function getStatusUrl(url) {
var options = {
method: 'GET',
uri: 'http://' + url + '/',
resolveWithFullResponse: true,
gzip: true,
followAllRedirects: true // <--- <--- <--- <---
};
rp(options)
.then(function (response) {
var status
var reg = /\+79|\+7 9|\+7 \(9|\+7\(9|звонок|812|495|499|tel:\+|телефон\b|тел\b|phone:\b|Услуги\b/gm
let body = response.body.toLowerCase()
if (reg.test(body)) {
status = "E-comm"
}
if (body.indexOf('window.location') != -1 && body.indexOf('domains.domainname.ru/?') != -1) {
status = "n/a"
}
if (body.indexOf('window.location') != -1 || body.indexOf('http-equiv="Refresh" content="0') != -1 || body.indexOf("http-equiv='Refresh' content='0") != -1 || (body.indexOf('http-equiv="refresh"') != -1 && body.indexOf('content="0') != -1))
redirect.forEach((string) => {
if (body.indexOf(string.toLowerCase() + url.toLowerCase()) == -1) {
status = "n/a"
}
})
var bool
parking.forEach((string) => {
if (body.indexOf(string.toLowerCase()) != -1) {
status = "Parking"
}
})
if (body.indexOf(("Домен " + url + " продается").toLowerCase()) != -1 || body.indexOf(("Домен " + url + " продаётся").toLowerCase()) != -1 || body.indexOf((url + " возможно продается").toLowerCase()) != -1 || body.indexOf(("ДОМЕН " + url + " ЗАРЕГИСТРИРОВАН").toLowerCase()) != -1 || body.indexOf("начальная страница " + url) != -1) {
status = "Parking"
}
if (body.length < 10)
status = "n/a"
if (!status)
status = response.statusCode
if (status == 200)
querySetTest(url, status, null, null, null)
})
.catch(function (err) {
//querySetTest(url, "n/a", null, null, null)
});
}
async function querySetTest(
url,
status = 'n/a'
) {
console.log(`call insertSite('${url}','${status}')`)
await setTimeout(() => {
data.querySet(`call insertSite('${url}','${status}')`);
}, 200 * Math.random());
}