⚡️ 优化请求代码
This commit is contained in:
@@ -8,7 +8,6 @@ import _ from 'lodash'
|
||||
const CHROME_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36'
|
||||
const POSTMAN_UA = 'PostmanRuntime/7.29.0'
|
||||
|
||||
// let HttpsProxyAgent = ''
|
||||
export default new class {
|
||||
async get (url, options = {}) {
|
||||
// 处理参数
|
||||
@@ -27,22 +26,21 @@ export default new class {
|
||||
...options.headers
|
||||
}
|
||||
if (!options.agent)options.agent = await this.getAgent()
|
||||
return await fetch(url, options).catch(err => console.error(err))
|
||||
return await fetch(url, options)
|
||||
}
|
||||
|
||||
async post (url, options = {}) {
|
||||
if (options.data) options.body = JSON.stringify(options.data)
|
||||
options.method = 'POST'
|
||||
options.headers = {
|
||||
'User-Agent': CHROME_UA,
|
||||
...options.headers
|
||||
}
|
||||
if (!options.agent)options.agent = await this.getAgent()
|
||||
try {
|
||||
return await fetch(url, options).catch(err => console.error(err))
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
if (typeof options.data === 'object') {
|
||||
options.body = JSON.stringify(options.data)
|
||||
options.headers['Content-Type'] = 'application/json'
|
||||
}
|
||||
if (!options.agent)options.agent = await this.getAgent()
|
||||
return await fetch(url, options)
|
||||
}
|
||||
|
||||
async cfGet (url, options = {}) {
|
||||
@@ -98,10 +96,8 @@ export default new class {
|
||||
* @return {segment.image} 构造图片消息
|
||||
*/
|
||||
async proxyRequestImg (url, { cache, timeout, headers } = {}) {
|
||||
let agent = await this.getAgent()
|
||||
if (!agent) return segment.image(url, cache, timeout, headers)
|
||||
let buffer = await fetch(url, {
|
||||
agent,
|
||||
if (!await this.getAgent()) return segment.image(url, cache, timeout, headers)
|
||||
let buffer = await this.get(url, {
|
||||
headers
|
||||
}).then(res => res.arrayBuffer())
|
||||
.catch((err) => logger.warn(`图片加载失败 reason: ${err.message}`))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import fetch from 'node-fetch'
|
||||
import lodash from 'lodash'
|
||||
import { common } from './index.js'
|
||||
import request from '../lib/request/request.js'
|
||||
/** API请求错误文案 */
|
||||
const API_ERROR = '❎ 出错辣,请稍后重试'
|
||||
export default new (class {
|
||||
@@ -156,7 +156,6 @@ export default new (class {
|
||||
async requestBikaImg (fileServer, path) {
|
||||
fileServer = /static/.test(fileServer) ? fileServer : fileServer + '/static/'
|
||||
let url = (/picacomic.com/.test(fileServer) && this.imgproxy ? this.imgproxy : fileServer) + path
|
||||
console.log(url)
|
||||
return common.proxyRequestImg(url)
|
||||
return request.proxyRequestImg(url)
|
||||
}
|
||||
})()
|
||||
|
||||
@@ -59,7 +59,7 @@ const callAscii2dUrlApiWithPuppeteer = (imgUrl) => {
|
||||
return getAscii2dWithPuppeteer(`${domain}/search/url/${imgUrl}`)
|
||||
}
|
||||
const callAscii2dUrlApi = async (imgUrl) => {
|
||||
let res = await request.cfGet(`${domain}/search/url/${imgUrl}`)
|
||||
let res = await request.cfGet(`${domain}/search/url/${imgUrl}`).catch(err => console.error(err))
|
||||
if (!res || !res.ok) return false
|
||||
return {
|
||||
url: res.url,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import lodash from 'lodash'
|
||||
import { Config } from '../../components/index.js'
|
||||
import sagiri from '../../tools/sagiri.js'
|
||||
import { common } from '../index.js'
|
||||
import request from '../../lib/request/request.js'
|
||||
export default async function doSearch (url) {
|
||||
let res = await getSearchResult(url)
|
||||
@@ -12,7 +11,7 @@ export default async function doSearch (url) {
|
||||
|
||||
let msgMap = async item => [
|
||||
`SauceNAO (${item.similarity}%)\n`,
|
||||
Config.picSearch.hideImg ? '' : await common.proxyRequestImg(item.thumbnail),
|
||||
Config.picSearch.hideImg ? '' : await request.proxyRequestImg(item.thumbnail),
|
||||
`图源:${item.site}\n`,
|
||||
`作者:${item.authorName}(${item.authorUrl})\n`,
|
||||
`来源:${item.url.toString()}`
|
||||
@@ -56,7 +55,7 @@ async function getSearchResult (imgURL, db = 999) {
|
||||
url: imgURL,
|
||||
hide: Config.picSearch.hideImgWhenSaucenaoNSFW
|
||||
}
|
||||
})
|
||||
}).catch(err => console.error(err))
|
||||
if (!res || !res.ok) return { error: 'SauceNAO搜图网络请求失败,注:移动网络无法访问SauceNAO,可尝试配置代理' }
|
||||
return await res.json()
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ import fetch from 'node-fetch'
|
||||
import lodash from 'lodash'
|
||||
import { segment } from 'oicq'
|
||||
import moment from 'moment'
|
||||
import { common } from './index.js'
|
||||
import { rankType, MSG } from '../tools/pixiv.js'
|
||||
import request from '../lib/request/request.js'
|
||||
/** API请求错误文案 */
|
||||
const API_ERROR = '❎ 出错辣,请稍后重试'
|
||||
|
||||
@@ -458,7 +458,7 @@ export default new class Pixiv {
|
||||
*/
|
||||
async requestPixivImg (url) {
|
||||
url = url.replace('i.pximg.net', this.proxy)
|
||||
return common.proxyRequestImg(url, { headers: this.headers })
|
||||
return request.proxyRequestImg(url, { headers: this.headers })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import child_process from 'child_process'
|
||||
import fetch from 'node-fetch'
|
||||
import { segment } from 'oicq'
|
||||
import common from '../../../lib/common/common.js'
|
||||
import Config from '../components/Config.js'
|
||||
import setu from './setu.js'
|
||||
let HttpsProxyAgent = ''
|
||||
export default new class newCommon {
|
||||
/**
|
||||
* @description: 延时函数
|
||||
@@ -165,49 +162,6 @@ export default new class newCommon {
|
||||
} else return ck
|
||||
}
|
||||
|
||||
async getAgent () {
|
||||
let proxyAddress = Config.other.proxyAddress
|
||||
if (!Config.other.switchProxy) return null
|
||||
if (!proxyAddress) return null
|
||||
if (proxyAddress === 'http://0.0.0.0:0') return null
|
||||
|
||||
if (HttpsProxyAgent === '') {
|
||||
HttpsProxyAgent = await import('https-proxy-agent').catch((err) => {
|
||||
logger.error(err)
|
||||
})
|
||||
|
||||
HttpsProxyAgent = HttpsProxyAgent ? HttpsProxyAgent.default : undefined
|
||||
}
|
||||
|
||||
if (HttpsProxyAgent) {
|
||||
return new HttpsProxyAgent(proxyAddress)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 代理请求图片
|
||||
* @param {String} url 图片链接
|
||||
* @param {Boolean} cache 是否缓存
|
||||
* @param {Number} timeout 超时时间
|
||||
* @param {Object} headers 请求头
|
||||
* @return {segment.image} 构造图片消息
|
||||
*/
|
||||
async proxyRequestImg (url, { cache, timeout, headers } = {}) {
|
||||
let agent = await this.getAgent()
|
||||
if (!agent) return segment.image(url, cache, timeout, headers)
|
||||
let buffer = await fetch(url, {
|
||||
agent,
|
||||
headers
|
||||
}).then(res => res.arrayBuffer())
|
||||
.catch((err) => logger.warn(`图片加载失败 reason: ${err.message}`))
|
||||
if (!buffer) return segment.image('/plugins/yenai-plugin/resources/img/imgerror.png')
|
||||
let buff = Buffer.from(buffer)
|
||||
logger.debug(`Success: imgSize => ${(buff.length / 1024).toFixed(2) + 'kb'}`)
|
||||
return segment.image(buff, cache, timeout)
|
||||
}
|
||||
|
||||
/** 默认秒转换格式 */
|
||||
getsecondformat (value) {
|
||||
let time = this.getsecond(value)
|
||||
|
||||
Reference in New Issue
Block a user