2022-09-03 21:08:57 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import request from 'request'
|
2023-03-07 17:52:11 +00:00
|
|
|
import { Data } from '#miao'
|
2023-03-03 22:01:11 +00:00
|
|
|
import HttpsProxyAgent from 'https-proxy-agent'
|
|
|
|
|
|
|
|
let agent = new HttpsProxyAgent('http://localhost:4780')
|
|
|
|
request.defaults({
|
|
|
|
agent
|
|
|
|
})
|
2022-09-03 21:08:57 +00:00
|
|
|
|
|
|
|
const _path = process.cwd()
|
|
|
|
const _root = _path + '/plugins/miao-plugin/'
|
|
|
|
const _cRoot = _root + 'resources/meta/character/'
|
|
|
|
|
|
|
|
export default class ImgDown {
|
|
|
|
constructor (name) {
|
|
|
|
this.name = name
|
|
|
|
this.imgs = []
|
|
|
|
}
|
|
|
|
|
|
|
|
add (key, url) {
|
|
|
|
this.imgs.push({
|
|
|
|
url,
|
|
|
|
file: `${this.name}/${key}.webp`
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
add2 (name, type, url) {
|
|
|
|
this.imgs.push({
|
|
|
|
url,
|
|
|
|
file: `../material/${type}/${name}.webp`
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async _down (ds) {
|
|
|
|
if (fs.existsSync(`${_cRoot}/${ds.file}`)) {
|
|
|
|
// console.log(`已存在,跳过 ${ds.file}`)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
let stream = fs.createWriteStream(`${_cRoot}/${ds.file}`)
|
|
|
|
await request('https://genshin.honeyhunterworld.com/' + ds.url).pipe(stream)
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
stream.on('finish', () => {
|
|
|
|
console.log(`图像下载成功: ${ds.file}`)
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
console.log(`图像下载失败: ${ds.file}`)
|
|
|
|
console.log(e)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async download () {
|
|
|
|
await Data.asyncPool(5, this.imgs, this._down)
|
|
|
|
}
|
|
|
|
}
|