miao-plugin/tools/char-data-sprider.js

245 lines
6.1 KiB
JavaScript
Raw Normal View History

import fs from 'fs'
import cheerio from 'cheerio'
import lodash from 'lodash'
import fetch from 'node-fetch'
import { roleId, abbr } from '../../../config/genshin/roleId.js'
const _path = process.cwd()
let roleIdMap = {}
2022-04-07 20:51:47 +00:00
lodash.forEach(roleId, (names, id) => {
roleIdMap[names[0]] = id
})
2022-04-07 19:39:43 +00:00
function getBasic ($, name) {
2022-04-07 19:39:43 +00:00
let ret = {}
2022-04-07 20:51:47 +00:00
if (name) {
ret.name = name
2022-04-07 20:51:47 +00:00
} else {
// 采集基础信息
ret.name = $('#scroll_card_item').next('table').find('tr:first td:eq(1)').text()
2022-04-07 20:51:47 +00:00
}
ret.abbr = abbr[ret.name] || ret.name
ret.id = roleIdMap[ret.name] || ''
2022-04-07 20:51:47 +00:00
let basic = $('.data_cont_wrapper table:first')
console.log('basic', basic)
2022-04-07 19:39:43 +00:00
let title = function (title) {
return basic.find(`td:contains('${title}')`).next('td').text()
2022-04-07 19:39:43 +00:00
}
ret.title = title('Title')
ret.star = basic.find('td:contains(\'Rarity\')').next('td').find('.sea_char_stars_wrap').length
let elem = basic.find('td:contains(\'Element\')').next('td').find('img').attr('data-src')
2022-04-07 20:51:47 +00:00
let elemRet = /\/([^/]*)_35/.exec(elem)
2022-04-07 20:51:47 +00:00
if (elemRet) {
ret.elem = elemRet[1]
2022-04-07 20:51:47 +00:00
}
ret.allegiance = title('Allegiance')
ret.weapon = title('Weapon Type')
ret.britydah = title('Birthday')
ret.astro = title('Astrolabe Name')
ret.cncv = title('Chinese Seiyuu')
ret.jpcv = title('Japanese Seiyuu')
ret.desc = title('In-game Description')
return ret
2022-04-07 19:39:43 +00:00
}
function getStat ($) {
2022-04-07 19:39:43 +00:00
// 采集属性信息
let stat = $('#beta_scroll_stat').next('.skilldmgwrapper').find('table')
let attrs = []
let idx = 4
stat.find('tr:first td:gt(0)').each(function (i) {
let title = $(this).text()
if (title === 'Ascension') {
idx = i + 1
console.log(idx)
return false
2022-04-07 19:39:43 +00:00
}
attrs.push($(this).text())
})
let lvs = []
let lvStat = {}
stat.find('tr:gt(0)').each(function (i) {
let tr = $(this)
let lvl = tr.find('td:first').text()
lvs.push(lvl)
let data = []
2022-04-07 19:39:43 +00:00
tr.find(`td:lt(${idx})`).each(function (i) {
if (i > 0) {
data.push($(this).text())
}
})
lvStat[lvl] = data
})
2022-04-07 19:39:43 +00:00
return {
lvs,
stat: attrs,
detail: lvStat
}
2022-04-07 19:39:43 +00:00
}
function getTalents ($, eq, onlyLv1 = false) {
let root = $('#beta_scroll_attack_talent')
let info = root.nextAll(`.item_main_table:eq(${eq})`)
2022-04-07 19:39:43 +00:00
let name = info.find('tr:first td:eq(1)').text()
let icon = info.find('tr:first td:first img').attr('data-src')
2022-04-07 19:39:43 +00:00
// 说明
let desc = info.find('tr:eq(1) td div.skill_desc_layout').html()
desc = desc.replace(/<color=[^>]*>/g, '')
desc = desc.replace(/<\/color=[^>]*>/g, '')
desc = desc.replace(/<span class=[^>]*>/g, '<strong>')
desc = desc.replace(/<\/span>/g, '</strong>')
desc = desc.split('<br>')
2022-04-07 19:39:43 +00:00
lodash.forEach(desc, (txt, i) => {
desc[i] = lodash.trim(txt)
2022-04-07 19:39:43 +00:00
})
// detail
let detail = root.nextAll(`.skilldmgwrapper:eq(${eq})`).find('table')
let lvs = []
let details = []
detail.find('tr:first td').each(function (i) {
if (onlyLv1 && i > 1) {
return false
}
2022-04-07 19:39:43 +00:00
if (i > 0) {
lvs.push($(this).text())
}
})
detail.find('tr:gt(0)').each(function () {
let name = $(this).find('td:eq(0)').text()
let values = []
let isSame = true
$(this).find('td:gt(0)').each(function (i) {
if (onlyLv1 && i > 0) {
return false
}
let val = lodash.trim($(this).text())
values.push(val)
2022-04-07 19:39:43 +00:00
if (i > 0 && values[0] !== val) {
isSame = false
2022-04-07 19:39:43 +00:00
}
})
2022-04-07 19:39:43 +00:00
details.push({
2022-04-07 21:06:06 +00:00
name, isSame, values
2022-04-07 19:39:43 +00:00
})
})
2022-04-07 19:39:43 +00:00
return {
2022-04-07 21:06:06 +00:00
name,
2022-04-07 19:39:43 +00:00
icon,
desc,
2022-04-07 21:06:06 +00:00
tables: details,
2022-04-07 19:39:43 +00:00
lvs
}
}
let getPassive = function ($, name) {
let table = $('#beta_scroll_passive_talent').next('table')
let ret = []
table.find('tr').each(function (idx) {
2022-04-07 19:39:43 +00:00
if (idx % 2 === 0) {
let ds = {}
ds.icon = $(this).find('td:first img').attr('data-src')
ds.name = $(this).find('td:eq(1)').text()
ret[idx / 2] = ds
2022-04-07 19:39:43 +00:00
} else {
ret[(idx - 1) / 2].desc = $(this).find('td').text()
2022-04-07 19:39:43 +00:00
}
})
if (name === '莫娜' || name === '神里绫华') {
ret.push(getTalents($, 2, true))
}
return ret
2022-04-07 19:39:43 +00:00
}
let getCons = function ($) {
let table = $('#beta_scroll_constellation').next('table')
let ret = {}
table.find('tr').each(function (idx) {
2022-04-07 19:39:43 +00:00
if (idx % 2 === 0) {
let ds = {}
ds.icon = $(this).find('td:first img').attr('data-src')
ds.name = $(this).find('td:eq(1)').text()
ret[idx / 2 + 1] = ds
2022-04-07 19:39:43 +00:00
} else {
ret[(idx + 1) / 2].desc = $(this).find('td').text()
2022-04-07 19:39:43 +00:00
}
})
return ret
2022-04-07 19:39:43 +00:00
}
let getImgs = function ($) {
let cont = $('#scroll_gallery').next('.homepage_index_cont')
2022-04-07 19:39:43 +00:00
let img = function (idx, _cont) {
return (_cont || cont).find(`.gallery_content_cont:eq(${idx}) a`).attr('href')
2022-04-07 19:39:43 +00:00
}
let card = $('#scroll_name_card').nextAll('.homepage_index_cont:first')
2022-04-07 19:39:43 +00:00
return {
face: img(0),
side: img(1),
gacha_card: img(2),
gacha_splash: img(3),
profile: img(1, card),
party: img(2, card),
char: $('#live_data table.item_main_table:first td:first img').attr('data-src')
2022-04-07 19:39:43 +00:00
}
}
let getCharData = async function (url, name = '') {
url = 'https://genshin.honeyhunterworld.com/' + url
console.log('req' + url)
2022-04-07 19:39:43 +00:00
let req = await fetch(url)
let txt = await req.text()
2022-04-07 20:51:47 +00:00
const $ = cheerio.load(txt)
let ret = getBasic($, name)
name = ret.name
ret.lvStat = getStat($)
2022-04-07 19:39:43 +00:00
ret.talent = {
a: getTalents($, 0),
e: getTalents($, 1),
q: getTalents($, name === '莫娜' || name === '神里绫华' ? 3 : 2)
2022-04-07 19:39:43 +00:00
}
ret.passive = getPassive($, name)
ret.cons = getCons($)
ret.imgs = getImgs($)
return ret
2022-04-07 19:39:43 +00:00
}
async function saveCharData (url, name) {
let data = await getCharData(url, name)
name = name || data.name
2022-05-17 07:38:23 +00:00
if (!name) {
console.log('角色名不存在' + url)
return
2022-05-17 07:38:23 +00:00
}
let charPath = `${_path}/plugins/miao-plugin/resources/meta/character/${data.name}/`
if (!fs.existsSync(charPath)) {
fs.mkdirSync(charPath)
}
fs.writeFileSync(`${charPath}data.json`, JSON.stringify(data, '', '\t'))
console.log(data.name + '下载完成')
}
async function down () {
await saveCharData('/collei_067/?lang=CHS', '柯莱')
2022-04-07 19:39:43 +00:00
}
// await saveCharData("https://genshin.honeyhunterworld.com/db/char/ayaka/?lang=CHS", "ayaka");
2022-04-07 19:39:43 +00:00
await down()