Skip to content

Commit

Permalink
optimize: TextDynamic's img sending & update: dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
snowtafir committed Dec 6, 2024
1 parent ef9eaea commit ac9f46f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 62 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.0.6
* 优化文字动态图片资源的发送
* 依赖升级

# 2.0.5
* 优化ck
* 新增 User-Agent 配置项
Expand Down
46 changes: 27 additions & 19 deletions models/bilibili/bilibili.main.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,15 @@ export class BiliQuery {
* @param setData - 设置数据
* @returns 生成的动态消息文字内容
*/
static async formatTextDynamicData(upName: string, data: any, isForward: boolean, setData: any): Promise<any> {
static async formatTextDynamicData(upName: string, data: any, isForward: boolean, setData: any) {
const BiliDrawDynamicLinkUrl = 'https://m.bilibili.com/dynamic/';
let desc: any, msg: any, pics: any, author: any, majorType: any, content: any, dynamicTitle: any;
let desc: any,
msg: any[] = [],
pics: any[] = [],
author: any,
majorType: any,
content: any,
dynamicTitle: any;
let title = `B站【${upName}】动态推送:\n`;

switch (data.type) {
Expand All @@ -421,11 +427,12 @@ export class BiliQuery {
`标题:${desc.title}\n`,
`${desc.desc}\n`,
`链接:${this.formatUrl(desc.jump_url)}\n`,
`时间:${author ? moment(author.pub_ts * 1000).format('YYYY年MM月DD日 HH:mm:ss') : ''}\n`,
segment.image(desc?.cover)
`时间:${author ? moment(author.pub_ts * 1000).format('YYYY年MM月DD日 HH:mm:ss') : ''}`
];

return msg;
pics = [segment.image(desc?.cover)];

return { msg, pics };

case 'DYNAMIC_TYPE_WORD':
// 处理文字动态
Expand Down Expand Up @@ -457,7 +464,7 @@ export class BiliQuery {
`时间:${author ? moment(author.pub_ts * 1000).format('YYYY年MM月DD日 HH:mm:ss') : ''}`
];

return msg;
return { msg, pics };

case 'DYNAMIC_TYPE_DRAW':
// 处理图文动态
Expand Down Expand Up @@ -504,11 +511,10 @@ export class BiliQuery {
`-----------------------------\n`,
`${this.dynamicContentLimit(content, setData)}\n`,
`链接:${BiliDrawDynamicLinkUrl}${data.id_str}\n`,
`时间:${author ? moment(author.pub_ts * 1000).format('YYYY年MM月DD日 HH:mm:ss') : ''}\n`,
...pics
`时间:${author ? moment(author.pub_ts * 1000).format('YYYY年MM月DD日 HH:mm:ss') : ''}`
];

return msg;
return { msg, pics };

case 'DYNAMIC_TYPE_ARTICLE':
// 处理文章动态
Expand Down Expand Up @@ -550,11 +556,10 @@ export class BiliQuery {
`-----------------------------\n`,
`标题:${dynamicTitle}\n`,
`链接:${this.formatUrl(desc.jump_url)}\n`,
`时间:${author ? moment(author.pub_ts * 1000).format('YYYY年MM月DD日 HH:mm:ss') : ''}\n`,
...pics
`时间:${author ? moment(author.pub_ts * 1000).format('YYYY年MM月DD日 HH:mm:ss') : ''}`
];

return msg;
return { msg, pics };

case 'DYNAMIC_TYPE_FORWARD':
// 处理转发动态
Expand All @@ -568,10 +573,12 @@ export class BiliQuery {

isForward = true;
let orig = await this.formatTextDynamicData(upName, data.orig, isForward, setData);
if (orig && orig.length) {
orig = orig.slice(2);
let origContent = [];
if (orig && typeof orig === 'object') {
origContent = orig.msg.slice(2);
pics = orig.pics;
} else {
return false;
return 'continue';
}

title = `B站【${upName}】转发动态推送:\n`;
Expand All @@ -582,10 +589,10 @@ export class BiliQuery {
`链接:${BiliDrawDynamicLinkUrl}${data.id_str}\n`,
`时间:${author ? moment(author.pub_ts * 1000).format('YYYY年MM月DD日 HH:mm:ss') : ''}\n`,
'\n---以下为转发内容---\n',
...orig
...origContent
];

return msg;
return { msg, pics };

case 'DYNAMIC_TYPE_LIVE_RCMD':
// 处理直播动态
Expand All @@ -595,9 +602,10 @@ export class BiliQuery {
desc = desc?.live_play_info;
if (!desc) return;
title = `B站【${upName}】直播动态推送:\n`;
msg = [title, `-----------------------------\n`, `标题:${desc.title}\n`, `链接:https:${desc.link}\n`, segment.image(desc.cover)];
msg = [title, `-----------------------------\n`, `标题:${desc.title}\n`, `链接:https:${desc.link}`];

return msg;
pics = [segment.image(desc.cover)];
return { msg, pics };

default:
// 处理未定义的动态类型
Expand Down
15 changes: 11 additions & 4 deletions models/bilibili/bilibili.main.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class BiliTask {
let imgs: Buffer[] | null = await this.renderDynamicCard(uid, renderData, ScreenshotOptionsData);
if (!imgs) return;

redis.set(`${markKey}${chatId}:${id_str}`, '1', { EX: 3600 * 10 }); // 设置已发送标记
redis.set(`${markKey}${chatId}:${id_str}`, '1', { EX: 3600 * 72 }); // 设置已发送标记

(logger ?? Bot.logger)?.mark('优纪插件:B站动态执行推送');

Expand All @@ -246,20 +246,27 @@ export class BiliTask {
} else {
const dynamicMsg = await BiliQuery.formatTextDynamicData(upName, pushDynamicData, false, biliConfigData); // 构建图文动态消息

redis.set(`${markKey}${chatId}:${id_str}`, '1', { EX: 3600 * 10 }); // 设置已发送标记
redis.set(`${markKey}${chatId}:${id_str}`, '1', { EX: 3600 * 72 }); // 设置已发送标记

if (dynamicMsg == 'continue') {
return 'return'; // 如果动态消息构建失败,则直接返回
}

if (biliConfigData.banWords.length > 0) {
const banWords = new RegExp(biliConfigData.banWords.join('|'), 'g'); // 构建屏蔽关键字正则表达式
if (banWords.test(dynamicMsg.join(''))) {
if (banWords.test(dynamicMsg.msg.join(''))) {
return 'return'; // 如果动态消息包含屏蔽关键字,则直接返回
}
}

await this.sendMessage(chatId, bot_id, chatType, dynamicMsg);
await this.sendMessage(chatId, bot_id, chatType, dynamicMsg.msg);
const pics = dynamicMsg.pics;
if (pics && pics.length > 0) {
for (let i = 0; i < pics.length; i++) {
await this.sendMessage(chatId, bot_id, chatType, pics[i]);
await this.randomDelay(1000, 2000); // 随机延时1-2秒
}
}
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
Expand Down
40 changes: 18 additions & 22 deletions models/weibo/weibo.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ export class WeiboQuery {
static async formatTextDynamicData(upName: string, raw_post: any, isForward?: boolean, setData?: any) {
let msg: any[] = [],
/**全部图片资源链接*/
raw_pics_list,
raw_pics_list: any[] = [],
/**图片高清资源链接*/
pic_urls: string[],
pic_urls: string[] = [],
/**图片*/
pics;
pics: any[] = [];

let info = raw_post?.mblog || raw_post;
let retweeted = info && info.retweeted_status ? true : false; //是否为转发动态
Expand Down Expand Up @@ -241,11 +241,12 @@ export class WeiboQuery {
`标题:${info?.page_info?.title || ''}\n`,
`${this.filterText(info?.text)}\n`,
`链接:${detail_url}\n`,
`时间:${created_time ? moment(created_time).format('YYYY年MM月DD日 HH:mm:ss') : ''}\n`,
cover_img
`时间:${created_time ? moment(created_time).format('YYYY年MM月DD日 HH:mm:ss') : ''}`
];

return msg;
pics = [cover_img];

return { msg, pics };
case 'DYNAMIC_TYPE_DRAW':
raw_pics_list = retweeted ? info?.retweeted_status?.pics || [] : info?.pics || [];

Expand All @@ -255,8 +256,6 @@ export class WeiboQuery {

pic_urls = raw_pics_list.map((img: any) => img?.large?.url);

pics = [];

for (let pic_url of pic_urls) {
const temp = segment.image(pic_url, false, 15000, { referer: 'https://weibo.com' });
pics.push(temp);
Expand All @@ -268,11 +267,10 @@ export class WeiboQuery {
`-----------------------------\n`,
`${this.dynamicContentLimit(this.filterText(info?.text), setData)}\n`,
`链接:${detail_url}\n`,
`时间:${created_time ? moment(created_time).format('YYYY年MM月DD日 HH:mm:ss') : ''}\n`,
...pics
`时间:${created_time ? moment(created_time).format('YYYY年MM月DD日 HH:mm:ss') : ''}`
];

return msg;
return { msg, pics };
case 'DYNAMIC_TYPE_ARTICLE':
if (!info) return;

Expand All @@ -282,8 +280,6 @@ export class WeiboQuery {

pic_urls = raw_pics_list.map(img => img?.large?.url);

pics = [];

for (const pic_url of pic_urls) {
const temp = segment.image(pic_url, false, 15000, { referer: 'https://weibo.com' });
pics.push(temp);
Expand All @@ -295,23 +291,23 @@ export class WeiboQuery {
`-----------------------------\n`,
`正文:${this.dynamicContentLimit(this.filterText(info?.text), setData)}\n`,
`链接:${detail_url}\n`,
`时间:${created_time ? moment(created_time).format('YYYY年MM月DD日 HH:mm:ss') : ''}\n`,
...pics
`时间:${created_time ? moment(created_time).format('YYYY年MM月DD日 HH:mm:ss') : ''}`
];

return msg;
return { msg, pics };
case 'DYNAMIC_TYPE_FORWARD':
if (!info) return;
if (!info?.retweeted_status) return;

const origin_post_info = info?.retweeted_status;
isForward = true;
let orig = await this.formatTextDynamicData(upName, origin_post_info, isForward, setData);

if (orig && orig.length) {
orig = orig.slice(2);
let origContent = [];
if (orig && typeof orig === 'object') {
origContent = orig.msg.slice(2);
pics = orig.pics;
} else {
return false;
return 'continue';
}

title = `微博【${upName}】转发动态推送:\n`;
Expand All @@ -322,10 +318,10 @@ export class WeiboQuery {
`链接:${detail_url}\n`,
`时间:${created_time ? moment(created_time).format('YYYY年MM月DD日 HH:mm:ss') : ''}\n`,
'\n---以下为转发内容---\n',
...orig
...origContent
];

return msg;
return { msg, pics };
default:
logger?.mark(`未处理的微博推送【${upName}】:${type}`);
return 'continue';
Expand Down
19 changes: 13 additions & 6 deletions models/weibo/weibo.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class WeiboTask {
let imgs: Buffer[] | null = await this.renderDynamicCard(uid, renderData, ScreenshotOptionsData);
if (!imgs) return;

redis.set(`${markKey}${chatId}:${id_str}`, '1', { EX: 3600 * 10 }); // 设置已发送标记
redis.set(`${markKey}${chatId}:${id_str}`, '1', { EX: 3600 * 72 }); // 设置已发送标记

(logger ?? Bot.logger)?.mark('优纪插件:微博动态执行推送');

Expand All @@ -219,22 +219,29 @@ export class WeiboTask {

await new Promise(resolve => setTimeout(resolve, 1000)); // 休眠1秒
} else {
const dynamicMsg: string[] | 'continue' | false = await WeiboQuery.formatTextDynamicData(upName, pushDynamicData, false, weiboConfigData); //构建文字动态消息
const dynamicMsg = await WeiboQuery.formatTextDynamicData(upName, pushDynamicData, false, weiboConfigData); //构建文字动态消息

redis.set(`${markKey}${chatId}:${id_str}`, '1', { EX: 3600 * 10 }); // 设置已发送标记
redis.set(`${markKey}${chatId}:${id_str}`, '1', { EX: 3600 * 72 }); // 设置已发送标记

if (dynamicMsg == 'continue' || dynamicMsg == false) {
if (dynamicMsg == 'continue') {
return 'return'; // 如果动态消息构建失败或内部资源获取失败,则直接返回
}

if (weiboConfigData.banWords.length > 0) {
const banWords = new RegExp(weiboConfigData.banWords.join('|'), 'g'); // 构建屏蔽关键字正则表达式
if (banWords.test(dynamicMsg.join(''))) {
if (banWords.test(dynamicMsg.msg.join(''))) {
return 'return'; // 如果动态消息包含屏蔽关键字,则直接返回
}
}

await this.sendMessage(chatId, bot_id, chatType, dynamicMsg);
await this.sendMessage(chatId, bot_id, chatType, dynamicMsg.msg);
const pics = dynamicMsg.pics;
if (pics && pics.length > 0) {
for (let i = 0; i < pics.length; i++) {
await this.sendMessage(chatId, bot_id, chatType, pics[i]);
await this.randomDelay(1000, 2000); // 随机延时1-2秒
}
}
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
Expand Down
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yuki-plugin",
"version": "2.0.5-16",
"version": "2.0.6-0",
"author": "snowtafir",
"description": "优纪插件,yunzai-V4 关于 微博推送、B站推送 等功能的拓展插件",
"main": "./index",
Expand All @@ -22,20 +22,20 @@
"url": "https://gitee.com/snowtafir/yuki-plugin"
},
"dependencies": {
"axios": "^1.7.7",
"chokidar": "^3.6.0",
"jsdom": "^24.1.1",
"axios": "^1.7.9",
"chokidar": "^4.0.1",
"jsdom": "^25.0.1",
"jsxp": "1.0.4",
"lodash": "^4.17.21",
"md5": "^2.3.0",
"moment": "^2.30.1",
"node-fetch": "^3.3.2",
"puppeteer": "^23.8.0",
"puppeteer": "^23.10.1",
"qrcode": "^1.5.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"redis": "^4.7.0",
"yaml": "^2.5.0",
"yaml": "^2.6.1",
"json5": "^2.2.3"
},
"devDependencies": {
Expand All @@ -55,17 +55,17 @@
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/yaml": "1.9.7",
"axios": "^1.7.7",
"chokidar": "^3.6.0",
"axios": "^1.7.9",
"chokidar": "^4.0.1",
"husky": "^9.1.6",
"jsxp": "1.0.4",
"jsdom": "^24.1.1",
"jsdom": "^25.0.1",
"lodash": "^4.17.21",
"md5": "^2.3.0",
"node-fetch": "^3.3.2",
"pm2": "^5.4.2",
"prettier": "^3.3.3",
"puppeteer": "^23.8.0",
"prettier": "^3.4.2",
"puppeteer": "^23.10.1",
"qrcode": "^1.5.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand All @@ -76,6 +76,7 @@
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-ignore": "^1.0.10",
"typescript": "^5.6.3",
"yaml": "^2.6.1",
"json5": "^2.2.3"
}
}

0 comments on commit ac9f46f

Please sign in to comment.