miao-plugin/tools/weapon-data.js

153 lines
5.1 KiB
JavaScript
Raw Normal View History

import fetch from 'node-fetch'
import cheerio from 'cheerio'
import lodash from 'lodash'
2023-03-07 17:52:11 +00:00
import { Data } from '#miao'
import fs from 'fs'
import request from 'request'
2022-11-19 22:33:00 +00:00
import WeaponData from './sprider/WeaponData.js'
import ImgDownloader from './sprider/ImgDown.js'
let ret = {}
const types = ['sword', 'claymore', 'polearm', 'bow', 'catalyst']
for (let type of types) {
2023-03-07 17:52:11 +00:00
ret[type] = Data.readJSON(`resources/meta/weapon/${type}/data.json`, 'miao')
}
2023-03-07 17:52:11 +00:00
let mData = Data.readJSON('resources/meta/material/data.json', 'miao')
let weaponIdMap = Data.readJSON('tools/meta/weapon.json', 'miao')
2022-11-19 22:33:00 +00:00
let getWeaponTypeData = async function (type) {
let url = `https://genshin.honeyhunterworld.com/fam_${type}/?lang=CHS`
console.log('req: ' + url)
let req = await fetch(url, {
method: 'GET',
headers: {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.20',
referer: 'https://genshin.honeyhunterworld.com/fam_chars/?lang=CHS',
'sec-ch-ua': '"Microsoft Edge";v = "105", " Not;A Brand";v = "99", "Chromium";v = "105"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': 'Windows',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': 1
}
})
let txt = await req.text()
2023-05-04 11:29:10 +00:00
let sTxt = /sortable_data.push\((\[.*?\])[\n\s]*\)/.exec(txt)
console.log('1', sTxt[1])
if (sTxt && sTxt[1]) {
2023-05-04 11:29:10 +00:00
let tmp = eval(sTxt[1])
lodash.forEach(tmp, (ds) => {
let a = cheerio.load(ds[1])('a')
let name = a.text()
2023-05-04 11:29:10 +00:00
console.log(a.attr('href'))
let idRet = /i_(.*)\//.exec(a.attr('href'))
let star = cheerio.load(ds[2])('img').length
2023-02-27 19:20:43 +00:00
let wid = idRet && idRet[1] ? idRet[1] : ''
ret[type] = ret[type] || {}
2023-02-27 19:20:43 +00:00
let tmp = {
id: weaponIdMap[name] || '',
name,
star
}
2023-02-27 19:20:43 +00:00
if (wid !== 'n' + tmp.id) {
tmp.wid = wid
}
2023-05-04 11:29:10 +00:00
console.log(tmp)
2023-02-27 19:20:43 +00:00
ret[type][name] = tmp
})
}
}
2022-11-19 22:33:00 +00:00
let getWeaponData = async function (type, ds) {
let { id, name } = ds
2023-02-27 19:20:43 +00:00
let url = `https://genshin.honeyhunterworld.com/i_n${id}/?lang=CHS`
2022-11-19 22:33:00 +00:00
console.log(`req:[${name}], ${url}`)
let req = await fetch(url, {
method: 'GET',
headers: {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.20',
referer: 'https://genshin.honeyhunterworld.com/fam_chars/?lang=CHS',
'sec-ch-ua': '"Microsoft Edge";v = "105", " Not;A Brand";v = "99", "Chromium";v = "105"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': 'Windows',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': 1
}
})
let txt = await req.text()
let $ = cheerio.load(txt)
let imgs = new ImgDownloader(name)
$.imgs = imgs
let ret = WeaponData.getDetail($, ds, mData)
await $.imgs.download()
return ret
}
2022-12-10 01:49:23 +00:00
async function down (t, n) {
for (let type of types) {
if (type !== t) {
continue
}
2022-11-19 22:33:00 +00:00
await getWeaponTypeData(type)
2023-03-07 17:52:11 +00:00
Data.createDir(`resources/meta/weapon/${type}`, 'miao')
Data.writeJSON({ name: `resources/meta/weapon/${type}/data.json`, data: ret[type], rn: true, root: 'miao' })
let imgs = []
2023-02-27 19:20:43 +00:00
await Data.asyncPool(6, lodash.keys(ret[type]), async (name) => {
2022-12-10 01:49:23 +00:00
if (n && n !== name) {
2023-02-27 19:20:43 +00:00
return
2022-11-19 22:33:00 +00:00
}
let ds = ret[type][name]
2023-03-07 17:52:11 +00:00
Data.createDir(`resources/meta/weapon/${type}/${ds.name}`, 'miao')
2022-11-19 22:33:00 +00:00
let data = await getWeaponData(type, ds)
2023-03-07 17:52:11 +00:00
Data.writeJSON({ name: `resources/meta/weapon/${type}/${ds.name}/data.json`, data, rn: true, root: 'miao' })
lodash.forEach({
icon: '',
awaken: '_awaken_icon',
gacha: '_gacha_icon'
}, (affix, key) => {
imgs.push({
url: `img/i_${ds.id}${affix}.webp`,
file: `${type}/${ds.name}/${key}.webp`
})
})
2023-02-27 19:20:43 +00:00
})
const _path = process.cwd()
const _root = _path + '/plugins/miao-plugin/'
const _wRoot = _root + 'resources/meta/weapon/'
await Data.asyncPool(5, imgs, async function (ds) {
if (fs.existsSync(`${_wRoot}/${ds.file}`)) {
// console.log(`已存在,跳过 ${ds.file}`)
return true
}
try {
let stream = fs.createWriteStream(`${_wRoot}/${ds.file}.tmp`)
await request('https://genshin.honeyhunterworld.com/' + ds.url).pipe(stream)
return new Promise((resolve) => {
stream.on('finish', () => {
fs.rename(`${_wRoot}/${ds.file}.tmp`, `${_wRoot}/${ds.file}`, () => {
console.log(`图像下载成功: ${ds.file}`)
resolve()
})
})
})
} catch (e) {
console.log(`图像下载失败: ${ds.file}`)
console.log(e)
return false
}
})
}
2023-03-07 17:52:11 +00:00
Data.writeJSON({ name: 'resources/meta/material/data.json', data: mData, rn: true, root: 'miao' })
}
// 'sword', 'claymore', 'polearm', 'bow', 'catalyst'
2023-05-04 11:29:10 +00:00
await down('catalyst', '')