feat: 随机转发热门视频(#40)

This commit is contained in:
shanmiteko 2021-10-06 20:42:22 +08:00
parent 27eae63762
commit e845e3f7ef
7 changed files with 110 additions and 18 deletions

View File

@ -2,6 +2,7 @@ const utils = require('../utils');
const bili = require('../net/bili');
const { sendNotify } = require('../helper/notify');
const event_bus = require('../helper/event_bus');
const { randomDynamic } = require('../helper/randomDynamic')
const { Searcher } = require('./searcher');
const global_var = require("../data/global_var");
const config = require("../data/config");
@ -119,11 +120,10 @@ class Monitor extends Searcher {
if (create_dy
&& create_dy_mode instanceof Array
&& index > 0
&& index % create_dy_mode[0] === 0) {
for (let i = 0; i < create_dy_mode[1]; i++) {
await bili.createDynamic(utils.getRandomOne(dy_contents));
await utils.delay(2000);
}
&& index % utils.getRandomOne(create_dy_mode[0]) === 0
) {
const number = utils.getRandomOne(create_dy_mode[1]) || 0;
randomDynamic(number)
}
status = await this.go(Lottery)

View File

@ -0,0 +1,39 @@
const config = require("../data/config");
const bili = require("../net/bili");
const utils = require("../utils");
/**
* 随机动态
* @param {number} num
* @returns
*/
async function randomDynamic(num) {
const { create_dy_type, dy_contents } = config;
if (create_dy_type === -1 || create_dy_type === 0 || typeof create_dy_type === 'undefined') {
for (let index = 0; index < num; index++) {
await bili.createDynamic(utils.getRandomOne(dy_contents));
await utils.delay(2000);
}
}
if (create_dy_type === -1 || create_dy_type === 1) {
let videos = await bili.getTopRcmd()
for (let index = 0; videos.length < num; index++) {
videos.push(...await bili.getTopRcmd())
}
await utils.try_for_each(videos, async ([uid, aid]) => {
if (num--) {
await bili.shareVideo(uid, aid)
await utils.delay(2000)
return false
} else {
return true
}
})
}
}
module.exports = { randomDynamic }

View File

@ -1,10 +1,10 @@
const utils = require('./utils');
const bili = require('./net/bili');
const event_bus = require('./helper/event_bus');
const global_var = require('./data/global_var');
const { Searcher } = require('./core/searcher');
const { Monitor } = require('./core/monitor');
const config = require('./data/config');
const { randomDynamic } = require('./helper/randomDynamic');
const { log } = utils;
async function createRandomDynamic(num) {
@ -12,10 +12,7 @@ async function createRandomDynamic(num) {
log.info('随机动态', `准备创建${num}条随机动态`);
const Dynamic = await Searcher.checkAllDynamic(global_var.get("myUID"), 1);
if ((Dynamic.allModifyDynamicResArray[0] || { type: 0 }).type === 1) {
for (let index = 0; index < num; index++) {
await bili.createDynamic(utils.getRandomOne(config.dy_contents));
await utils.delay(2000);
}
await randomDynamic(num)
} else {
log.info('随机动态', '已有非抽奖动态故无需创建');
}
@ -39,7 +36,6 @@ function start() {
return;
}
if (times.value() === lotterys.length) {
await createRandomDynamic(config.create_dy_num);
log.info('抽奖', '所有动态转发完毕');
times.clear();
event_bus.emit('Turn_off_the_Monitor', '目前无抽奖信息,过一会儿再来看看吧')

View File

@ -1,6 +1,7 @@
module.exports = Object.freeze({
DYNAMIC_LIKE_THUMB: 'https://api.vc.bilibili.com/dynamic_like/v1/dynamic_like/thumb',
DYNAMIC_REPOST_REPOST: 'https://api.vc.bilibili.com/dynamic_repost/v1/dynamic_repost/repost',
DYNAMIC_REPOST_SHARE: 'https://api.vc.bilibili.com/dynamic_repost/v1/dynamic_repost/share',
DYNAMIC_SVR_CREATE_DRAW: 'https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/create_draw',
DYNAMIC_SVR_CREATE: 'https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/create',
DYNAMIC_SVR_GET_DYNAMIC_DETAIL: 'https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/get_dynamic_detail',
@ -25,6 +26,7 @@ module.exports = Object.freeze({
SESSION_SVR_UPDATE_ACK: 'https://api.vc.bilibili.com/session_svr/v1/session_svr/update_ack',
SPACE_MYINFO: 'https://api.bilibili.com/x/space/myinfo',
TAG_INFO: 'https://api.bilibili.com/x/tag/info',
TOP_RCMD: "https://api.bilibili.com/x/web-interface/index/top/rcmd",
TOPIC_SVR_TOPIC_HISTORY: 'https://api.vc.bilibili.com/topic_svr/v1/topic_svr/topic_history',
TOPIC_SVR_TOPIC_NEW: 'https://api.vc.bilibili.com/topic_svr/v1/topic_svr/topic_new',
WEB_INTERFACE_CARD: 'https://api.bilibili.com/x/web-interface/card',

View File

@ -716,6 +716,53 @@ const bili_client = {
}
})
},
/**
* 获取推荐
* @returns {Promise<Array<[number, number]>>}
*/
getTopRcmd() {
return get(
{
url: API.TOP_RCMD,
}
).then(responseText => {
const res = strToJson(responseText);
if (res.code === 0) {
log.info('获取推荐', `成功`);
return res.data.item.map(it => {
return [it.owner.mid, it.id]
})
} else {
log.error('获取推荐', `获取推荐失败\n${responseText}`);
return []
}
})
},
/**
* 分享视频
* @param {number} uid
* @param {number} aid
*/
shareVideo(uid, aid) {
return post({
url: API.DYNAMIC_REPOST_SHARE,
contents: {
platform: "pc",
uid,
type: 8,
content: "分享视频",
repost_code: 20000,
rid: aid,
csrf_token: GlobalVar.get("csrf")
}
}).then(responseText => {
if (/^{"code":0/.test(responseText)) {
log.info('转发视频', `成功转发视频(av${aid})`);
} else {
log.error('转发视频', `转发失败\n${responseText}`);
}
})
},
/**
* 移除动态
* @param {string} dyid

View File

@ -122,17 +122,19 @@ module.exports = Object.freeze({
*/
create_dy: false,
/**
* 随机动态类型
* - 0 自定义文字与图片
* - 1 推荐视频
* - -1 混合
*/
create_dy_type: 0,
/**
* - 结束运行时发送随机动态的数量
*/
create_dy_num: 1,
/**
* - 每转发x条抽奖动态就发送x条随机动态
* - @example [6,1] 每转发6条抽奖动态就发送1条随机动态
*/
create_dy_mode: [0, 0],
/**
* - 随机动态内容
* - 类型 `content[]`
@ -144,6 +146,12 @@ module.exports = Object.freeze({
*/
dy_contents: ['[doge]', '[doge][doge]'],
/**
* - 每转发x条抽奖动态就发送x条随机动态
* - @example [[10,11,9],[6,8,9]] 每转发9,10,11条抽奖动态就发送6,8,9条随机动态
*/
create_dy_mode: [[0], [0]],
/**
* 转发时[at]的用户
*/

View File

@ -1,6 +1,6 @@
{
"name": "lottery-auto-script",
"version": "2.2.1",
"version": "2.2.2",
"description": "自动参与B站动态抽奖",
"main": "main.js",
"scripts": {