From f08bd5d5718e96fdf82c9f30938cb48c903d8bf5 Mon Sep 17 00:00:00 2001 From: yawwwwwn <40122222+yawwwwwn@users.noreply.github.com> Date: Fri, 2 Aug 2019 22:59:45 +0800 Subject: [PATCH] Adapt to captain monitor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 匹配舰长监控返回数据的格式修改 组织上决定自动刷满勋章只送辣条和亿元了,避免各种新礼物gift_id报错 412风控提示 弹幕cmd例行维护 鉴于B站新活动太频繁,暂时取消新cmd的Warning 鉴于验证码服务器修复希望渺茫,增加手动输入验证码的模块 实物抽奖id范围更新 --- OnlineHeart.py | 61 ++++++++++++++++++++++------------------------- Tasks.py | 17 ++++++------- bilibili.py | 8 +++++++ bilibiliCilent.py | 19 +++++++++------ login.py | 39 +++++++++++++++++++++++------- 5 files changed, 88 insertions(+), 56 deletions(-) diff --git a/OnlineHeart.py b/OnlineHeart.py index 97a71fd..48fbb45 100644 --- a/OnlineHeart.py +++ b/OnlineHeart.py @@ -40,44 +40,41 @@ async def guard_lottery(self): Printer().printer("连接舰长服务器失败", "Error", "red") return for i in range(0, len(json_response)): - if json_response[i]['Status']: - GuardId = json_response[i]['GuardId'] - if GuardId not in had_gotted_guard and GuardId != 0: - had_gotted_guard.append(GuardId) - OriginRoomId = json_response[i]['OriginRoomId'] - if not OriginRoomId == OnlineHeart.last_guard_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) - OnlineHeart.last_guard_room = OriginRoomId - response2 = await bilibili().get_gift_of_captain(OriginRoomId, GuardId) - json_response2 = await response2.json(content_type=None) - if json_response2['code'] == 0: - Printer().printer(f"获取到房间 {OriginRoomId} 编号 {GuardId} 的上船亲密度: {json_response2['data']['message']}", - "Lottery", "cyan") - elif json_response2['code'] == 400 and json_response2['msg'] == "你已经领取过啦": - Printer().printer( - f"房间 {OriginRoomId} 编号 {GuardId} 的上船亲密度已领过", - "Info", "green") - elif json_response2['code'] == 400 and json_response2['msg'] == "访问被拒绝": - Printer().printer(f"获取房间 {OriginRoomId} 编号 {GuardId} 的上船亲密度: {json_response2['message']}", - "Lottery", "cyan") - print(json_response2) - else: - Printer().printer( - f"房间 {OriginRoomId} 编号 {GuardId} 的上船亲密度领取出错: {json_response2}", - "Error", "red") - else: - pass + GuardId = json_response[i]['GuardId'] + if GuardId not in had_gotted_guard and GuardId != 0: + had_gotted_guard.append(GuardId) + OriginRoomId = json_response[i]['OriginRoomId'] + if not OriginRoomId == OnlineHeart.last_guard_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) + OnlineHeart.last_guard_room = OriginRoomId + response2 = await bilibili().get_gift_of_captain(OriginRoomId, GuardId) + json_response2 = await response2.json(content_type=None) + if json_response2['code'] == 0: + Printer().printer(f"获取到房间 {OriginRoomId} 编号 {GuardId} 的上船亲密度: {json_response2['data']['message']}", + "Lottery", "cyan") + elif json_response2['code'] == 400 and json_response2['msg'] == "你已经领取过啦": + Printer().printer( + f"房间 {OriginRoomId} 编号 {GuardId} 的上船亲密度已领过", + "Info", "green") + elif json_response2['code'] == 400 and json_response2['msg'] == "访问被拒绝": + Printer().printer(f"获取房间 {OriginRoomId} 编号 {GuardId} 的上船亲密度: {json_response2['message']}", + "Lottery", "cyan") + print(json_response2) + else: + Printer().printer( + f"房间 {OriginRoomId} 编号 {GuardId} 的上船亲密度领取出错: {json_response2}", + "Error", "red") async def draw_lottery(self): black_list = ["123", "1111", "测试", "測試", "测一测", "ce-shi", "test", "T-E-S-T", "lala", # 已经出现 "測一測", "TEST", "Test", "t-e-s-t"] # 合理猜想 last_lottery = 0 - for i in range(295, 400): + for i in range(390, 600): response = await bilibili().get_lotterylist(i) json_response = await response.json() if json_response['code'] == 0: diff --git a/Tasks.py b/Tasks.py index f70cf76..93f2caa 100644 --- a/Tasks.py +++ b/Tasks.py @@ -77,13 +77,14 @@ async def send_gift(self): async def auto_send_gift(self): if self.dic_user['auto-gift']['on/off'] == "1": a = await utils.fetch_medal(printer=False) - res = await bilibili().gift_list() - json_res = await res.json() - temp_dic = {} - for j in range(0, len(json_res['data'])): - price = json_res['data'][j]['price'] - id = json_res['data'][j]['id'] - temp_dic[id] = price + # res = await bilibili().gift_list() + # json_res = await res.json() + # temp_dic = {} + # for j in range(0, len(json_res['data'])): + # price = json_res['data'][j]['price'] + # id = json_res['data'][j]['id'] + # temp_dic[id] = price + temp_dic = {1: 100, 6: 1000} x, temp = await utils.fetch_bag_list(printer=False) roomid = a[0] today_feed = a[1] @@ -95,7 +96,7 @@ async def auto_send_gift(self): gift_num = int(temp[i][1]) bag_id = int(temp[i][2]) expire = int(temp[i][3]) - if (gift_id != 4 and gift_id != 3 and gift_id != 9 and gift_id != 10) and expire != 0: + if gift_id in [1, 6] and expire != 0: if (gift_num * (temp_dic[gift_id] / 100) < left_num): calculate = calculate + temp_dic[gift_id] / 100 * gift_num tmp2 = temp_dic[gift_id] / 100 * gift_num diff --git a/bilibili.py b/bilibili.py index 66d9488..2ba7bcb 100644 --- a/bilibili.py +++ b/bilibili.py @@ -92,6 +92,10 @@ async def bili_section_post(self, url, headers=None, data=None): Printer().printer('403频繁,5s后重试',"Error","red") await asyncio.sleep(5) continue + elif response.status == 412: + Printer().printer('412触发风控被拒绝,60s后重试',"Error","red") + await asyncio.sleep(60) + continue tag = await self.replay_request(response) if tag: continue @@ -110,6 +114,10 @@ async def bili_section_get(self, url, headers=None, data=None, params=None): Printer().printer('403频繁,5s后重试',"Error","red") await asyncio.sleep(5) continue + elif response.status == 412: + Printer().printer('412触发风控被拒绝,60s后重试',"Error","red") + await asyncio.sleep(60) + continue tag = await self.replay_request(response) if tag: continue diff --git a/bilibiliCilent.py b/bilibiliCilent.py index 716331d..c44ec3a 100644 --- a/bilibiliCilent.py +++ b/bilibiliCilent.py @@ -240,7 +240,7 @@ async def parseDanMu(self, messages): elif cmd in ["NOTICE_MSG", "SYS_MSG"]: pass # 各种高能 [节奏风暴(开始 结束),高能广播(无抽奖 活动高能 全频风暴),抽奖通知(现在广播全在这里了),总督广播] - elif cmd in ["SPECIAL_GIFT", "SYS_GIFT", "SYS_MSG", "GUARD_MSG"]: + elif cmd in ["SPECIAL_GIFT", "SYS_GIFT", "SYS_MSG", "GUARD_MSG", "GUIARD_MSG"]: pass # 礼物效果 [连击开始,连击结束,使用积分加成卡] elif cmd in ["COMBO_SEND", "COMBO_END", "SCORE_CARD"]: @@ -265,15 +265,20 @@ async def parseDanMu(self, messages): # 活动榜单相关 [进入小时榜,未知,获小时榜第一道具奖励] elif cmd in ["ROOM_RANK", "new_anchor_reward", "HOUR_RANK_AWARDS"]: pass - # 活动相关 [活动获得的直播间入场特效,活动事件(如充能值信息),以前的高能事件,送礼抽奖活动开奖,LOL竞猜活动,LOL助力活动] - elif cmd in ["WELCOME_ACTIVITY", "ACTIVITY_EVENT", "EVENT_CMD", "BOX_LOTTERY_WIN", "LOL_ACTIVITY", "ACTIVITY_MATCH_GIFT"]: + # 活动相关 [活动获得的直播间入场特效,活动事件(如充能值信息),以前的高能事件,送礼抽奖活动开奖,LOL竞猜活动,LOL助力活动,?(不知道是啥,每个直播间都有,无论开播,每分钟发一次),?,?,冲鸭!机甲大作战相关,周星活动相关] + elif cmd in ["WELCOME_ACTIVITY", "ACTIVITY_EVENT", "EVENT_CMD", "BOX_LOTTERY_WIN", "LOL_ACTIVITY", "ACTIVITY_MATCH_GIFT", + "ACTIVITY_BANNER_RED_NOTICE_CLOSE", "ACTIVITY_BANNER_CLOSE", "DAILY_QUEST_NEWDAY", + "BOSS_ENERGY", "NOTICE_MSG_H5", "BOSS_INJURY", "BOSS_BATTLE", "ANIMATION", "BOSS_INFO", + "WEEK_STAR_CLOCK", "ROOM_BOX_MASTER", "ROOM_BOX_USER"]: pass - # 直播间信息相关 [直播间更换壁纸,直播间界面皮肤变化,许愿瓶进度变化,关注数变化,实物抽奖宝箱提醒,实物抽奖宝箱开奖] - elif cmd in ["CHANGE_ROOM_INFO", "ROOM_SKIN_MSG", "WISH_BOTTLE", "ROOM_REAL_TIME_MESSAGE_UPDATE", "BOX_ACTIVITY_START", "WIN_ACTIVITY"]: + # 直播间信息相关 [直播间更换壁纸,直播间界面皮肤变化,许愿瓶进度变化,关注数变化,直播间更名,实物抽奖宝箱提醒,实物抽奖宝箱开奖,弹幕抽奖结束] + elif cmd in ["CHANGE_ROOM_INFO", "ROOM_SKIN_MSG", "WISH_BOTTLE", "ROOM_REAL_TIME_MESSAGE_UPDATE", "ROOM_CHANGE", "BOX_ACTIVITY_START", "WIN_ACTIVITY", "DANMU_LOTTERY_END"]: 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"]: + elif cmd in ["PK_BATTLE_ENTRANCE", "PK_BATTLE_PRE", "PK_BATTLE_MATCH_TIMEOUT", "PK_BATTLE_START", "PK_BATTLE_VOTES_ADD", + "PK_BATTLE_PROCESS", "PK_BATTLE_PRO_TYPE", "PK_BATTLE_GIFT", "PK_BATTLE_END", "PK_BATTLE_RANK_CHANGE", + "PK_BATTLE_SETTLE_USER", "PK_BATTLE_SETTLE", "PK_LOTTERY_START", "ACTIVITY_BANNER_UPDATE"]: pass else: - Printer().printer(f"出现一个未知msg @[{self.area}分区]{self._roomId} {dic}", "Warning", "red") + # Printer().printer(f"出现一个未知msg @[{self.area}分区]{self._roomId} {dic}", "Warning", "red") pass diff --git a/login.py b/login.py index f3a0435..497324a 100644 --- a/login.py +++ b/login.py @@ -6,6 +6,7 @@ class login(): + auto_captcha_times = 3 def normal_login(self, username, password): # url = 'https://passport.bilibili.com/api/oauth2/login' //旧接口 @@ -29,7 +30,22 @@ def login_with_captcha(self, username, password): url = "https://passport.bilibili.com/captcha" res = s.get(url, headers=headers) tmp1 = base64.b64encode(res.content) - captcha = bilibili().cnn_captcha(tmp1) + for _ in range(login.auto_captcha_times): + try: + captcha = bilibili().cnn_captcha(tmp1) + break + except Exception: + Printer().printer("验证码识别服务器连接失败","Error","red") + login.auto_captcha_times -= 1 + else: + try: + from PIL import Image + from io import BytesIO + img = Image.open(BytesIO(res.content)) + img.show() + captcha = input('输入验证码\n').strip() + except ImportError: + Printer().printer("安装 Pillow 库后重启,以弹出验证码图片","Error","red") temp_params = 'actionKey=' + bilibili().dic_bilibili[ 'actionKey'] + '&appkey=' + bilibili().dic_bilibili['appkey'] + '&build=' + bilibili().dic_bilibili[ 'build'] + '&captcha=' + captcha + '&device=' + bilibili().dic_bilibili[ @@ -49,14 +65,19 @@ def login(self): username = str(bilibili().dic_bilibili['account']['username']) password = str(bilibili().dic_bilibili['account']['password']) if username != "": - response = bilibili().request_getkey() - value = response.json()['data'] - key = value['key'] - Hash = str(value['hash']) - username, password = bilibili().calc_name_passw(key, Hash, username, password) - response = self.normal_login(username, password) - while response.json()['code'] == -105: - response = self.login_with_captcha(username, password) + while True: + response = bilibili().request_getkey() + value = response.json()['data'] + key = value['key'] + Hash = str(value['hash']) + username, password = bilibili().calc_name_passw(key, Hash, username, password) + response = self.normal_login(username, password) + while response.json()['code'] == -105: + response = self.login_with_captcha(username, password) + if response.json()['code'] == -662: # "can't decrypt rsa password~" + Printer().printer("打码时间太长key失效,重试", "Error", "red") + continue + break try: access_key = response.json()['data']['token_info']['access_token'] cookie = (response.json()['data']['cookie_info']['cookies'])