Miao-Yunzai/plugins/genshin/model/mysNews.js

367 lines
10 KiB
JavaScript
Raw Normal View History

2023-03-04 14:30:13 +08:00
import base from './base.js'
import fetch from 'node-fetch'
import lodash from 'lodash'
import puppeteer from '../../../lib/puppeteer/puppeteer.js'
import common from '../../../lib/common/common.js'
import gsCfg from '../model/gsCfg.js'
let emoticon
export default class MysNews extends base {
2023-07-30 11:05:48 +08:00
constructor(e) {
2023-03-04 14:30:13 +08:00
super(e)
this.model = 'mysNews'
}
2023-07-30 11:05:48 +08:00
async getNews(gid) {
2023-03-04 14:30:13 +08:00
let type = 1
let typeName = '公告'
if (this.e.msg.includes('资讯')) {
type = '3'
typeName = '资讯'
}
if (this.e.msg.includes('活动')) {
type = '2'
typeName = '活动'
}
2023-07-30 11:05:48 +08:00
const res = await this.postData('getNewsList', { gids: gid, page_size: 20, type })
2023-03-04 14:30:13 +08:00
if (!res) return
const data = res.data.list
if (data.length == 0) {
return true
}
2023-07-30 11:05:48 +08:00
const page = this.e.msg.replace(/#||官方|星铁|原神|崩坏三|崩三|绝区零|崩坏二|崩二|崩坏学园二|未定|未定事件簿|公告|资讯|活动/g, '').trim() || 1
2023-03-04 14:30:13 +08:00
if (page > data.length) {
await this.e.reply('目前只查前20条最新的公告请输入1-20之间的整数。')
return true
}
const postId = data[page - 1].post.post_id
2023-07-30 11:05:48 +08:00
const param = await this.newsDetail(postId, gid)
2023-03-04 14:30:13 +08:00
const img = await this.render(param)
2023-07-30 11:05:48 +08:00
let game = this.game()
return this.replyMsg(img, `${game}${typeName}${param.data.post.subject}`)
2023-03-04 14:30:13 +08:00
}
render(param) {
return puppeteer.screenshots(this.model, param)
}
2023-03-04 14:30:13 +08:00
2023-07-30 11:05:48 +08:00
async newsDetail(postId, gid) {
const res = await this.postData('getPostFull', { gids: gid, read: 1, post_id: postId })
2023-03-04 14:30:13 +08:00
if (!res) return
2023-07-30 11:05:48 +08:00
const data = await this.detalData(res.data.post, gid)
2023-03-04 14:30:13 +08:00
return {
...this.screenData,
saveId: postId,
dataConent: data.post.content,
data
}
}
2023-07-30 11:05:48 +08:00
postApi(type, data) {
let host = 'https://bbs-api.miyoushe.com/'
2023-03-04 14:30:13 +08:00
let param = []
lodash.forEach(data, (v, i) => param.push(`${i}=${v}`))
param = param.join('&')
switch (type) {
// 搜索
case 'searchPosts':
2023-07-30 11:05:48 +08:00
host = 'https://bbs-api.miyoushe.com/post/wapi/searchPosts?'
2023-03-04 14:30:13 +08:00
break
// 帖子详情
case 'getPostFull':
host += 'post/wapi/getPostFull?'
break
// 公告列表
case 'getNewsList':
host += 'post/wapi/getNewsList?'
break
case 'emoticon':
host += 'misc/api/emoticon_set?'
break
}
return host + param
}
2023-07-30 11:05:48 +08:00
async postData(type, data) {
2023-03-04 14:30:13 +08:00
const url = this.postApi(type, data)
const headers = {
2023-07-30 11:05:48 +08:00
Referer: 'https://www.miyoushe.com',
2023-03-04 14:30:13 +08:00
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
}
let response
try {
response = await fetch(url, { method: 'get', headers })
} catch (error) {
logger.error(error.toString())
return false
}
if (!response.ok) {
logger.error(`[米游社接口错误][${type}] ${response.status} ${response.statusText}`)
return false
}
const res = await response.json()
return res
}
2023-07-30 11:05:48 +08:00
async detalData(data, gid) {
2023-03-04 14:30:13 +08:00
let json
try {
json = JSON.parse(data.post.content)
} catch (error) {
}
if (typeof json == 'object') {
if (json.imgs && json.imgs.length > 0) {
for (const val of json.imgs) {
data.post.content = ` <div class="ql-image-box"><img src="${val}?x-oss-process=image//resize,s_600/quality,q_80/auto-orient,0/interlace,1/format,png"></div>`
}
}
} else {
for (const img of data.post.images) {
data.post.content = data.post.content.replace(img, img + '?x-oss-process=image//resize,s_600/quality,q_80/auto-orient,0/interlace,1/format,jpg')
}
if (!emoticon) {
2023-07-30 11:05:48 +08:00
emoticon = await this.mysEmoticon(gid)
2023-03-04 14:30:13 +08:00
}
data.post.content = data.post.content.replace(/_\([^)]*\)/g, function (t, e) {
t = t.replace(/_\(|\)/g, '')
if (emoticon.has(t)) {
return `<img class="emoticon-image" src="${emoticon.get(t)}"/>`
} else {
return ''
}
})
const arrEntities = { lt: '<', gt: '>', nbsp: ' ', amp: '&', quot: '"' }
data.post.content = data.post.content.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) {
return arrEntities[t]
})
}
data.post.created_time = new Date(data.post.created_at * 1000).toLocaleString()
for (const i in data.stat) {
data.stat[i] = data.stat[i] > 10000 ? (data.stat[i] / 10000).toFixed(2) + '万' : data.stat[i]
}
return data
}
2023-07-30 11:05:48 +08:00
async mysEmoticon(gid) {
2023-03-04 14:30:13 +08:00
const emp = new Map()
2023-07-30 11:05:48 +08:00
const res = await this.postData('emoticon', { gids: gid })
2023-03-04 14:30:13 +08:00
if (res.retcode != 0) {
return emp
}
for (const val of res.data.list) {
if (!val.icon) continue
for (const list of val.list) {
if (!list.icon) continue
emp.set(list.name, list.icon)
}
}
return emp
}
2023-07-30 11:05:48 +08:00
async mysSearch() {
2023-03-04 14:30:13 +08:00
let msg = this.e.msg
msg = msg.replace(/#|米游社|mys/g, '')
if (!msg) {
await this.e.reply('请输入关键字,如#米游社七七')
return false
}
let page = msg.match(/.*(\d){1}$/) || 0
if (page && page[1]) {
page = page[1]
}
msg = lodash.trim(msg, page)
let res = await this.postData('searchPosts', { gids: 2, size: 20, keyword: msg })
if (!res) return
if (res?.data?.posts.length <= 0) {
await this.e.reply('搜索不到您要的结果,换个关键词试试呗~')
return false
}
let postId = res.data.posts[page].post.post_id
const param = await this.newsDetail(postId)
const img = await this.render(param)
2023-03-04 14:30:13 +08:00
return this.replyMsg(img, `${param.data.post.subject}`)
2023-03-04 14:30:13 +08:00
}
2023-07-30 11:05:48 +08:00
async mysUrl() {
2023-03-04 14:30:13 +08:00
let msg = this.e.msg
let postId = /[0-9]+/g.exec(msg)[0]
if (!postId) return false
const param = await this.newsDetail(postId)
const img = await this.render(param)
2023-03-04 14:30:13 +08:00
return this.replyMsg(img, `${param.data.post.subject}`)
2023-03-04 14:30:13 +08:00
}
2023-07-30 11:05:48 +08:00
async ysEstimate() {
2023-03-04 14:30:13 +08:00
let msg = '版本原石盘点'
let res = await this.postData('searchPosts', { gids: 2, size: 20, keyword: msg })
if (res?.data?.posts.length <= 0) {
await this.e.reply('暂无数据')
return false
}
let postId = ''
for (let post of res.data.posts) {
if (post.user.uid == '218945821') {
postId = post.post.post_id
break
}
}
if (!postId) {
await this.e.reply('暂无数据')
return false
}
const param = await this.newsDetail(postId)
const img = await this.render(param)
2023-03-04 14:30:13 +08:00
if (img.length > 1) {
img.push(segment.image(param.data.post.images[0] + '?x-oss-process=image//resize,s_600/quality,q_80/auto-orient,0/interlace,1/format,jpg'))
}
return this.replyMsg(img, `${param.data.post.subject}`)
2023-03-04 14:30:13 +08:00
}
replyMsg(img, title) {
2023-03-04 14:30:13 +08:00
if (!img || img.length <= 0) return false
if (img.length == 1) {
if (title) return [title, ...img]
return img
2023-03-04 14:30:13 +08:00
}
return common.makeForwardMsg(this.e, img, title)
2023-03-04 14:30:13 +08:00
}
2023-07-30 11:05:48 +08:00
async mysNewsTask() {
2023-03-04 14:30:13 +08:00
let cfg = gsCfg.getConfig('mys', 'pushNews')
// 推送2小时内的公告资讯
let interval = 7200
// 最多同时推送两条
this.maxNum = cfg.maxNum
2023-07-30 11:05:48 +08:00
for (let gid of [1, 2, 3, 4, 6, 8]) {
let type = gid == 1 ? 'bbb' : gid == 2 ? 'gs' : gid == 3 ? 'bb' : gid == 4 ? 'wd' : gid == 6 ? 'sr' : 'zzz'
2023-03-04 14:30:13 +08:00
2023-07-30 11:05:48 +08:00
let news = []
if (!lodash.isEmpty(cfg[`${type}announceGroup`])) {
let anno = await this.postData('getNewsList', { gids: gid, page_size: 10, type: 1 })
if (anno) anno.data.list.forEach(v => { news.push({ ...v, typeName: '公告', post_id: v.post.post_id }) })
2023-03-04 14:30:13 +08:00
}
2023-07-30 11:05:48 +08:00
if (!lodash.isEmpty(cfg[`${type}infoGroup`])) {
let info = await this.postData('getNewsList', { gids: gid, page_size: 10, type: 3 })
if (info) info.data.list.forEach(v => { news.push({ ...v, typeName: '资讯', post_id: v.post.post_id }) })
2023-03-04 14:30:13 +08:00
}
2023-07-30 11:05:48 +08:00
if (news.length <= 0) continue
news = lodash.orderBy(news, ['post_id'], ['asc'])
let now = Date.now() / 1000
this.key = `Yz:${type}:mys:newPush:`
this.e.isGroup = true
this.pushGroup = []
for (let val of news) {
if (Number(now - val.post.created_at) > interval)
2023-07-30 11:05:48 +08:00
continue
if (cfg.banWord[type] && new RegExp(cfg.banWord[type]).test(val.post.subject))
2023-07-30 11:05:48 +08:00
continue
if (val.typeName == '公告')
for (let botId in cfg[`${type}announceGroup`])
for (let groupId of cfg[`${type}announceGroup`][botId])
2023-07-30 11:05:48 +08:00
await this.sendNews(botId, groupId, val.typeName, val.post.post_id, gid)
if (val.typeName == '资讯')
for (let botId in cfg[`${type}infoGroup`])
for (let groupId of cfg[`${type}infoGroup`][botId])
2023-07-30 11:05:48 +08:00
await this.sendNews(botId, groupId, val.typeName, val.post.post_id, gid)
2023-03-04 14:30:13 +08:00
}
}
}
2023-07-30 11:05:48 +08:00
async sendNews(botId, groupId, typeName, postId, gid) {
2023-03-04 14:30:13 +08:00
if (!this.pushGroup[groupId]) this.pushGroup[groupId] = 0
if (this.pushGroup[groupId] >= this.maxNum) return
2023-07-30 11:05:48 +08:00
let sended = await redis.get(`${this.key}${botId}:${groupId}:${postId}`)
2023-03-04 14:30:13 +08:00
if (sended) return
2023-07-30 11:05:48 +08:00
let game = this.game(gid)
// 判断是否存在群关系
2023-07-30 11:05:48 +08:00
this.e.group = Bot[botId]?.pickGroup(groupId)
if (!this.e.group) {
logger.mark(`[米游社${game}${typeName}推送] 群${botId}:${groupId}未关联`)
return
}
2023-03-04 14:30:13 +08:00
if (!this[postId]) {
2023-07-30 11:05:48 +08:00
const param = await this.newsDetail(postId, gid)
2023-03-04 14:30:13 +08:00
2023-07-30 11:05:48 +08:00
logger.mark(`[米游社${game}${typeName}推送] ${param.data.post.subject}`)
2023-03-04 14:30:13 +08:00
this[postId] = {
img: await this.render(param),
2023-03-04 14:30:13 +08:00
title: param.data.post.subject
}
}
this.pushGroup[groupId]++
2023-07-30 11:05:48 +08:00
await redis.set(`${this.key}${botId}:${groupId}:${postId}`, '1', { EX: 3600 * 10 })
// 随机延迟10-90秒
await common.sleep(lodash.random(10000, 90000))
const msg = await this.replyMsg(this[postId].img, `${game}${typeName}推送:${this[postId].title}`)
return this.e.group.sendMsg(msg)
2023-03-04 14:30:13 +08:00
}
2023-07-30 11:05:48 +08:00
game(gid) {
switch (gid) {
case 1:
return '崩坏三'
case 2:
return '原神'
case 3:
return '崩坏二'
case 4:
return '未定事件簿'
case 6:
return '崩坏星穹铁道'
case 8:
return '绝区零'
}
return ''
}
}