️ 错误处理

This commit is contained in:
yeyang
2023-02-07 02:56:21 +08:00
parent 6bf3944cf0
commit f7d2370c4b
9 changed files with 147 additions and 156 deletions

View File

@@ -52,8 +52,8 @@ export class NewBika extends plugin {
e.reply(Pixiv.startMsg)
let regRet = e.msg.match(searchReg)
let page = common.translateChinaNum(regRet[5])
let msg = await Bika.search(regRet[3], page, regRet[2])
if (msg.error) return e.reply(msg.error)
let msg = await Bika.search(regRet[3], page, regRet[2]).catch(err => { e.reply(err.message) })
if (!msg) return
common.getRecallsendMsg(e, msg)
}
@@ -64,8 +64,8 @@ export class NewBika extends plugin {
let regRet = e.msg.match(comicPageReg)
let page = common.translateChinaNum(regRet[4])
let order = common.translateChinaNum(regRet[6])
let msg = await Bika.comicPage(regRet[2], page, order)
if (msg.error) return e.reply(msg.error)
let msg = await Bika.comicPage(regRet[2], page, order).catch(err => { e.reply(err.message) })
if (!msg) return
common.getRecallsendMsg(e, msg)
}
@@ -73,8 +73,8 @@ export class NewBika extends plugin {
async categories (e) {
if (!this.handlePermission()) return e.reply(SWITCH_ERROR)
e.reply(Pixiv.startMsg)
let msg = await Bika.categories()
if (msg.error) return e.reply(msg.error)
let msg = await Bika.categories().catch(err => { e.reply(err.message) })
if (!msg) return
common.getRecallsendMsg(e, msg)
}
@@ -83,8 +83,8 @@ export class NewBika extends plugin {
if (!this.handlePermission()) return e.reply(SWITCH_ERROR)
e.reply(Pixiv.startMsg)
let id = e.msg.match(new RegExp(`#?${Prefix}(详情|细节)(.*)`))[3]
let msg = await Bika.comicDetail(id)
if (msg.error) return e.reply(msg.error)
let msg = await Bika.comicDetail(id).catch(err => { e.reply(err.message) })
if (!msg) return
common.getRecallsendMsg(e, msg, { oneMsg: true })
}

View File

@@ -66,18 +66,26 @@ export class newPicSearch extends plugin {
async SauceNAO (e) {
if (!await this.handelImg(e, 'SauceNAO')) return
let res = await PicSearch.SauceNAO(e.img[0])
if (res.error) {
e.reply(res.error)
return this.Ascii2D(e)
}
.catch(err => {
e.reply(err.message)
e.reply('自动使用Ascii2D进行搜索')
this.Ascii2D(e)
})
if (!res) return
res.maxSimilarity > 80 ? common.recallsendMsg(e, res.message, true) : common.getRecallsendMsg(e, res.message)
if (res.maxSimilarity < Config.picSearch.SauceNAOMinSim) {
e.reply(`SauceNAO 相似度 ${res.maxSimilarity}% 过低使用Ascii2D进行搜索`)
this.Ascii2D(e)
}
}
async Ascii2D (e) {
if (!await this.handelImg(e, 'Ascii2D')) return
let res = await PicSearch.Ascii2D(e.img[0])
if (res?.error) return e.reply(res.error)
let res = await PicSearch.Ascii2D(e.img[0]).catch(err => {
e.reply(err.message)
})
if (!res) return
common.getRecallsendMsg(e, res.color, { isxml: false })
common.getRecallsendMsg(e, res.bovw, { isxml: false })
}

View File

@@ -76,19 +76,12 @@ export class NewPixiv extends plugin {
async searchPid (e) {
let { sese, sesepro } = Config.getGroup(e.group_id)
if (!sese && !sesepro && !e.isMaster) return e.reply(SWITCH_ERROR)
e.reply(Pixiv.startMsg)
let regRet = pidReg.exec(e.msg)
let res = await Pixiv.illust(regRet[1], !e.isMaster && !setu.getR18(e.group_id))
if (res?.error) return e.reply(res.error)
let res = await Pixiv.illust(regRet[1], !e.isMaster && !setu.getR18(e.group_id)).catch(err => { e.reply(err) })
if (!res) return
let { msg, img } = res
await e.reply(msg)
img.length == 1 ? common.recallsendMsg(e, img) : common.getRecallsendMsg(e, img, false)
}
@@ -99,17 +92,13 @@ export class NewPixiv extends plugin {
if (((!sese && !sesepro) || (regRet[4] && !setu.getR18(e.group_id))) && !e.isMaster) {
return e.reply(SWITCH_ERROR)
}
e.reply(Pixiv.startMsg)
let page = common.translateChinaNum(regRet[6])
let res = await Pixiv.Rank(page, regRet[2], regRet[3], regRet[4])
if (res?.error) return e.reply(res.error)
let res = await Pixiv.Rank(page, regRet[2], regRet[3], regRet[4]).catch(err => { e.reply(err) })
if (!res) return
common.getRecallsendMsg(e, res)
return true
}
/** 关键词搜图 */
@@ -123,8 +112,8 @@ export class NewPixiv extends plugin {
let page = common.translateChinaNum(regRet[4])
let res = await Pixiv[`searchTags${regRet[1] ? 'pro' : ''}`](regRet[2], page, !setu.getR18(e.group_id))
if (res?.error) return e.reply(res.error)
.catch(err => { e.reply(err) })
if (!res) return
common.getRecallsendMsg(e, res)
}
@@ -135,10 +124,8 @@ export class NewPixiv extends plugin {
e.reply(Pixiv.startMsg)
let res = await Pixiv.PopularTags()
if (res?.error) return e.reply(res.error)
let res = await Pixiv.PopularTags().catch(err => { e.reply(err) })
if (!res) return
common.getRecallsendMsg(e, res)
}
@@ -152,10 +139,8 @@ export class NewPixiv extends plugin {
let regRet = uidReg.exec(e.msg)
let page = common.translateChinaNum(regRet[3])
let res = await Pixiv.userIllust(regRet[1], page, !setu.getR18(e.group_id))
if (res?.error) return e.reply(res.error)
let res = await Pixiv.userIllust(regRet[1], page, !setu.getR18(e.group_id)).catch(err => { e.reply(err) })
if (!res) return
common.getRecallsendMsg(e, res)
}
@@ -174,10 +159,8 @@ export class NewPixiv extends plugin {
num = 1
}
num = common.translateChinaNum(num)
let res = await Pixiv.randomImg(num)
if (res?.error) return e.reply(res.error)
let res = await Pixiv.randomImg(num).catch(err => { e.reply(err) })
if (!res) return
common.getRecallsendMsg(e, res)
}
@@ -189,8 +172,8 @@ export class NewPixiv extends plugin {
e.reply(Pixiv.startMsg)
let regRet = e.msg.match(/\d+/)
let res = await Pixiv.related(regRet[0], !setu.getR18(e.group_id))
if (res?.error) return e.reply(res.error)
let res = await Pixiv.related(regRet[0], !setu.getR18(e.group_id)).catch(err => { e.reply(err) })
if (!res) return
common.getRecallsendMsg(e, res)
}
@@ -201,8 +184,8 @@ export class NewPixiv extends plugin {
let { sese, sesepro } = Config.getGroup(e.group_id)
if (((!sese && !sesepro) || (!sesepro && ispro)) && !e.isMaster) return e.reply(SWITCH_ERROR)
let res = await Pixiv.pximg(ispro)
if (res?.error) return e.reply(res.error)
let res = await Pixiv.pximg(ispro).catch(err => { e.reply(err) })
if (!res) return
ispro ? common.getRecallsendMsg(e, [res]) : common.recallsendMsg(e, res)
}
@@ -214,10 +197,10 @@ export class NewPixiv extends plugin {
e.reply(Pixiv.startMsg)
let regRet = e.msg.match(searchUser)
let page = common.translateChinaNum(regRet[3])
let msg = await Pixiv.searchUser(regRet[1], page, !setu.getR18(e.group_id))
if (msg.error) return e.reply(msg.error)
let res = await Pixiv.searchUser(regRet[1], page, !setu.getR18(e.group_id)).catch(err => { e.reply(err) })
if (!res) return
common.getRecallsendMsg(e, msg)
common.getRecallsendMsg(e, res)
}
// 更换代理

View File

@@ -26,7 +26,11 @@ export default new class {
...options.headers
}
if (!options.agent)options.agent = await this.getAgent()
return fetch(url, options)
return await fetch(url, options).catch(err => {
logger.error(err)
const reason = err.message.match(/reason:(.*)/)
throw Error(`Request Get Errorreason${reason[1]}`)
})
}
async post (url, options = {}) {
@@ -40,7 +44,10 @@ export default new class {
options.headers['Content-Type'] = 'application/json'
}
if (!options.agent)options.agent = await this.getAgent()
return fetch(url, options)
return await fetch(url, options).catch(err => {
logger.error(err)
throw Error(`Request Post Errorreason${err.message.match(/reason:(.*)/)[1]}`)
})
}
async cfGet (url, options = {}) {

View File

@@ -1,9 +1,7 @@
import fetch from 'node-fetch'
import lodash from 'lodash'
import request from '../lib/request/request.js'
import { Config } from '../components/index.js'
/** API请求错误文案 */
const API_ERROR = '❎ 出错辣,请稍后重试'
export default new (class {
constructor () {
this.domain = 'http://api.liaobiao.top/api/bika'
@@ -43,12 +41,14 @@ export default new (class {
}
]
type = types.find(item => item.alias.includes(type))
let res = await fetch(type.url, this.hearder)
.then((res) => res.json())
.catch((err) => console.log(err))
if (!res) return { error: API_ERROR }
let res = await request.get(type.url, this.hearder)
.then(res => res.json())
.catch(err => {
logger.error(err)
throw Error(`bika search Errorreason${err.message.match(/reason:(.*)/)[1]}`)
})
let { docs, total, page: pg, pages } = res.data.comics
if (total == 0) return { error: `未找到作品,换个${type.alias[0]}试试吧` }
if (total == 0) throw Error(`未找到作品,换个${type.alias[0]}试试吧`)
return [
`共找到${total}个关于「${keyword}${type.alias[0]}的作品`,
`当前为第${pg}页,共${pages}`,
@@ -77,11 +77,13 @@ export default new (class {
* @return {*}
*/
async comicPage (id, page = 1, order = 1) {
let res = await fetch(`${this.domain}/comic_page?id=${id}&page=${page}&order=${order}`, this.hearder)
let res = await request.get(`${this.domain}/comic_page?id=${id}&page=${page}&order=${order}`, this.hearder)
.then((res) => res.json())
.catch((err) => console.log(err))
if (!res) return { error: API_ERROR }
if (res.error) return { error: res.message }
.catch(err => {
logger.error(err)
throw Error(`bika comicPage Errorreason${err.message.match(/reason:(.*)/)[1]}`)
})
if (res.error) throw Error(res.message)
let { docs, total, page: pg, pages } = res.data.pages
let { _id, title } = res.data.ep
return [
@@ -96,11 +98,13 @@ export default new (class {
let key = 'yenai:bika:categories'
let res = JSON.parse(await redis.get(key))
if (!res) {
res = await fetch(`${this.domain}/categories`, this.hearder)
res = await request.get(`${this.domain}/categories`, this.hearder)
.then((res) => res.json())
.catch((err) => console.log(err))
if (!res) return { error: API_ERROR }
if (res.error) return { error: res.message }
.catch(err => {
logger.error(err)
throw Error(`bika categories Errorreason${err.message.match(/reason:(.*)/)[1]}`)
})
if (res.error) throw Error(res.message)
res = res.data.categories.filter(item => !item.isWeb)
await redis.set(key, JSON.stringify(res), { EX: 43200 })
}
@@ -120,11 +124,13 @@ export default new (class {
* @return {*}
*/
async comicDetail (id) {
let res = await fetch(`${this.domain}/comic_detail?id=${id}`, this.hearder)
let res = await request.get(`${this.domain}/comic_detail?id=${id}`, this.hearder)
.then((res) => res.json())
.catch((err) => console.log(err))
if (!res) return { error: API_ERROR }
if (res.error) return { error: res.message }
.catch(err => {
logger.error(err)
throw Error(`bika comicDetail Errorreason${err.message.match(/reason:(.*)/)[1]}`)
})
if (res.error) throw Error(res.message)
let {
_id, title, description, author, chineseTeam, categories, tags, pagesCount, epsCount, finished, totalLikes, totalViews, totalComments, thumb
} = res.data.comic

View File

@@ -13,19 +13,19 @@ export default async function doSearch (url) {
try {
cheerio = await import('cheerio')
} catch (e) {
return { error: '未检测到依赖cheerio请安装后再使用Ascii2D搜图安装命令pnpm add cheerio -w 或 pnpm install -P' }
throw Error('未检测到依赖cheerio请安装后再使用Ascii2D搜图安装命令pnpm add cheerio -w 或 pnpm install -P')
}
}
const { ascii2dUsePuppeteer, ascii2dResultMaxQuantity } = Config.picSearch
const callApi = ascii2dUsePuppeteer ? callAscii2dUrlApiWithPuppeteer : callAscii2dUrlApi
let ret = await callApi(url)
if (!ret) return { error: 'Ascii2D搜图请求失败' }
if (!ret) throw Error('Ascii2D搜图请求失败')
const colorURL = ret.url
if (!colorURL.includes('/color/')) {
const $ = cheerio.load(ret.data, { decodeEntities: false })
console.error('[error] ascii2d url:', colorURL)
logger.error('[error] ascii2d url:', colorURL)
logger.debug(ret.data)
return { error: ($('.container > .row > div:first-child > p').text().trim()) }
throw Error(`搜索失败,错误原因:${$('.container > .row > div:first-child > p').text().trim()}`)
}
const bovwURL = colorURL.replace('/color/', '/bovw/')
let bovwDetail = await (ascii2dUsePuppeteer ? getAscii2dWithPuppeteer(bovwURL) : request.cfGet(bovwURL))
@@ -37,13 +37,13 @@ export default async function doSearch (url) {
}
let colorData = (await parse(ret.data)).slice(0, ascii2dResultMaxQuantity)
let bovwData = (await parse(bovwDetail.data)).slice(0, ascii2dResultMaxQuantity)
if (lodash.isEmpty(colorData)) return { error: 'Ascii2D数据获取失败' }
if (lodash.isEmpty(colorData)) throw Error('Ascii2D数据获取失败')
let mapfun = item => [
Config.picSearch.hideImg ? '' : segment.image(item.image),
`${item.info}\n`,
`标题:${item.source?.text}\n`,
`作者:${item.author?.text}(${item.author?.link})\n`,
`来源:(${item.source?.link})`
`来源:${item.source?.link}`
]
let color = colorData.map(mapfun)
let bovw = bovwData.map(mapfun)
@@ -59,8 +59,7 @@ const callAscii2dUrlApiWithPuppeteer = (imgUrl) => {
return getAscii2dWithPuppeteer(`${domain}/search/url/${imgUrl}`)
}
const callAscii2dUrlApi = async (imgUrl) => {
let res = await request.cfGet(`${domain}/search/url/${imgUrl}`).catch(err => console.error(err))
if (!res || !res.ok) return false
let res = await request.cfGet(`${domain}/search/url/${imgUrl}`)
return {
url: res.url,
data: await res.text()

View File

@@ -4,11 +4,10 @@ import sagiri from '../../tools/sagiri.js'
import request from '../../lib/request/request.js'
export default async function doSearch (url) {
let res = await getSearchResult(url)
logger.debug(`SauceNAO result: ${res}`)
if (!res) return { error: 'SauceNAO搜图网络请求失败移动网络无法访问SauceNAO可尝试配置代理' }
if (res.header.status != 0) return { error: 'SauceNAO搜图错误信息' + res.header.message?.replace(/<.*?>/g, '') }
logger.debug('SauceNAO result:', res)
if (res.header.status != 0) throw Error('SauceNAO搜图,错误信息:' + res.header.message?.replace(/<.*?>/g, ''))
let format = sagiri(res)
if (lodash.isEmpty(format)) return { error: 'SauceNAO搜图无数据' }
if (lodash.isEmpty(format)) throw Error('SauceNAO搜图无数据')
let msgMap = async item => [
`SauceNAO (${item.similarity}%)\n`,
@@ -18,9 +17,6 @@ export default async function doSearch (url) {
`来源:${item.url.toString()}`
]
let maxSimilarity = format[0].similarity
if (maxSimilarity < Config.picSearch.SauceNAOMinSim) {
return { error: `SauceNAO 相似度 ${maxSimilarity}% 过低` }
}
let filterSimilarity = format.filter(item => item.similarity > 80)
let message = []
if (!lodash.isEmpty(filterSimilarity)) {
@@ -56,5 +52,11 @@ async function getSearchResult (imgURL, db = 999) {
url: imgURL,
hide: Config.picSearch.hideImgWhenSaucenaoNSFW
}
}).then(res => res.json()).catch(err => console.error(err))
}).then(res => {
if (res.status === 429) {
return { error: 'SauceNAO搜图 搜索次数已达单位时间上限,请稍候再试' }
} else {
return res.json()
}
})
}

View File

@@ -6,7 +6,6 @@ import { rankType, MSG } from '../tools/pixiv.js'
import request from '../lib/request/request.js'
import { Config } from '../components/index.js'
/** API请求错误文案 */
const API_ERROR = '❎ 出错辣,请稍后重试'
export default new class Pixiv {
constructor () {
@@ -41,13 +40,10 @@ export default new class Pixiv {
* @return {Object}
*/
async illust (ids, filter = false) {
let api = `${this.domain}/illust?id=${ids}`
let res = await fetch(api).then(res => res.json()).catch(err => console.log(err))
if (!res) return { error: API_ERROR }
const parame = { id: ids }
let res = await request.get(`${this.domain}/illust`, { parame }).then(res => res.json())
if (res.error) throw Error(res.error?.user_message || '无法获取数据')
if (res.error) {
return { error: res.error?.user_message || '无法获取数据' }
}
let illust = this.format(res.illust)
let { id, title, user, tags, total_bookmarks, total_view, url, create_date, x_restrict, illust_ai_type } = illust
let msg = [
@@ -64,16 +60,13 @@ export default new class Pixiv {
`传送门https://www.pixiv.net/artworks/${id}`
]
if (filter && x_restrict) {
let linkmsg = [
'该作品不适合所有年龄段,请自行使用链接查看:'
]
let linkmsg = ['该作品不适合所有年龄段,请自行使用链接查看:']
if (url.length > 1) {
linkmsg.push(...url.map((item, index) => `\nhttps://pixiv.re/${id}-${index + 1}.jpg`))
linkmsg.push(...url.map((item, index) => `https://pixiv.re/${id}-${index + 1}.jpg`))
} else {
linkmsg.push(`\nhttps://pixiv.re/${id}.jpg`)
linkmsg.push(`https://pixiv.re/${id}.jpg`)
}
return { error: linkmsg }
throw Error(linkmsg.join('\n'))
}
let img = await Promise.all(url.map(async item => await this.requestPixivImg(item)))
return { msg, img }
@@ -97,30 +90,31 @@ export default new class Pixiv {
// r18处理
if (r18) {
let R18 = this.ranktype[mode].r18
if (!R18) return { error: '该排行没有不适合所有年龄段的分类哦~' }
if (!R18) throw Error('该排行没有不适合所有年龄段的分类哦~')
type = R18.type
pageSizeAll = R18.total
}
// 总页数
let pageAll = Math.ceil(pageSizeAll / 30)
if (page > pageAll) {
return { error: '哪有那么多图片给你辣(•̀へ •́ ╮ )' }
}
if (page > pageAll) throw Error('哪有那么多图片给你辣(•̀へ •́ ╮ )')
if (!date) date = moment().subtract(moment().utcOffset(9).hour() >= 12 ? 1 : 2, 'days').format('YYYY-MM-DD')
let parame = `mode=${type}&page=${page}&date=${date}`
let parame = {
mode: type,
page,
date
}
// 请求api
let api = `${this.domain}/rank?${parame}`
let res = await fetch(api).then(res => res.json()).catch(err => console.log(err))
let api = `${this.domain}/rank`
let res = await request.get(api, { parame }).then(res => res.json()).catch(err => console.log(err))
if (!res || res.error || lodash.isEmpty(res.illusts)) {
logger.mark('[椰奶Pixiv][排行榜]使用备用接口')
res = await fetch(`https://api.obfs.dev/api/pixiv/rank?${parame}`).then(res => res.json())
.catch(err => console.log(err))
};
if (!res) return { error: API_ERROR }
if (res.error) return { error: res.error.message }
if (lodash.isEmpty(res.illusts)) return { error: '暂无数据,请等待榜单更新哦(。-ω-)zzz' }
res = await request.get('https://api.obfs.dev/api/pixiv/rank', { parame }).then(res => res.json())
}
if (res.error) throw Error(res.error.message)
if (lodash.isEmpty(res.illusts)) throw Error('暂无数据,请等待榜单更新哦(。-ω-)zzz')
let illusts = await Promise.all(res.illusts.map(async (item, index) => {
let list = this.format(item)
@@ -163,17 +157,12 @@ export default new class Pixiv {
*/
async searchTags (tag, page = 1) {
let api = `https://www.vilipix.com/api/v1/picture/public?limit=30&tags=${tag}&sort=new&offset=${(page - 1) * 30}`
let res = await fetch(api).then(res => res.json()).catch(err => console.log(err))
if (!res) return { error: API_ERROR }
if (res.data.count == 0) {
return { error: '呜呜呜,人家没有找到相关的插画(ó﹏ò。)' }
}
let res = await request.get(api).then(res => res.json())
if (res.data.count == 0) throw Error('呜呜呜,人家没有找到相关的插画(ó﹏ò。)')
let pageall = Math.ceil(res.data.count / 30)
if (page > pageall) {
return { error: '啊啊啊淫家给不了你那么多辣d(ŐдŐ๑)' }
}
if (page > pageall) throw Error('啊啊啊淫家给不了你那么多辣d(ŐдŐ๑)')
let list = [
`当前为第${page}页,共${pageall}页,本页共${res.data.rows.length}张,总共${res.data.count}`
@@ -201,11 +190,14 @@ export default new class Pixiv {
* @return {*}
*/
async searchTagspro (tag, page = 1, isfilter = true) {
let api = `${this.domain}/search?word=${tag}&page=${page}&order=popular_desc`
let res = await fetch(api).then(res => res.json()).catch(err => console.log(err))
if (!res) return { error: API_ERROR }
if (res.error) return { error: res.error.message }
if (lodash.isEmpty(res.illusts)) return { error: '宝~没有数据了哦(๑>︶<)و' }
const parame = {
word: tag,
page,
order: 'popular_desc'
}
let res = await request.get(`${this.domain}/search`, { parame }).then(res => res.json())
if (res.error) throw Error(res.error.message)
if (lodash.isEmpty(res.illusts)) throw Error('宝~没有数据了哦(๑>︶<)و')
let illusts = []
let filter = 0
@@ -226,7 +218,7 @@ export default new class Pixiv {
await this.requestPixivImg(image_urls.large)
])
}
if (lodash.isEmpty(illusts)) return { error: '该页全为涩涩内容已全部过滤(#/。\#)' }
if (lodash.isEmpty(illusts)) throw Error('该页全为涩涩内容已全部过滤(#/。\#)')
return [
`本页共${NowNum}${filter ? `,过滤${filter}` : ''}\n可尝试使用 "#tagpro搜图${tag}${page - 0 + 1}页" 翻页\n无数据则代表无下一页`,
@@ -241,9 +233,8 @@ export default new class Pixiv {
async PopularTags () {
let api = `${this.domain}/tags`
let res = await fetch(api).then(res => res.json()).catch(err => console.log(err))
if (!res) return { error: API_ERROR }
if (!res.trend_tags) return { error: '呜呜呜,没有获取到数据(๑ १д१)' }
let res = await fetch(api).then(res => res.json())
if (!res.trend_tags) throw Error('呜呜呜,没有获取到数据(๑ १д१)')
let list = []
for (let i of res.trend_tags) {
@@ -272,20 +263,18 @@ export default new class Pixiv {
// 关键词搜索
if (!/^\d+$/.test(keyword)) {
let wordapi = `${this.domain}/search_user?word=${keyword}`
let wordlist = await fetch(wordapi).then(res => res.json()).catch(err => console.log(err))
if (!wordlist) return { error: API_ERROR }
if (lodash.isEmpty(wordlist.user_previews)) return { error: '呜呜呜人家没有找到这个淫d(ŐдŐ๑)' }
let wordlist = await request.get(wordapi).then(res => res.json())
if (lodash.isEmpty(wordlist.user_previews)) throw Error('呜呜呜人家没有找到这个淫d(ŐдŐ๑)')
keyword = wordlist.user_previews[0].user.id
}
// 作品
let api = `${this.domain}/member_illust?id=${keyword}&page=${page}`
let res = await fetch(api).then(res => res.json()).catch(err => console.log(err))
if (!res) return { error: API_ERROR }
if (res.error) return { error: res.error.message }
let res = await request.get(api).then(res => res.json())
if (res.error) throw Error(res.error.message)
// 没有作品直接返回信息
if (lodash.isEmpty(res.illusts)) return { error: page >= 2 ? '这一页没有作品辣(>人<;)' : 'Σ(っ °Д °;)っ这个淫居然没有作品' }
if (lodash.isEmpty(res.illusts)) throw Error(page >= 2 ? '这一页没有作品辣(>人<;)' : 'Σ(っ °Д °;)っ这个淫居然没有作品')
let illusts = []
let filter = 0
@@ -305,7 +294,7 @@ export default new class Pixiv {
await this.requestPixivImg(url[0])
])
}
if (lodash.isEmpty(illusts)) return { error: '该页全为涩涩内容已全部过滤(#/。\#)' }
if (lodash.isEmpty(illusts)) throw Error('该页全为涩涩内容已全部过滤(#/。\#)')
let { id: uid, name, profile_image_urls } = res.user
return [
[
@@ -327,10 +316,9 @@ export default new class Pixiv {
*/
async searchUser (word, page = 1, isfilter = true) {
let api = `${this.domain}/search_user?word=${word}&page=${page}&size=10`
let user = await fetch(api).then(res => res.json()).catch(err => console.log(err))
if (!user) return { error: API_ERROR }
if (user.error) return { error: user.error.message }
if (lodash.isEmpty(user.user_previews)) return { error: '呜呜呜人家没有找到这个淫d(ŐдŐ๑)' }
let user = await request.get(api).then(res => res.json())
if (user.error) throw Error(user.error.message)
if (lodash.isEmpty(user.user_previews)) throw Error('呜呜呜人家没有找到这个淫d(ŐдŐ๑)')
let msg = await Promise.all(user.user_previews.slice(0, 10).map(async (item, index) => {
let { id, name, profile_image_urls } = item.user
@@ -359,9 +347,8 @@ export default new class Pixiv {
*/
async randomImg (limit) {
let api = `https://www.vilipix.com/api/v1/picture/recommand?limit=${limit}&offset=${lodash.random(1, 700)}`
let res = await fetch(api).then(res => res.json()).catch(err => console.log(err))
if (!res) return { error: API_ERROR }
if (!res.data || !res.data.rows) return { error: '呜呜呜,没拿到瑟瑟的图片(˃ ⌑ ˂ഃ )' }
let res = await request.get(api).then(res => res.json())
if (!res.data || !res.data.rows) throw Error('呜呜呜,没拿到瑟瑟的图片(˃ ⌑ ˂ഃ )')
let list = []
for (let i of res.data.rows) {
@@ -384,10 +371,9 @@ export default new class Pixiv {
*/
async related (pid, isfilter = true) {
let api = `${this.domain}/related?id=${pid}`
let res = await fetch(api).then(res => res.json()).catch(err => console.log(err))
if (!res) return { error: API_ERROR }
if (res.error) return { error: res.error.user_message }
if (lodash.isEmpty(res.illusts)) return { error: '呃...没有数据(•ิ_•ิ)' }
let res = await request.get(api).then(res => res.json())
if (res.error) throw Error(res.error.user_message)
if (lodash.isEmpty(res.illusts)) throw Error('呃...没有数据(•ิ_•ิ)')
let illusts = []
let filter = 0
@@ -407,7 +393,7 @@ export default new class Pixiv {
await this.requestPixivImg(image_urls.large)
])
}
if (lodash.isEmpty(illusts)) return { error: '啊啊啊!!!居然全是瑟瑟哒不给你看(*/ω\*)' }
if (lodash.isEmpty(illusts)) throw Error('啊啊啊!!!居然全是瑟瑟哒不给你看(*/ω\*)')
return [
`Pid:${pid}的相关作品,共${res.illusts.length}${filter ? `,过滤${filter}` : ''}`,
@@ -421,8 +407,7 @@ export default new class Pixiv {
if (type) {
url = 'https://xiaobapi.top/api/xb/api/setu.php'
}
let res = await fetch(url).then(res => res.json()).catch(err => console.log(err))
if (!res) return { error: API_ERROR }
let res = await request.get(url).then(res => res.json())
let { pid, uid, title, author, tags, urls, r18 } = res.data[0] || res.data
urls = urls.original.replace(/i.der.ink|i.pixiv.re/, this.proxy)
let msg = [
@@ -443,7 +428,6 @@ export default new class Pixiv {
*/
async requestPixivImg (url) {
url = url.replace('i.pximg.net', this.proxy)
console.log(this.proxy)
logger.debug(`pixiv getImg URL: ${url}`)
return request.proxyRequestImg(url, { headers: this.headers })
}

View File

@@ -2,6 +2,7 @@ import child_process from 'child_process'
import common from '../../../lib/common/common.js'
import Config from '../components/Config.js'
import setu from './setu.js'
import _ from 'lodash'
export default new class newCommon {
/**
* @description: 延时函数
@@ -75,6 +76,7 @@ export default new class newCommon {
*/
async getforwardMsg (e, message, { recallMsg = 0, isBot = true, fkmsg = '', isxml = false, oneMsg = false } = {}) {
let forwardMsg = []
if (_.isEmpty(message)) throw Error('[椰奶sendforwardMsg][Error]发送的转发消息不能为空')
let add = (msg) => forwardMsg.push(
{
message: msg,