mirror of
https://github.com/yoimiya-kokomi/miao-plugin.git
synced 2024-11-16 04:35:42 +00:00
enka请求增加proxy配置,按需引入
#更新面板会基于enka服务返回的ttl设置cd
This commit is contained in:
parent
1c744f8234
commit
5c94304c70
@ -400,12 +400,12 @@ export async function uploadData (e, { render }) {
|
||||
return false
|
||||
}
|
||||
let MysApi = await e.getMysApi({
|
||||
auth: 'cookie',
|
||||
auth: 'all',
|
||||
targetType: 'self',
|
||||
cookieType: 'all',
|
||||
action: '获取深渊信息'
|
||||
})
|
||||
if (!MysApi || !MysApi.isSelfCookie) return true
|
||||
if (!MysApi || !MysApi.isSelfCookie) return false
|
||||
let ret = {}
|
||||
let uid = e.selfUser.uid
|
||||
let resDetail, resAbyss
|
||||
|
@ -34,23 +34,28 @@ let Profile = {
|
||||
}
|
||||
let Serv = getServ(uid)
|
||||
|
||||
let inCd = await redis.get(`miao:role-all:${uid}`)
|
||||
if (inCd === 'loading') {
|
||||
e.reply('请求过快,请稍后重试..')
|
||||
return false
|
||||
} else if (inCd === 'pending') {
|
||||
e.reply(`距上次请求刷新成功间隔小于${requestInterval}分钟,请稍后重试..`)
|
||||
let cdTime = await Profile.inCd(uid)
|
||||
if (cdTime) {
|
||||
e.reply(`请求过快,请${cdTime}秒后重试..`)
|
||||
return false
|
||||
}
|
||||
|
||||
await redis.set(`miao:role-all:${uid}`, 'loading', { EX: 20 })
|
||||
await Profile.setCd(uid, 20) // 发起请求设置20s cd
|
||||
e.reply('开始获取数据,可能会需要一定时间~')
|
||||
await sleep(1000)
|
||||
await sleep(500)
|
||||
let data
|
||||
try {
|
||||
data = await Serv.request({ uid, e, sysCfg, diyCfg })
|
||||
data = await Serv.request({ uid, e, sysCfg, diyCfg, setCd: Profile.setCd })
|
||||
if (!data) {
|
||||
return false
|
||||
}
|
||||
// enka服务测冷却时间5分钟
|
||||
await redis.set(`miao:role-all:${uid}`, 'pending', { EX: requestInterval * 60 })
|
||||
let cdTime = requestInterval * 60
|
||||
if (data.ttl) {
|
||||
cdTime = Math.max(cdTime, data.ttl * 1)
|
||||
delete data.ttl
|
||||
}
|
||||
await Profile.setCd(uid, cdTime) // 根据设置设置cd
|
||||
return Profile.save(uid, data, Serv.key)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
@ -59,6 +64,23 @@ let Profile = {
|
||||
}
|
||||
},
|
||||
|
||||
async setCd (uid, cdTime = 5 * 60) {
|
||||
let ext = new Date() * 1 + cdTime * 1000
|
||||
await redis.set(`miao:role-all:${uid}`, ext + '', { EX: cdTime })
|
||||
},
|
||||
|
||||
async inCd (uid) {
|
||||
let ext = await redis.get(`miao:role-all:${uid}`)
|
||||
if (!ext || isNaN(ext)) {
|
||||
return false
|
||||
}
|
||||
let cd = new Date() * 1 - ext
|
||||
if (cd < 0) {
|
||||
return Math.ceil(0 - cd / 1000)
|
||||
}
|
||||
return false
|
||||
},
|
||||
|
||||
save (uid, data, dataSource = 'enka') {
|
||||
let userData = {}
|
||||
const userFile = `${userPath}/${uid}.json`
|
||||
|
@ -64,6 +64,10 @@ let Data = {
|
||||
ret.chars[char.id] = char
|
||||
})
|
||||
|
||||
if (data.ttl) {
|
||||
ret.ttl = data.ttl
|
||||
}
|
||||
|
||||
return ret
|
||||
},
|
||||
getAvatar (data) {
|
||||
|
@ -1,10 +1,11 @@
|
||||
import fetch from 'node-fetch'
|
||||
import EnkaData from './enka-data.js'
|
||||
//import HttpsProxyAgent from "https-proxy-agent";
|
||||
import Data from '../Data.js'
|
||||
|
||||
let Enka = {
|
||||
key: 'enka',
|
||||
cd: 5,
|
||||
async request ({ e, uid, avatar, diyCfg, sysCfg }) {
|
||||
async request ({ e, uid, avatar, diyCfg, sysCfg, setCd }) {
|
||||
let url = diyCfg?.enkaApi?.url || sysCfg.enkaApi.url
|
||||
let profileApi = diyCfg?.enkaApi?.listApi || sysCfg.enkaApi.listApi
|
||||
let api = profileApi({ url, uid, avatar })
|
||||
@ -12,8 +13,9 @@ let Enka = {
|
||||
api += '?key=' + diyCfg.enkaApi.apiKey
|
||||
}
|
||||
let config = { headers: { 'User-Agent': diyCfg?.enkaApi?.userAgent || sysCfg.enkaApi.userAgent } }
|
||||
if (diyCfg?.enkaApi?.proxyAgent){
|
||||
// config.agent=new HttpsProxyAgent(diyCfg.enkaApi.proxyAgent)
|
||||
if (diyCfg?.enkaApi?.proxyAgent) {
|
||||
let { HttpsProxyAgent } = await Data.importModule('./plugins/miao-plugin/components/profile-data/', 'enka-proxy.js')
|
||||
config.agent = new HttpsProxyAgent(diyCfg.enkaApi.proxyAgent)
|
||||
}
|
||||
let req = await fetch(api, config)
|
||||
let data = await req.json()
|
||||
@ -24,6 +26,7 @@ let Enka = {
|
||||
let details = data.avatarInfoList
|
||||
if (!details || details.length === 0 || !details[0].propMap) {
|
||||
e.reply('请打开游戏内角色展柜的【显示详情】后,等待5分钟重新获取面板')
|
||||
await setCd(uid, 5 * 60)
|
||||
return false
|
||||
}
|
||||
return EnkaData.getData(uid, data)
|
||||
|
@ -7,6 +7,7 @@
|
||||
* Enka面板服务API配置
|
||||
*
|
||||
* 【Enka官网】:https://enka.network/
|
||||
*
|
||||
* 感谢Enka提供的面板查询服务
|
||||
* https://github.com/yoimiya-kokomi/miao-plugin/issues/63#issuecomment-1199348789
|
||||
* 如果可以的话,也可考虑在Patreon上支持Enka,或提供闲置的原神账户,具体可在Discord联系
|
||||
@ -24,8 +25,8 @@
|
||||
* */
|
||||
|
||||
export const enkaApi = {
|
||||
url: 'https://enka.network/',
|
||||
proxyAgent:''
|
||||
url: 'https://enka.network/', // 请求API地址,可从上方提供的API地址中进行选择
|
||||
proxyAgent: '' // 请求的proxy配置,如无需proxy则留空
|
||||
}
|
||||
|
||||
/*
|
||||
|
Loading…
Reference in New Issue
Block a user