diff --git a/lib/request/request.js b/lib/request/request.js index 7948311..1e84239 100644 --- a/lib/request/request.js +++ b/lib/request/request.js @@ -24,11 +24,12 @@ const checkStatus = response => { } } -const qs = (obj) => { +export const qs = (obj) => { let res = '' for (const [k, v] of Object.entries(obj)) { res += `${k}=${encodeURIComponent(v)}&` } return res.slice(0, res.length - 1) } + export default new class { /** * @description: Get请求 @@ -47,7 +48,7 @@ export default new class { 'User-Agent': CHROME_UA, ...options.headers } - if (!options.agent) options.agent = await this.getAgent() + if (!options.agent) options.agent = this.getAgent() try { let res = await fetch(url, options) if (!options.closeCheckStatus) { @@ -89,7 +90,7 @@ export default new class { } delete options.data } - if (!options.agent) options.agent = await this.getAgent() + if (!options.agent) options.agent = this.getAgent() try { let res = await fetch(url, options) if (!options.closeCheckStatus) { @@ -113,7 +114,7 @@ export default new class { * @return {FetchObject} */ async cfGet (url, options = {}) { - options.agent = await this.getAgent(true) + options.agent = this.getAgent(true) options.headers = { 'User-Agent': POSTMAN_UA, ...options.headers @@ -129,7 +130,7 @@ export default new class { * @return {FetchObject} */ async cfPost (url, options = {}) { - options.agent = await this.getAgent(true) + options.agent = this.getAgent(true) options.headers = { 'User-Agent': POSTMAN_UA, ...options.headers @@ -137,7 +138,7 @@ export default new class { return this.post(url, options) } - async getAgent (cf) { + getAgent (cf) { let { proxyAddress, switchProxy } = Config.proxy; let { cfTLSVersion } = Config.picSearch return cf ? this.getTlsVersionAgent(proxyAddress, cfTLSVersion) : (switchProxy ? new HttpsProxyAgent(proxyAddress) : false) } @@ -171,7 +172,7 @@ export default new class { * @return {segment.image} 构造图片消息 */ async proxyRequestImg (url, { cache, timeout, headers } = {}) { - if (!await this.getAgent()) return segment.image(url, cache, timeout, headers) + if (!this.getAgent()) return segment.image(url, cache, timeout, headers) let buffer = await this.get(url, { headers }).then(res => res.arrayBuffer()) diff --git a/model/Pixiv/api.js b/model/Pixiv/api.js index 94900cc..361c5a9 100644 --- a/model/Pixiv/api.js +++ b/model/Pixiv/api.js @@ -1,4 +1,4 @@ -import request from '../../lib/request/request.js' +import request, { qs } from '../../lib/request/request.js' import moment from 'moment' import { Config } from '../../components/index.js' import md5 from 'md5' @@ -57,9 +57,9 @@ export default class PixivApi { } } - async request (target, options = {}) { + async request (target, options = {}, caching = false) { try { - return await this._get(target, options) + return await this._get(target, options, caching) } catch (error) { if (this._once) { this._once = false @@ -67,16 +67,33 @@ export default class PixivApi { } await this.login() this._once = true - return await this._get(target, options) + return await this._get(target, options, caching) } } - async _get (target, options = {}) { + async _get (target, options = {}, cache) { const headers = { ...this.headers, Authorization: `Bearer ${this.access_token}` } - return request[options.data ? 'post' : 'get'](this.baseUrl + target, { headers, ...options, statusCode: 'json' }) + // 读取缓存 + const cacheUrl = options.params ? target + '?' + qs(options.params) : target + const cacheKey = `yenai:pixiv:cache:${cacheUrl}` + const cacheData = await redis.get(cacheKey) + if (cacheData) return JSON.parse(cacheData) + // 请求 + let data = await request[options.data ? 'post' : 'get'](this.baseUrl + target, { + headers, + ...options, + statusCode: 'json' + }) + // 写入缓存 + if (cache) { + redis.set(cacheKey, JSON.stringify(data), { + EX: timeToSeconds(cache) + }) + } + return data } async tags () { @@ -95,7 +112,7 @@ export default class PixivApi { date, offset: (page - 1) * size } - }) + }, getNoonTomorrow()) } async illust ({ id }) { @@ -184,3 +201,38 @@ export default class PixivApi { return await this.request('v1/illust/recommended', params) } } +function timeToSeconds (time) { + let seconds = 0 + let timeArray = time.split(' ') + for (let i = 0; i < timeArray.length; i++) { + let unit = timeArray[i].charAt(timeArray[i].length - 1) + let value = parseInt(timeArray[i].substring(0, timeArray[i].length - 1)) + switch (unit) { + case 's': + seconds += value + break + case 'm': + seconds += value * 60 + break + case 'h': + seconds += value * 60 * 60 + break + case 'd': + seconds += value * 60 * 60 * 24 + break + default: + break + } + } + return seconds +} + +function getNoonTomorrow () { + const now = moment() // 获取当前时间 + const noonToday = moment().startOf('day').add(12, 'hours') // 获取今天中午12点的时间 + const noonTomorrow = moment().add(1, 'day').startOf('day').add(12, 'hours') // 获取明天中午12点的时间 + + return (now < noonToday + ? noonToday.diff(now, 'hours') + : noonTomorrow.diff(now, 'hours')) + 'h' +}