Skip to content

Commit

Permalink
统一使用 node-fetch,删除 axios (yoimiya-kokomi#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeRainStarSky authored Aug 14, 2023
1 parent cd818ed commit 233d027
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 85 deletions.
62 changes: 31 additions & 31 deletions .puppeteerrc.cjs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
const os = require("os");
const { existsSync } = require("fs");
const arch = os.arch();
let skipDownload = false;
let executablePath;

// win32 存在 Edge 优先选择
if (process.platform == "win32") {
if (existsSync("C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe")) {
skipDownload = true;
executablePath = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe";
}
} else if (process.platform == "linux") {
// 如果 arm64 架构且存在 Chromium,跳过下载
if ((arch == "arm64" || arch == "aarch64") && existsSync("/usr/bin/chromium")) {
skipDownload = true;
executablePath = "/usr/bin/chromium";
} else if (existsSync("/usr/bin/chromium")) {
// 不论什么架构,如果存在 Chromium,跳过下载且配置路径
skipDownload = true;
executablePath = "/usr/bin/chromium";
}
}

/**
* @type {import("puppeteer").Configuration}
*/
module.exports = {
skipDownload,
executablePath,
};
const os = require("os");
const { existsSync } = require("fs");
const arch = os.arch();
let skipDownload = false;
let executablePath;

// win32 存在 Edge 优先选择
if (process.platform == "win32") {
if (existsSync("C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe")) {
skipDownload = true;
executablePath = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe";
}
} else if (process.platform == "linux") {
// 如果 arm64 架构且存在 Chromium,跳过下载
if ((arch == "arm64" || arch == "aarch64") && existsSync("/usr/bin/chromium")) {
skipDownload = true;
executablePath = "/usr/bin/chromium";
} else if (existsSync("/usr/bin/chromium")) {
// 不论什么架构,如果存在 Chromium,跳过下载且配置路径
skipDownload = true;
executablePath = "/usr/bin/chromium";
}
}

/**
* @type {import("puppeteer").Configuration}
*/
module.exports = {
skipDownload,
executablePath,
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
},
"dependencies": {
"art-template": "^4.13.2",
"axios": "^1.4.0",
"chalk": "^5.2.0",
"chokidar": "^3.5.3",
"https-proxy-agent": "5.0.1",
Expand Down
81 changes: 28 additions & 53 deletions plugins/genshin/apps/takeBirthdayPhoto.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import plugin from '../../../lib/plugins/plugin.js'
import gsCfg from '../model/gsCfg.js'
// import axios from 'axios'
let axios = null
let getAxios = async function () {
if (axios) {
return axios
}
try {
axios = (await import('axios')).default
} catch (e) {
console.log('未安装依赖axios...')
return false
}
return axios
}
import fetch from 'node-fetch'

export class takeBirthdayPhoto extends plugin {
constructor () {
Expand Down Expand Up @@ -60,10 +47,10 @@ export class takeBirthdayPhoto extends plugin {
return true
}

for (let role of birthday_star_list) {
for (const role of birthday_star_list) {
await e.reply(`正在获取${role.name}的图片,请稍等~`, true)
await e.reply(segment.image(role.take_picture))
let message = await this.getBirthdayStarImg(userInfo.uid, e_hk4e_token, userInfo.ck, role.role_id)
const message = await this.getBirthdayStarImg(userInfo.uid, e_hk4e_token, userInfo.ck, role.role_id)
if (message != 'success') {
await e.reply(message)
return true
Expand All @@ -85,7 +72,7 @@ export class takeBirthdayPhoto extends plugin {
const url = isCN ? 'https://api-takumi.mihoyo.com/common/badge/v1/login/account' : 'https://api-os-takumi.mihoyo.com/common/badge/v1/login/account'
const game_biz = isCN ? 'hk4e_cn' : 'hk4e_global'
const region = await this.getServer(uid)
const header = {
const headers = {
'Cookie': ck,
'Content-Type': 'application/json;charset=UTF-8',
'Referer': 'https://webstatic.mihoyo.com/',
Expand All @@ -97,15 +84,12 @@ export class takeBirthdayPhoto extends plugin {
lang: 'zh-cn',
region: region
})
let axios = await getAxios()
if (!axios) {
return false
}
let res = await axios.post(url, body, { headers: header })
if (res.data.retcode != 0) {
let res = await fetch(url, { method: "POST", body, headers })
const e_hk4e_token = res.headers.get('set-cookie').match(/e_hk4e_token=(.*?);/)[1]
res = await res.json()
if (res.retcode != 0) {
return false
}
let e_hk4e_token = res.headers['set-cookie'].toString().match(/e_hk4e_token=(.*?);/)[1]
return e_hk4e_token
}

Expand All @@ -129,38 +113,29 @@ export class takeBirthdayPhoto extends plugin {
}

async getBirthdayStar (uid, e_hk4e_token, ck) {
let cookie = `e_hk4e_token=${e_hk4e_token};${ck}`
let badge_region = await this.getServer(uid)
let isCN = uid.toString().match(/^[125]/) ? true : false
let game_biz = isCN ? 'hk4e_cn' : 'hk4e_global'
let header = {
'Cookie': cookie
}
let url = `https://hk4e-api.mihoyo.com/event/birthdaystar/account/index?lang=zh-cn&badge_uid=${uid}&badge_region=${badge_region}&game_biz=${game_biz}&activity_id=20220301153521`
let axios = await getAxios()
if (!axios) {
return false
}
let res = await axios.get(url, { headers: header })
return res.data.data.role
const cookie = `e_hk4e_token=${e_hk4e_token};${ck}`
const badge_region = await this.getServer(uid)
const isCN = uid.toString().match(/^[125]/) ? true : false
const game_biz = isCN ? 'hk4e_cn' : 'hk4e_global'
const headers = { 'Cookie': cookie }
const url = `https://hk4e-api.mihoyo.com/event/birthdaystar/account/index?lang=zh-cn&badge_uid=${uid}&badge_region=${badge_region}&game_biz=${game_biz}&activity_id=20220301153521`
let res = await fetch(url, { headers })
res = await res.json()
return res.data.role
}

async getBirthdayStarImg (uid, e_hk4e_token, ck, role_id) {
let cookie = `e_hk4e_token=${e_hk4e_token};${ck}`
let badge_region = await this.getServer(uid)
let isCN = uid.toString().match(/^[125]/) ? true : false
let game_biz = isCN ? 'hk4e_cn' : 'hk4e_global'
let header = {
'Cookie': cookie
}
let url = `https://hk4e-api.mihoyo.com/event/birthdaystar/account/post_my_draw?lang=zh-cn&badge_uid=${uid}&badge_region=${badge_region}&game_biz=${game_biz}&activity_id=20220301153521`
let axios = await getAxios()
if (!axios) {
return false
}
let res = await axios.post(url, { role_id: Number(role_id) }, { headers: header })
if (res.data.retcode != 0) {
return res.data.message
const cookie = `e_hk4e_token=${e_hk4e_token};${ck}`
const badge_region = await this.getServer(uid)
const isCN = uid.toString().match(/^[125]/) ? true : false
const game_biz = isCN ? 'hk4e_cn' : 'hk4e_global'
const headers = { 'Cookie': cookie }
const url = `https://hk4e-api.mihoyo.com/event/birthdaystar/account/post_my_draw?lang=zh-cn&badge_uid=${uid}&badge_region=${badge_region}&game_biz=${game_biz}&activity_id=20220301153521`
const body = JSON.stringify({ role_id: Number(role_id) })
let res = await fetch(url, { method: "POST", body, headers })
res = await res.json()
if (res.retcode != 0) {
return res.message
} else {
return 'success'
}
Expand Down

0 comments on commit 233d027

Please sign in to comment.