-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserInfo.js
147 lines (133 loc) · 4.03 KB
/
userInfo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const pino = require('pino')
const NodeCache = require('node-cache')
const http = require('./utils/http')
const util = require('./utils/index')
const logger = pino({ level: process.env.LOG_LEVEL || 'info' })
const roleIdCache = new NodeCache({ stdTTL: 60 * 60 * 24 * 365 })
const cardCache = new NodeCache({ stdTTL: 60 * 60 * 24 })
const __API = {
FETCH_ROLE_ID:
'https://bbs-api-os.hoyolab.com/game_record/card/wapi/getGameRecordCard',
FETCH_ROLE_INDEX:
'https://bbs-api-os.hoyolab.com/game_record/genshin/api/index',
}
const HEADERS = {
Cookie: process.env.COOKIE,
}
// const MY_UID = process.env.MY_UID
// const COOKIE_PRIVATE = process.env.COOKIE_PRIVATE
async function getRoleInfo(uid) {
const key = `__uid__${uid}`
let cachedData = roleIdCache.get(key)
if (cachedData) {
logger.info(`Cached data: UID: ${uid}, ${cachedData}`)
return cachedData
}
const params = { uid: uid }
const rsp = (
await http({
method: 'GET',
url: __API.FETCH_ROLE_ID,
params: params,
headers: {
...HEADERS,
Cookie: HEADERS.Cookie,
},
})
).data
if (rsp.retcode !== 0) {
logger.error('获取角色ID接口报错')
// logger.error(rsp.message)
return
}
if (!rsp.data.list || rsp.data.list.length <= 0) {
logger.warn('无角色数据, uid %s', uid)
return
}
const roleInfo = rsp.data.list.find((_) => _.game_id === 2)
if (!roleInfo) {
logger.warn('无角色数据, uid %s', uid)
reject(
'无角色数据,请检查输入的米哈游通行证ID是否有误(非游戏内的UID)和是否设置了公开角色信息,若操作无误则可能是被米哈游屏蔽,请第二天再试'
)
}
logger.info(`UID: ${uid}`)
roleIdCache.set(key, roleInfo)
return roleInfo
}
async function userInfo({ uid, detail = false }) {
const key = `__uid__${uid}_${detail ? 'detail' : 'lite'}`
let cachedBody = cardCache.get(key)
if (cachedBody) {
if (cachedBody.retcode === 10101) {
return Promise.reject(cachedBody.message)
} else {
return cachedBody
}
}
const roleInfo = await getRoleInfo(uid)
const { game_role_id, region } = roleInfo
logger.info(region)
const params = { role_id: game_role_id, server: region }
if (!detail) {
const [
active_day_number,
avatar_number,
achievement_number,
spiral_abyss,
] = roleInfo.data
const parsed = {
active_day_number: active_day_number.value,
avatar_number: avatar_number.value,
achievement_number: achievement_number.value,
spiral_abyss: spiral_abyss.value,
}
const data = {
uid: game_role_id,
...parsed,
...roleInfo,
}
cardCache.set(key, data)
return data
}
const resp = (
await http({
method: 'GET',
url: __API.FETCH_ROLE_INDEX,
params: params,
headers: {
...HEADERS,
Cookie: HEADERS.Cookie,
},
})
).data
if (resp.retcode !== 0) {
logger.error('获取角色详情接口报错 %s', resp.message)
// logger.error(resp)
return Promise.reject(resp.message)
}
const { world_explorations } = resp.data
const percentage = Math.min(
(
(world_explorations.reduce(
(total, next) => total + next.exploration_percentage,
0
) /
world_explorations.length /
10000) *
1000
).toFixed(1),
100
)
const world_exploration = percentage
const data = {
uid: game_role_id,
world_exploration,
...resp.data.stats,
...roleInfo,
}
cardCache.set(key, data)
// logger.info(data)
return data
}
module.exports = userInfo