From 5b806dd8dd2a39d2fd4fa2850fde9ccef0e783e2 Mon Sep 17 00:00:00 2001 From: yawwwwwn <40122222+yawwwwwn@users.noreply.github.com> Date: Thu, 20 Jun 2019 00:28:06 +0800 Subject: [PATCH] PK lottery MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加大乱斗获胜抽奖 其他日常更新 --- bilibili.py | 20 ++++++++++++++ bilibiliCilent.py | 7 +++-- login.py | 2 +- pkLottery.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++ run.py | 6 +++-- 5 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 pkLottery.py diff --git a/bilibili.py b/bilibili.py index 36dd1ae..66d9488 100644 --- a/bilibili.py +++ b/bilibili.py @@ -274,6 +274,13 @@ async def get_gift_of_captain(self, roomid, id): response2 = await self.bili_section_post(join_url, data=payload, headers=self.dic_bilibili['pcheaders']) return response2 + async def get_gift_of_pk(self, roomid, id): + join_url = "https://api.live.bilibili.com/xlive/lottery-interface/v1/pk/join" + payload = {"roomid": roomid, "id": id, "csrf": self.dic_bilibili['csrf'], + "csrf_token": self.dic_bilibili['csrf']} + response2 = await self.bili_section_post(join_url, data=payload, headers=self.dic_bilibili['pcheaders']) + return response2 + async def get_giftlist_of_events(self, text1): headers = { 'Accept': 'application/json, text/plain, */*', @@ -323,6 +330,11 @@ async def get_giftlist_of_captain(self, roomid): response1 = await self.bili_section_get(true_url, headers=headers) return response1 + async def get_lotterylist_of_pk(self, roomid): + url = 'http://api.live.bilibili.com/xlive/lottery-interface/v1/pk/check?roomid=' + str(roomid) + response = await self.bili_section_get(url, headers=self.dic_bilibili['pcheaders']) + return response + def get_giftids_raffle(self, str): return self.dic_bilibili['giftids_raffle'][str] @@ -394,6 +406,14 @@ async def guard_list(self): response = requests.get(url, headers=headers) return response + async def pk_list(self): + url = "http://118.25.108.153:8080/pk" + headers = { + "User-Agent": "bilibili-live-tools/" + str(self.dic_bilibili['uid']) + } + response = requests.get(url, headers=headers) + return response + async def get_lotterylist(self, i): url = "https://api.live.bilibili.com/lottery/v1/box/getStatus?aid=" + \ str(i) diff --git a/bilibiliCilent.py b/bilibiliCilent.py index 2953a8f..716331d 100644 --- a/bilibiliCilent.py +++ b/bilibiliCilent.py @@ -265,12 +265,15 @@ async def parseDanMu(self, messages): # 活动榜单相关 [进入小时榜,未知,获小时榜第一道具奖励] elif cmd in ["ROOM_RANK", "new_anchor_reward", "HOUR_RANK_AWARDS"]: pass - # 活动相关 [活动获得的直播间入场特效,活动事件(如充能值信息),以前的高能事件,送礼抽奖活动开奖,LOL竞猜活动] - elif cmd in ["WELCOME_ACTIVITY", "ACTIVITY_EVENT", "EVENT_CMD", "BOX_LOTTERY_WIN", "LOL_ACTIVITY"]: + # 活动相关 [活动获得的直播间入场特效,活动事件(如充能值信息),以前的高能事件,送礼抽奖活动开奖,LOL竞猜活动,LOL助力活动] + elif cmd in ["WELCOME_ACTIVITY", "ACTIVITY_EVENT", "EVENT_CMD", "BOX_LOTTERY_WIN", "LOL_ACTIVITY", "ACTIVITY_MATCH_GIFT"]: pass # 直播间信息相关 [直播间更换壁纸,直播间界面皮肤变化,许愿瓶进度变化,关注数变化,实物抽奖宝箱提醒,实物抽奖宝箱开奖] elif cmd in ["CHANGE_ROOM_INFO", "ROOM_SKIN_MSG", "WISH_BOTTLE", "ROOM_REAL_TIME_MESSAGE_UPDATE", "BOX_ACTIVITY_START", "WIN_ACTIVITY"]: pass + # 大乱斗活动 + elif cmd in ["PK_BATTLE_PRE", "PK_BATTLE_START", "PK_BATTLE_PROCESS", "PK_BATTLE_PRO_TYPE", "PK_BATTLE_END", "PK_BATTLE_SETTLE_USER", "PK_BATTLE_SETTLE", "PK_LOTTERY_START"]: + pass else: Printer().printer(f"出现一个未知msg @[{self.area}分区]{self._roomId} {dic}", "Warning", "red") pass diff --git a/login.py b/login.py index e5601f4..f3a0435 100644 --- a/login.py +++ b/login.py @@ -78,7 +78,7 @@ def login(self): configloader.write2bilibili(dic_saved_session) Printer().printer(f"登录成功", "Info","green") except: - Printer().printer(f"登录失败,错误信息为:{response.json()['message']}","Error","red") + Printer().printer(f"登录失败,错误信息为:{response.json()}","Error","red") async def login_new(self): if bilibili().dic_bilibili['saved-session']['cookie']: diff --git a/pkLottery.py b/pkLottery.py new file mode 100644 index 0000000..3bfac62 --- /dev/null +++ b/pkLottery.py @@ -0,0 +1,68 @@ +import traceback +import asyncio + +from bilibili import bilibili +from printer import Printer +import utils + + +class PKLottery: + last_pk_room = 0 + had_gotten_pk = [] + + async def pk_lottery(self): + for _ in range(3): + try: + response = await bilibili().pk_list() + json_response = response.json() + # print(json_response) + break + except Exception: + continue + else: + Printer().printer("连接大乱斗服务器失败", "Error", "red") + return + for i in range(0, len(json_response)): + PKId = json_response[i]['PKId'] + if PKId not in PKLottery.had_gotten_pk and PKId != 0: + PKLottery.had_gotten_pk.append(PKId) + OriginRoomId = json_response[i]['OriginRoomId'] + if not OriginRoomId == PKLottery.last_pk_room: + result = await utils.check_room_true(OriginRoomId) + if True in result: + Printer().printer(f"检测到房间 {OriginRoomId} 的钓鱼操作", "Warning", "red") + continue + await bilibili().post_watching_history(OriginRoomId) + PKLottery.last_pk_room = OriginRoomId + response2 = await bilibili().get_gift_of_pk(OriginRoomId, PKId) + json_response2 = await response2.json(content_type=None) + # print(json_response2) + # {'code': 0, 'message': '0', 'ttl': 1, 'data': {'id': 343560, 'gift_type': 0, 'award_id': '1', 'award_text': '辣条X1', 'award_image': 'https://i0.hdslb.com/bfs/live/da6656add2b14a93ed9eb55de55d0fd19f0fc7f6.png', 'award_num': 0, 'title': '大乱斗获胜抽奖'}} + # {'code': -1, 'message': '抽奖已结束', 'ttl': 1} + # {'code': -2, 'message': '您已参加过抽奖', 'ttl': 1} + # {"code":-403,"data":null,"message":"访问被拒绝","msg":"访问被拒绝"} + if json_response2['code'] == 0: + Printer().printer(f"参与房间 {OriginRoomId} 编号 {PKId} 的大乱斗获胜抽奖: {json_response2['data']['award_text']}", + "Lottery", "cyan") + elif json_response2['code'] == -2 and json_response2['message'] == "您已参加过抽奖": + Printer().printer( + f"房间 {OriginRoomId} 编号 {PKId} 的大乱斗获胜抽奖已参与过", + "Info", "green") + elif json_response2['code'] == -403 and json_response2['message'] == "访问被拒绝": + Printer().printer(f"参与房间 {OriginRoomId} 编号 {PKId} 的大乱斗获胜抽奖: {json_response2['message']}", + "Lottery", "cyan") + print(json_response2) + else: + Printer().printer( + f"房间 {OriginRoomId} 编号 {PKId} 的大乱斗获胜抽奖参与出错: {json_response2}", + "Error", "red") + + + async def run(self): + while True: + try: + await self.pk_lottery() + await asyncio.sleep(30) + except Exception: + await asyncio.sleep(10) + Printer().printer(traceback.format_exc(), "Error", "red") diff --git a/run.py b/run.py index 524ae18..01a215f 100644 --- a/run.py +++ b/run.py @@ -11,6 +11,7 @@ from bilibili import bilibili import threading import biliconsole +from pkLottery import PKLottery loop = asyncio.get_event_loop() loop1 = asyncio.get_event_loop() @@ -25,6 +26,7 @@ task2 = Tasks() task3 = LotteryResult() task4 = connect() +task5 = PKLottery() console_thread = threading.Thread(target=biliconsole.controler) @@ -42,8 +44,8 @@ biliconsole.Biliconsole().run(), task4.create(), task3.query(), - rafflehandler.run() - + rafflehandler.run(), + task5.run() ] loop.run_until_complete(asyncio.wait(tasks))