From 23ae605136031de5d603c9fcdfb2511e2dc28139 Mon Sep 17 00:00:00 2001 From: yawwwwwn <40122222+yawwwwwn@users.noreply.github.com> Date: Thu, 5 Sep 2019 02:02:45 +0800 Subject: [PATCH] Optimize draw lottery MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 因为最近出现了连续两个空id导致提前结束实物抽奖的获取,故优化一下这里的跳出逻辑。 B站删除哔考分区导致持续获取被风控,故增加分区房间获取不到时的简易处理。 --- MultiRoom.py | 7 ++++++- OnlineHeart.py | 14 ++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/MultiRoom.py b/MultiRoom.py index 578fb9b..7c23f52 100644 --- a/MultiRoom.py +++ b/MultiRoom.py @@ -18,6 +18,10 @@ async def area2room(area_id): response = await bilibili().bili_section_get(url) json_response = await response.json(content_type=None) checklen = len(json_response['data']) + if not checklen: + Printer().printer(f"{area_id}号分区当前无开播房间,5分钟后重新获取", "Error", "red") + await asyncio.sleep(300) + continue rand_num = random.randint(0, checklen-1) new_area_id = json_response['data'][rand_num]['parent_id'] if not new_area_id == int(area_id): @@ -28,7 +32,8 @@ async def area2room(area_id): new_area = str(new_area_id) + json_response['data'][rand_num]['parent_name'] return [area_room, new_area] else: - Printer().printer("检测到获取房间未开播,立即尝试重新获取", "Error", "red") + Printer().printer("检测到获取房间未开播,1秒后尝试重新获取", "Error", "red") + await asyncio.sleep(1) except Exception as e: Printer().printer(f"获取房间列表失败,5s后进行下次尝试 {repr(e)}", "Error", "red") await asyncio.sleep(5) diff --git a/OnlineHeart.py b/OnlineHeart.py index 48fbb45..76a32bc 100644 --- a/OnlineHeart.py +++ b/OnlineHeart.py @@ -5,6 +5,7 @@ import traceback import datetime import asyncio +import queue from printer import Printer @@ -68,17 +69,19 @@ async def guard_lottery(self): Printer().printer( f"房间 {OriginRoomId} 编号 {GuardId} 的上船亲密度领取出错: {json_response2}", "Error", "red") - + await asyncio.sleep(0.2) 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 + former_lottery = queue.Queue(maxsize=4) + [former_lottery.put(True) for _ in range(4)] for i in range(390, 600): response = await bilibili().get_lotterylist(i) json_response = await response.json() + former_lottery.get() + former_lottery.put(not json_response['code']) if json_response['code'] == 0: - last_lottery = 0 title = json_response['data']['title'] check = len(json_response['data']['typeB']) for g in range(check): @@ -98,10 +101,9 @@ async def draw_lottery(self): json_response1 = await response1.json(content_type=None) Printer().printer(f"参与『{title}>>>{jp_list}』抽奖回显: {json_response1}", "Lottery", "cyan") else: - if not last_lottery == 0: # 因为有中途暂时空一个-400的情况 + if not any(former_lottery.queue): # 检查最近4个活动id是否都-400 break - else: - last_lottery = json_response['code'] + await asyncio.sleep(0.2) async def run(self): while 1: