85 lines
3.1 KiB
JavaScript
85 lines
3.1 KiB
JavaScript
const cheer = require('cheerio');
|
|
const axios = require('axios');
|
|
const http = require('https');
|
|
const sizeOf = require('image-size');
|
|
const qs = require('qs');
|
|
class utils {
|
|
constructor() {
|
|
this.ax = axios
|
|
this.ResponseInterceptor()
|
|
}
|
|
|
|
/**
|
|
* 返回 cheerio 数据结构
|
|
* @param html
|
|
* @returns {*}
|
|
*/
|
|
cheerio(html) {
|
|
return cheer.load(html)
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param url 请求的地址
|
|
* @returns {Promise<AxiosResponse<any>>}
|
|
*/
|
|
async get(url) {
|
|
return await this.ax.get(url, {
|
|
headers: {
|
|
'Cookie': 'UM_distinctid=17ded35632a529-0b0dffb1765c8c-36657407-232800-17ded35632b93f; PHPSESSID=snskonuvdhtsn757ja3uiau61b; fr=mtuji001; uid=154298; name=2271608011; leixing=0; CNZZDATA1257039673=1795578984-1640353617-https%253A%252F%252Fwww.tuji001.com%252F%7C1643720559',
|
|
'authority': 'www.tujidao01.com',
|
|
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36',
|
|
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
|
|
}
|
|
});
|
|
}
|
|
|
|
async AgentPost(url, params = {}) {
|
|
return await this.ax.post(url, qs.stringify(params), {
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'refer': '',
|
|
'TOKEN': '/NWOYjE9WKn66bBitGVVEy7XfnW2sYpjGUJR5IzndI0l9yLhb/Btqd01ln1JWBA+8e8nmuivxpJrElXCnxPSJg==',
|
|
'Cookie': 'UM_distinctid=17ded35632a529-0b0dffb1765c8c-36657407-232800-17ded35632b93f; PHPSESSID=snskonuvdhtsn757ja3uiau61b; fr=mtuji001; uid=154298; name=2271608011; leixing=0; CNZZDATA1257039673=1795578984-1640353617-https%253A%252F%252Fwww.tuji001.com%252F%7C1643720559',
|
|
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
*
|
|
* 响应拦截器
|
|
* @constructor
|
|
*/
|
|
async ResponseInterceptor() {
|
|
this.ax.interceptors.response.use((response) => {
|
|
if (response.config.method == 'post') {
|
|
return response.data;
|
|
} else {
|
|
if (response.config.url.includes("index.php")) {
|
|
return response.data
|
|
} else {
|
|
return this.cheerio(response.data);
|
|
}
|
|
}
|
|
}, (error) => {
|
|
console.log("请求错误...");
|
|
return Promise.reject(error)
|
|
})
|
|
}
|
|
|
|
async GetImageWidthOrHeight(url) {
|
|
return new Promise((resolve,reject) => {
|
|
http.get(url, function (response) {
|
|
var chunks = [];
|
|
response.on('data', function (chunk) {
|
|
chunks.push(chunk);
|
|
}).on('end', function () {
|
|
var buffer = Buffer.concat(chunks);
|
|
resolve(sizeOf(buffer))
|
|
});
|
|
});
|
|
})
|
|
}
|
|
}
|
|
module.exports = new utils(); |