From 0d6cabf3aed778e76abc68cbdff7ec87dcb8c78a Mon Sep 17 00:00:00 2001 From: lzghzr Date: Thu, 18 Jan 2018 17:07:49 +0800 Subject: [PATCH] close #53 --- bilive/listener.ts | 86 ++++++++++++++++--------------------- bilive/options.default.json | 55 ++++++++++++------------ bilive/raffle.ts | 74 +++++++++++++++---------------- package.json | 8 ++-- 4 files changed, 106 insertions(+), 117 deletions(-) diff --git a/bilive/listener.ts b/bilive/listener.ts index c899fc3..0438702 100644 --- a/bilive/listener.ts +++ b/bilive/listener.ts @@ -1,17 +1,16 @@ import * as request from 'request' -import * as tools from './lib/tools' import { EventEmitter } from 'events' -import { AppClient } from './lib/app_client' -import { CommentClient } from './lib/comment_client' +import * as tools from './lib/tools' +import AppClient from './lib/app_client' +import DMclient from './dm_client_re' import { liveOrigin, apiLiveOrigin, smallTVPathname, rafflePathname, _options } from './index' /** * 监听服务器消息 * - * @export * @class Listener * @extends {EventEmitter} */ -export class Listener extends EventEmitter { +class Listener extends EventEmitter { constructor() { super() } @@ -19,10 +18,10 @@ export class Listener extends EventEmitter { * 用于接收弹幕消息 * * @private - * @type {CommentClient} + * @type {DMclient} * @memberof Listener */ - private _CommentClient: CommentClient + private _DMclient: DMclient /** * 小电视ID * @@ -61,14 +60,13 @@ export class Listener extends EventEmitter { * @memberof Listener */ public Start() { - let config = _options.config - , roomID = config.defaultRoomID - , userID = config.defaultUserID - this._CommentClient = new CommentClient({ roomID, userID }) - this._CommentClient - .on('serverError', error => { tools.Error('弹幕服务器监听', error) }) - .on('SYS_MSG', this._SYSMSGHandler.bind(this)) - .on('SYS_GIFT', this._SYSGiftHandler.bind(this)) + const config = _options.config + const roomID = config.defaultRoomID + const userID = config.defaultUserID + this._DMclient = new DMclient({ roomID, userID }) + this._DMclient + .on('SYS_MSG', dataJson => this._SYSMSGHandler(dataJson)) + .on('SYS_GIFT', dataJson => this._SYSGiftHandler(dataJson)) .Connect() } /** @@ -79,10 +77,10 @@ export class Listener extends EventEmitter { * @memberof Listener */ private _SYSMSGHandler(dataJson: SYS_MSG) { - if (dataJson.real_roomid == null || dataJson.tv_id == null) return - let url = apiLiveOrigin + smallTVPathname - , roomID = dataJson.real_roomid - this._RaffleCheck(url, roomID, 'smallTV').catch(error => { tools.Error('系统消息', roomID, error) }) + if (dataJson.real_roomid === undefined || dataJson.tv_id === undefined) return + const url = apiLiveOrigin + smallTVPathname + const roomID = dataJson.real_roomid + this._RaffleCheck(url, roomID, 'smallTV') } /** * 监听系统礼物消息 @@ -92,11 +90,11 @@ export class Listener extends EventEmitter { * @memberof Listener */ private _SYSGiftHandler(dataJson: SYS_GIFT) { - if (dataJson.real_roomid == null || dataJson.giftId == null) return - let url = apiLiveOrigin + rafflePathname - , roomID = dataJson.real_roomid - this._RaffleCheck(url, roomID, 'raffle').catch(error => { tools.Error('系统礼物消息', roomID, error) }) - // this._AppLightenCheck(roomID).catch(error => { tools.Error('系统礼物消息', roomID, error) }) + if (dataJson.real_roomid === undefined || dataJson.giftId === undefined) return + const url = apiLiveOrigin + rafflePathname + const roomID = dataJson.real_roomid + this._AppLightenCheck(roomID) + this._RaffleCheck(url, roomID, 'raffle') } /** * 检查房间抽奖信息 @@ -108,30 +106,21 @@ export class Listener extends EventEmitter { * @memberof Listener */ private async _RaffleCheck(url: string, roomID: number, raffle: 'smallTV' | 'raffle' | 'lighten') { - let check: request.Options = { + const check: request.Options = { uri: `${url}/check?roomid=${roomID}`, json: true, headers: { 'Referer': `${liveOrigin}/${roomID}` } } - , raffleCheck = await tools.XHR(check) - if (raffleCheck.response.statusCode === 200 && raffleCheck.body.code === 0 && raffleCheck.body.data.length > 0) { + const raffleCheck = await tools.XHR(check) + if (raffleCheck !== undefined && raffleCheck.response.statusCode === 200 + && raffleCheck.body.code === 0 && raffleCheck.body.data.length > 0) { raffleCheck.body.data.forEach(data => { - let message: raffleMSG = { + const message: raffleMSG = { cmd: raffle, roomID, id: +data.raffleId } this._RaffleHandler(message) - // 临时 - if (raffle === 'raffle') { - let message: appLightenMSG = { - cmd: 'appLighten', - roomID, - id: +data.raffleId, - type: 'openfire' - } - this._RaffleHandler(message) - } }) } } @@ -142,18 +131,18 @@ export class Listener extends EventEmitter { * @param {number} roomID * @memberof Listener */ - // @ts-ignore 暂时无用 private async _AppLightenCheck(roomID: number) { - let room: request.Options = { + const room: request.Options = { uri: `${apiLiveOrigin}/AppRoom/index?${AppClient.signQueryBase(`room_id=${roomID}`)}`, json: true } - , roomInfo = await tools.XHR(room, 'Android') - if (roomInfo.response.statusCode === 200 && roomInfo.body.code === 0 && roomInfo.body.data.event_corner.length > 0) { + const roomInfo = await tools.XHR(room, 'Android') + if (roomInfo !== undefined && roomInfo.response.statusCode === 200 + && roomInfo.body.code === 0 && roomInfo.body.data.event_corner.length > 0) { roomInfo.body.data.event_corner.forEach(event => { - let type = event.event_type.split('-') + const type = event.event_type.split('-') if (type.length !== 2) return - let message: appLightenMSG = { + const message: appLightenMSG = { cmd: 'appLighten', roomID, id: +type[1], @@ -171,9 +160,9 @@ export class Listener extends EventEmitter { * @memberof Listener */ private _RaffleHandler(raffleMSG: raffleMSG | appLightenMSG) { - let roomID = raffleMSG.roomID - , id = raffleMSG.id - , msg = '' + const roomID = raffleMSG.roomID + const id = raffleMSG.id + let msg = '' switch (raffleMSG.cmd) { case 'smallTV': if (this._smallTVID >= id) return @@ -199,4 +188,5 @@ export class Listener extends EventEmitter { this.emit('raffle', raffleMSG) tools.Log(`房间 ${roomID} 开启了第 ${id} 轮${msg}抽奖`) } -} \ No newline at end of file +} +export default Listener \ No newline at end of file diff --git a/bilive/options.default.json b/bilive/options.default.json index 81e69ce..fc1bb82 100644 --- a/bilive/options.default.json +++ b/bilive/options.default.json @@ -6,49 +6,48 @@ "protocol": "admin" }, "apiIPs": [ - "101.69.131.10", + "14.136.134.87", + "27.221.61.109", + "47.88.138.238", + "47.90.50.109", + "47.90.207.174", + "47.91.19.168", + "47.91.74.133", + "58.222.35.202", + "58.222.35.205", + "61.147.236.15", + "61.188.190.7", "101.75.240.7", + "101.200.58.11", "110.80.129.143", + "111.6.174.4", + "111.47.237.18", + "111.62.72.10", + "111.230.84.45", + "111.230.84.59", + "111.230.85.89", + "111.231.212.88", "112.117.218.167", - "113.113.80.9", - "113.207.39.7", "113.6.246.37", + "113.113.80.9", "114.80.223.172", + "114.80.223.177", "116.207.118.12", "118.89.213.55", - "120.221.74.132", "120.41.32.15", + "120.192.82.106", + "120.221.74.132", "121.32.238.201", "121.32.238.204", + "122.143.15.150", "122.228.77.82", - "124.14.8.137", - "124.232.151.74", + "122.228.77.85", + "123.206.1.201", "125.88.148.10", - "14.136.134.86", - "14.136.134.87", - "140.143.177.142", - "140.249.9.4", - "218.98.33.197", "219.144.79.7", "221.13.201.9", "221.204.199.135", - "222.186.35.81", - "223.99.231.4", - "27.221.106.4", - "27.221.61.109", - "36.248.28.7", - "47.88.107.100", - "47.88.138.238", - "47.90.207.174", - "47.90.50.109", - "47.91.19.168", - "47.91.74.133", - "58.222.35.202", - "58.222.35.205", - "58.56.111.209", - "59.47.225.8", - "61.147.236.15", - "61.188.190.7" + "222.186.35.81" ], "config": { "defaultUserID": 0, diff --git a/bilive/raffle.ts b/bilive/raffle.ts index 618dcd7..b7292d5 100644 --- a/bilive/raffle.ts +++ b/bilive/raffle.ts @@ -1,25 +1,24 @@ import * as request from 'request' import * as tools from './lib/tools' -import { User } from './user' -import { AppClient } from './lib/app_client' +import User from './user' +import AppClient from './lib/app_client' import { liveOrigin, apiLiveOrigin, smallTVPathname, rafflePathname, lightenPathname } from './index' /** * 自动参与抽奖 * - * @export * @class Raffle */ -export class Raffle { +class Raffle { /** * 创建一个 Raffle 实例 * @param {raffleOptions} raffleOptions * @memberof Raffle */ constructor(raffleOptions: raffleOptions) { - if (raffleOptions.type != null) this._type = raffleOptions.type + if (raffleOptions.type !== undefined) this._type = raffleOptions.type this._raffleId = raffleOptions.raffleId this._roomID = raffleOptions.roomID - this._User = raffleOptions.User + this._user = raffleOptions.user } /** * type @@ -52,7 +51,7 @@ export class Raffle { * @type {User} * @memberof Raffle */ - private _User: User + private _user: User /** * 抽奖地址 * @@ -68,7 +67,7 @@ export class Raffle { */ public SmallTV() { this._url = apiLiveOrigin + smallTVPathname - return this._Raffle() + this._Raffle() } /** * 参与抽奖 @@ -77,7 +76,7 @@ export class Raffle { */ public Raffle() { this._url = apiLiveOrigin + rafflePathname - return this._Raffle() + this._Raffle() } /** * 抽奖 @@ -86,17 +85,16 @@ export class Raffle { * @memberof Raffle */ private async _Raffle() { - let join: request.Options = { + const join: request.Options = { uri: `${this._url}/join?roomid=${this._roomID}&raffleId=${this._raffleId}`, - jar: this._User.jar, + jar: this._user.jar, json: true, headers: { 'Referer': `${liveOrigin}/${this._roomID}` } } - , raffleJoin = await tools.XHR(join) - if (raffleJoin.response.statusCode === 200 && raffleJoin.body.code === 0) { - let time = raffleJoin.body.data.time * 1e+3 + 3e+4 - await tools.Sleep(time) - this._RaffleReward().catch(error => { tools.Error(this._User.nickname, '获取抽奖结果', this._raffleId, error) }) + const raffleJoin = await tools.XHR(join) + if (raffleJoin !== undefined && raffleJoin.response.statusCode === 200 && raffleJoin.body.code === 0) { + await tools.Sleep(120 * 1000) + this._RaffleReward() } } /** @@ -106,22 +104,22 @@ export class Raffle { * @memberof Raffle */ private async _RaffleReward() { - let reward: request.Options = { + const reward: request.Options = { uri: `${this._url}/notice?roomid=${this._roomID}&raffleId=${this._raffleId}`, - jar: this._User.jar, + jar: this._user.jar, json: true, headers: { 'Referer': `${liveOrigin}/${this._roomID}` } } - , raffleReward = await tools.XHR(reward) - if (raffleReward.response.statusCode !== 200) return + const raffleReward = await tools.XHR(reward) + if (raffleReward === undefined || raffleReward.response.statusCode !== 200) return if (raffleReward.body.code === -400 || raffleReward.body.data.status === 3) { - await tools.Sleep(3e+4) //30s - this._RaffleReward().catch(error => { tools.Error(this._User.nickname, '获取抽奖结果', this._raffleId, error) }) + await tools.Sleep(30 * 1000) + this._RaffleReward() } else { - let gift = raffleReward.body.data - if (gift.gift_num === 0) tools.Log(this._User.nickname, `抽奖 ${this._raffleId}`, raffleReward.body.msg) - else tools.Log(this._User.nickname, `抽奖 ${this._raffleId}`, `获得 ${gift.gift_num} 个${gift.gift_name}`) + const gift = raffleReward.body.data + if (gift.gift_num === 0) tools.Log(this._user.nickname, `抽奖 ${this._raffleId}`, raffleReward.body.msg) + else tools.Log(this._user.nickname, `抽奖 ${this._raffleId}`, `获得 ${gift.gift_num} 个${gift.gift_name}`) } } /** @@ -131,17 +129,17 @@ export class Raffle { */ public async Lighten() { this._url = apiLiveOrigin + lightenPathname - let getCoin: request.Options = { + const getCoin: request.Options = { method: 'POST', uri: `${this._url}/getCoin`, body: `roomid=${this._roomID}&lightenId=${this._raffleId}}`, - jar: this._User.jar, + jar: this._user.jar, json: true, headers: { 'Referer': `${liveOrigin}/${this._roomID}` } } - , lightenReward = await tools.XHR(getCoin) - if (lightenReward.response.statusCode === 200 && lightenReward.body.code === 0) - tools.Log(this._User.nickname, `抽奖 ${this._raffleId}`, lightenReward.body.msg) + const lightenReward = await tools.XHR(getCoin) + if (lightenReward !== undefined && lightenReward.response.statusCode === 200 && lightenReward.body.code === 0) + tools.Log(this._user.nickname, `抽奖 ${this._raffleId}`, lightenReward.body.msg) } /** * app快速抽奖 @@ -150,14 +148,16 @@ export class Raffle { * @memberof Raffle */ public async AppLighten() { - let reward: request.Options = { + const reward: request.Options = { uri: `${apiLiveOrigin}/YunYing/roomEvent?${AppClient.signQueryBase(`event_type=${this._type}-${this._raffleId}\ -&room_id=${this._roomID}&${this._User.tokenQuery}`)}`, +&room_id=${this._roomID}&${this._user.tokenQuery}`)}`, json: true, - headers: this._User.headers + headers: this._user.headers } - , appLightenReward = await tools.XHR(reward, 'Android') - if (appLightenReward.response.statusCode === 200 && appLightenReward.body.code === 0) - tools.Log(this._User.nickname, `抽奖 ${this._raffleId}`, `获得${appLightenReward.body.data.gift_desc}`) + const appLightenReward = await tools.XHR(reward, 'Android') + if (appLightenReward !== undefined + && appLightenReward.response.statusCode === 200 && appLightenReward.body.code === 0) + tools.Log(this._user.nickname, `抽奖 ${this._raffleId}`, `获得${appLightenReward.body.data.gift_desc}`) } -} \ No newline at end of file +} +export default Raffle \ No newline at end of file diff --git a/package.json b/package.json index 63083f8..c2fdf2b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bilive_client", - "version": "1.3.1", + "version": "1.3.2", "description": "基于Node.JS的bilibili直播挂机系统", "main": "index.js", "scripts": { @@ -14,8 +14,8 @@ "author": "lzghzr", "license": "MIT", "devDependencies": { - "@types/node": "^8.5.2", - "@types/request": "^2.0.9", + "@types/node": "^9.3.0", + "@types/request": "^2.0.10", "@types/ws": "^3.2.1", "npm-run-posix-or-windows": "^2.0.2", "rimraf": "^2.6.2", @@ -23,6 +23,6 @@ }, "dependencies": { "request": "^2.83.0", - "ws": "^3.3.3" + "ws": "^4.0.0" } }