fetch


const ICP_MAP : Array<string> = ['域名','是否备案','主办单位名称','主办单位性质','网站备案/许可证号', '网站名称','网站首页网址','审核时间'];
const WHOIS_MAP = new Map<string,string>();
WHOIS_MAP.set('域名', 'host');
WHOIS_MAP.set('联系人', 'contactPerson');
WHOIS_MAP.set('注册商', 'registrar');
WHOIS_MAP.set('联系邮箱', 'email');
WHOIS_MAP.set('联系电话', 'phone');
WHOIS_MAP.set('创建时间', 'creationDate');
WHOIS_MAP.set('过期时间', 'expirationDate');
WHOIS_MAP.set('更新时间', 'updateDate');
WHOIS_MAP.set('域名服务器', 'whoisServer');
WHOIS_MAP.set('DNS', 'dnsServer');
WHOIS_MAP.set('状态', 'domainStatus');
WHOIS_MAP.set('公司', 'company');
//icp error
const ICP_ERROR = ['输入参数错误', '未备案或备案取消'];
//whois 查询三种错误提示
const whoisErrorMess = ['站长工具暂不支持', '该域名未被注册或被隐藏', '该域名被屏蔽'];
const whoisErrorMessMap = ['暂不支持该域名查询', '该域名未被注册或被隐藏', '该域名被屏蔽'];

async getIcpInfo(domain: string):Promise<IcpInfoResp>

{
// e.g https://icp.chinaz.com/home/info?host=baidu.com
if((/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/).test(domain)) {
throw this.new_prod_error('ICP_DOMAIN_ERROR')
}
const url: string = ICP_INFO + domain;
let resp;
try {
resp = await fetch(url, {
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36'
},
})
} catch (error) {
this.baseLogError(`${url} ,' fetch from icp.chinaz.com failed'`, error)
throw this.new_prod_error('ICP_FETCH_ERROR')
}
const res: string = await resp.text();
let error: string = '';
const $ = cheerio.load(res);
let data: Array<{
name: string,
value: string
}> = [{ name: '是否备案' , value: "是"}];
let check = $('div.Tool-MainWrap.wrapper.pr > div:nth-child(7) > p').text().replace(/\s/g, '');
ICP_ERROR.forEach((value, i) => {
if (check.indexOf(value) !== -1) {
if (i !== 1) {
return {data:[], error:'value'}
}
else
{
data.push({ name: '是否备案' , value: "是"})
}
}
})
data.push({name: '域名', value:`${$('div.Tool-MainWrap.wrapper.pr > div:nth-child(7) > div.pl15.lh30.ptb5.bor-t1s01.clearfix > p > span > a').text()}`})
$('ul[class="bor-t1s IcpMain01"]').children().each(function (i, e) {
let title = $(this).children().eq(0).text().replace(/\s/g, '');
let mess = $(this).children().eq(1).text().replace(/\s/g, '').replace('查看截图', '');
let find = ICP_MAP.find( value => title === value )
if (find) {
data.push({name: title, value: mess})
}
});
console.log ({ data, error });
return { data, error }
}



 async getWhoisInfo(domain: string): Promise<WhoisInfoResp>
 {
 const url = WHOIS_INFO + domain;
 let resp;
 try {
 resp = await fetch(url, {
 method: 'GET',
 headers: {
 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36'
 },
 })
 } catch (error) {
 this.baseLogError(`${url} ,' fetch from icp.chinaz.com failed'`, error)
 throw this.throw_prod_error('WHOIS_FETCH_ERROR')
 }
 const res: string = await resp.text();
 let flag: boolean = resp.ok;
 let error: string = '';
 let data: WhoisInfo = {}
 const $ = cheerio.load(res);
 let check = $('div[class=IcpMain02]').children().eq(0).text();
 whoisErrorMess.forEach((value, i) => {
 if(check.indexOf(value) != -1) {
 error = whoisErrorMessMap[i];
 flag = false;
 }
 });
 if(flag) {
 $('#sh_info').children().each(function (i, e) {
 if($(this).children().length == 2) {
 let title = $(this).children().eq(0).text().replace(/\s/g, '');
 let mess = $(this).children().eq(1);
 let text = mess.text().replace(/\s/g, '');
 let del = text.indexOf('[whois');
 if(del != -1)
 text = text.substring(0, del);
 if(title == 'DNS') {
 let temp = mess.html().split('<br>');
 data[WHOIS_MAP.get(title)] = {
 name: title,
 value:[temp[0], temp[1]],
 };
 continue;
 }
 data[WHOIS_MAP.get(title)] = {name: title, value: text};
 }
 });
 }
 console.log ({flag, data, error});
 return {data, error};
 }

声明:一个萌新|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - fetch


耳不闻人之非,目不视人之短,口不言人之过。