feat: 阻止同时运行多个抽奖

This commit is contained in:
shanmite 2021-03-19 12:50:10 +08:00
parent dc3fb9c026
commit 106f237055
6 changed files with 66 additions and 20 deletions

View File

@ -88,6 +88,37 @@ const GihubAPI = {
}
})
});
},
/**
* 检查是否正在抽奖
* @returns {Promise<boolean>}
*/
hasLotteryRun: () => {
return new Promise((resolve) => {
HttpRequest({
method: 'GET',
url: `https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs`,
query: {
status: "in_progress"
},
headers: {
accept: 'application/vnd.github.v3+json',
},
success: res => {
const { workflow_runs } = strToJson(res.body);
if (workflow_runs instanceof Array) {
workflow_runs.filter(wr => /sweepstakes/.test(wr.name)).length > 1 ?
resolve(true) : resolve(false)
} else {
resolve(true)
}
},
failure: err => {
console.log(err);
resolve(true)
}
})
});
}
}

View File

@ -185,7 +185,7 @@ class Public {
console.log(`开始获取带话题#${tag_name}#的动态信息`);
let mDRdata = modDR.modifyDynamicResArray; /* 热门动态 */
let next_offset = modDR.nextinfo.next_offset;
for (let index = 0; index < 6; index++) {
for (let index = 0; index < 3; index++) {
const newdy = await BiliAPI.getOneDynamicInfoByTag(tag_name, next_offset);
const _modify = self.modifyDynamicRes(newdy);
if (_modify === null) return null;
@ -219,7 +219,7 @@ class Public {
*/
async getLotteryInfoByUID(UID) {
console.log(`开始获取用户${UID}的动态信息`);
const { allModifyDynamicResArray: aMDRA } = await this.checkAllDynamic(UID, 6, 500);
const { allModifyDynamicResArray: aMDRA } = await this.checkAllDynamic(UID, 3, 500);
if (!aMDRA.length) return null;
const fomatdata = aMDRA.map(o => {
return {

View File

@ -31,6 +31,7 @@ const my_config = (() => {
try {
return JSON.parse(process.env.MY_CONFIG);
} catch (_) {
console.log("MY_CONFIG语法错误");
return {};
}
})();

View File

@ -1,6 +1,7 @@
const Base = require('./Base');
const { transformTimeZone } = require('./Base');
const BiliAPI = require('./BiliAPI');
const GihubAPI = require("./GithubAPI");
const eventBus = require('./eventBus');
const GlobalVar = require('./GlobalVar.json');
const Public = require('./Public');
@ -50,7 +51,18 @@ function start() {
createRandomDynamic()
resolve()
})
eventBus.emit('Turn_on_the_Monitor');
if(process.env.LOCALLAUNCH) {
eventBus.emit('Turn_on_the_Monitor');
} else {
GihubAPI.hasLotteryRun().then(s => {
if (s) {
console.log("另一个抽奖任务正在运行故终止此任务");
resolve()
} else {
eventBus.emit('Turn_on_the_Monitor');
}
})
}
});
}

View File

@ -13,29 +13,30 @@ let GlobalVar = {
* @type {(string|number)[]}
*/
Lottery: [...Script.UIDs, ...Script.TAGs],
SCKEY: '',
remoteconfig: {},
}
/**
* 生成全局变量文件
* @param {string} cookie
* @param {string} [PAT]
* @param {string} [pat]
* @param {string} [SCKEY]
* @param {string} [PUSH_PLUS_TOKEN]
*/
async function setVariable(cookie, PAT = '') {
const key = ['DedeUserID','bili_jct']
GlobalVar.cookie = cookie;
cookie.split(/\s*;\s*/).forEach(item=>{
const _item = item.split('=');
if (key.indexOf(_item[0]) !== -1) GlobalVar[_item[0]] = _item[1];
})
GlobalVar.csrf = GlobalVar.bili_jct;
GlobalVar.myUID = GlobalVar.DedeUserID;
GlobalVar.remoteconfig = await Base.getRemoteConfig();
await Base.ifNotExistCreateFile('GlobalVar.json', JSON.stringify(GlobalVar));
if (PAT) {
await MyStorage.init(PAT)
async function setVariable(cookie, pat = '') {
if (cookie) {
const key = ['DedeUserID','bili_jct']
GlobalVar.cookie = cookie;
cookie.split(/\s*;\s*/).forEach(item=>{
const _item = item.split('=');
if (key.indexOf(_item[0]) !== -1) GlobalVar[_item[0]] = _item[1];
})
GlobalVar.csrf = GlobalVar.bili_jct;
GlobalVar.myUID = GlobalVar.DedeUserID;
GlobalVar.remoteconfig = await Base.getRemoteConfig();
await Base.ifNotExistCreateFile('GlobalVar.json', JSON.stringify(GlobalVar));
}
if (pat) {
await MyStorage.init(pat)
} else {
await Base.ifNotExistCreateFile('dyid.txt')
}

View File

@ -5,13 +5,14 @@ const { NUMBER, CLEAR, COOKIE, PAT, LOCALLAUNCH } = process.env;
((async () => {
if (typeof COOKIE === 'string' && COOKIE.length > 10) {
if (!LOCALLAUNCH && !PAT) { console.log('请查看README文件, 填入相应的PAT'); return; }
await setVariable(COOKIE, PAT);
await setVariable(COOKIE);
const { start, isMe, checkCookie } = require("./lib/lottery-in-nodejs");
const { clear } = require("./lib/clear");
const isRight = await checkCookie(NUMBER);
if (!isRight) return;
switch (process.argv.slice(2)[0]) {
case 'start':
await setVariable('', PAT);
console.log('开始参与抽奖');
await start();
break;
@ -20,7 +21,7 @@ const { NUMBER, CLEAR, COOKIE, PAT, LOCALLAUNCH } = process.env;
await isMe();
break;
case 'clear':
if (typeof CLEAR === 'string' && CLEAR === 'true') {
if (CLEAR === 'true') {
console.log('开始清理动态');
await clear();
console.log('清理动态完毕');