async def connect(self): danmuji = bilibiliClient(self.printer, self.bilibili, self.api) task1 = asyncio.ensure_future(danmuji.connectServer()) # print('task1启动') task2 = asyncio.ensure_future(danmuji.HeartbeatLoop()) # print('task2启动') self.tasks[self.bilibili.roomid] = [task1, task2] while True: await asyncio.sleep(5) for roomid in self.tasks: item = self.tasks[roomid] task1 = item[0] task2 = item[1] if task1.done() == True or task2.done() == True: # print('task断线') if task1.done() == False: # print('仅task2断线') task1.cancel() if task2.done() == False: # print('仅task1断线') task2.cancel() print('# 重新连接直播间 %s' % roomid) with open("log.txt", "a+") as f: f.write("reconnect success!!!!!") danmuji = bilibiliClient(self.printer, self.bilibili, self.api) task11 = asyncio.ensure_future(danmuji.connectServer()) task22 = asyncio.ensure_future(danmuji.HeartbeatLoop()) self.tasks[roomid] = [task11, task22]
async def create(self): tmp = MultiRoom().get_all() for i in range(len(tmp)): connect.roomids.append(tmp[i][0]) for n in range(len(tmp)): connect.area_name.append(tmp[n][1]) for roomid,area_name in zip(connect.roomids,connect.area_name): self.danmuji = bilibiliClient(roomid,area_name) task1 = asyncio.ensure_future(self.danmuji.connectServer()) task2 = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) connect.tasks[roomid] = [task1, task2] while True: await asyncio.sleep(10) for roomid in connect.tasks: item = connect.tasks[roomid] task1 = item[0] task2 = item[1] if task1.done() == True or task2.done() == True: if task1.done() == False: task1.cancel() if task2.done() == False: task2.cancel() danmuji = bilibiliClient(roomid,connect.area_name) task11 = asyncio.ensure_future(danmuji.connectServer()) task22 = asyncio.ensure_future(danmuji.HeartbeatLoop()) connect.tasks[roomid] = [task11, task22]
async def create(self): tmp = await MultiRoom().get_all() for i in range(len(tmp)): connect.roomids.append(tmp[i][0]) for n in range(len(tmp)): connect.area_name.append(tmp[n][1]) for roomid, area_name in zip(connect.roomids, connect.area_name): self.danmuji = bilibiliClient(roomid, area_name) task1 = asyncio.ensure_future(self.danmuji.connectServer()) task2 = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) connect.tasks[roomid] = [task1, task2] while True: await asyncio.sleep(10) for roomid in connect.tasks: item = connect.tasks[roomid] task1 = item[0] task2 = item[1] if task1.done() == True or task2.done() == True: area_name = connect.area_name[connect.roomids.index( roomid)] Printer().printer( f"[{self.area_name}] 房间 {self._roomId} 任务出现异常", "Info", "green") if task1.done() == False: task1.cancel() if task2.done() == False: task2.cancel() [ckd_roomid, ckd_area_name ] = await MultiRoom().check_state(roomid=roomid, area=area_name) if not ckd_roomid == roomid: connect.roomids.remove(roomid) connect.area_name.remove(area_name) del connect.tasks[roomid] connect.roomids.append(ckd_roomid) connect.area_name.append(ckd_area_name) Printer().printer( f"更新四个分区房间{connect.roomids}[{connect.area_name}]", "Info", "green") danmuji = bilibiliClient(ckd_roomid, ckd_area_name) task11 = asyncio.ensure_future(danmuji.connectServer()) task22 = asyncio.ensure_future(danmuji.HeartbeatLoop()) connect.tasks[ckd_roomid] = [task11, task22] else: # Printer().printer(f"[{self.area_name}] 房间 {self._roomId} 任务保持正常", "Info", "green") pass
async def run(self): while True: print('# 正在启动弹幕姬') time_start = int(utils.CurrentTime()) self.danmuji = bilibiliClient() task_connect = asyncio.ensure_future(self.danmuji.connectServer()) connect_results = await asyncio.gather(task_connect) # print(connect_results) if all(connect_results): pass else: continue task_main = asyncio.ensure_future( self.danmuji.ReceiveMessageLoop()) task_heartbeat = asyncio.ensure_future( self.danmuji.HeartbeatLoop()) finished, pending = await asyncio.wait( [task_main, task_heartbeat], return_when=asyncio.FIRST_COMPLETED) print('# 弹幕姬异常或主动断开,处理完剩余信息后重连') self.danmuji.connected = False time_end = int(utils.CurrentTime()) if not task_heartbeat.done(): task_heartbeat.cancel() print('# 弹幕主程序退出,立即取消心跳模块') else: await asyncio.wait(pending) print('# 弹幕心跳模块退出,主程序剩余任务处理完毕') if time_end - time_start < 5: print('# 当前网络不稳定,为避免频繁不必要尝试,将自动在5秒后重试') await asyncio.sleep(5)
async def connect(self): while True: print('# 正在启动弹幕姬') time_start = int(utils.CurrentTime()) self.danmuji = bilibiliClient() task_main = asyncio.ensure_future(self.danmuji.connectServer()) task_heartbeat = asyncio.ensure_future( self.danmuji.HeartbeatLoop()) finished, pending = await asyncio.wait( [task_main, task_heartbeat], return_when=asyncio.FIRST_COMPLETED) print('# 弹幕姬异常或主动断开,处理完剩余信息后重连') self.danmuji.connected = False time_end = int(utils.CurrentTime()) if task_heartbeat.done() == False: task_heartbeat.cancel() print('# 弹幕主程序退出,立即取消心跳模块') else: await asyncio.wait(pending) print('# 弹幕心跳模块退出,主程序剩余任务处理完毕') # 类似于lock功能,当reconnect模块使用时,禁止重启,直到reconnect模块修改完毕) while self.tag_reconnect: await asyncio.sleep(0.5) print('pending') if time_end - time_start < 5: print('# 当前网络不稳定,为避免频繁不必要尝试,将自动在5秒后重试') await asyncio.sleep(5)
async def run(self): self.danmuji = bilibiliClient(self.roomid, self.areaid) while True: self.danmuji.roomid = await get_one(self.areaid) printer.info(['# 正在启动抽奖监控弹幕姬'], True) time_start = int(utils.CurrentTime()) connect_results = await self.danmuji.connectServer() # print(connect_results) if not connect_results: continue task_main = asyncio.ensure_future(self.danmuji.ReceiveMessageLoop()) task_heartbeat = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) task_checkarea = asyncio.ensure_future(self.danmuji.CheckArea()) finished, pending = await asyncio.wait([task_main, task_heartbeat, task_checkarea], return_when=asyncio.FIRST_COMPLETED) printer.info([f'{self.areaid}号弹幕姬异常或主动断开,正在处理剩余信息'], True) time_end = int(utils.CurrentTime()) if not task_heartbeat.done(): task_heartbeat.cancel() if not task_checkarea.done(): task_checkarea.cancel() task_terminate = asyncio.ensure_future(self.danmuji.close_connection()) await asyncio.wait(pending) await asyncio.wait([task_terminate]) printer.info([f'{self.areaid}号弹幕姬退出,剩余任务处理完毕'], True) if time_end - time_start < 5: print('# 当前网络不稳定,为避免频繁不必要尝试,将自动在5秒后重试') await asyncio.sleep(5)
async def recreate(self, area_name): try: roomid = connect.roomids[connect.area_name.index(area_name)] item = connect.tasks[roomid] task1 = item[0] task2 = item[1] task1.cancel() task2.cancel() connect.roomids.remove(roomid) connect.area_name.remove(area_name) del connect.tasks[roomid] [new_roomid, new_area_name] = await MultiRoom().check_state(area=area_name) connect.roomids.append(new_roomid) connect.area_name.append(new_area_name) Printer().printer(f"更新四个分区房间{connect.roomids} {connect.area_name}", "Info", "green") self.danmuji = bilibiliClient(new_roomid, new_area_name) task11 = asyncio.ensure_future(self.danmuji.connectServer()) task21 = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) connect.tasks[new_roomid] = [task11, task21] except Exception: traceback.print_exc()
async def recreate(self): try: for roomid in connect.tasks: item = connect.tasks[roomid] task1 = item[0] task2 = item[1] task1.cancel() task2.cancel() connect.tasks.clear() tmp = MultiRoom().get_all() connect.roomids.clear() connect.area_name.clear() for i in range(len(tmp)): connect.roomids.append(tmp[i][0]) for n in range(len(tmp)): connect.area_name.append(tmp[n][1]) Printer().printer(f"获取新的四个分区房间{connect.roomids}[{connect.area_name}]","Info","green") for roomid, area_name in zip(connect.roomids, connect.area_name): self.danmuji = bilibiliClient(roomid,area_name) task1 = asyncio.ensure_future(self.danmuji.connectServer()) task2 = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) connect.tasks[roomid] = [task1, task2] except Exception as e: print(e) traceback.print_exc()
async def run(self): while True: self.roomid = await get_one(self.areaid) print('# 正在启动抽奖监控弹幕姬') time_start = int(utils.CurrentTime()) self.danmuji = bilibiliClient(self.roomid, self.areaid) connect_results = await self.danmuji.connectServer() # print(connect_results) if not connect_results: continue task_main = asyncio.ensure_future( self.danmuji.ReceiveMessageLoop()) task_heartbeat = asyncio.ensure_future( self.danmuji.HeartbeatLoop()) task_checkarea = asyncio.ensure_future(self.danmuji.CheckArea()) finished, pending = await asyncio.wait( [task_main, task_heartbeat, task_checkarea], return_when=asyncio.FIRST_COMPLETED) print('# 弹幕姬异常或主动断开,处理完剩余信息后重连') self.danmuji.connected = False time_end = int(utils.CurrentTime()) if not task_heartbeat.done(): task_heartbeat.cancel() await self.danmuji.close_connection() print('# 弹幕主程序退出,立即取消心跳模块') else: await asyncio.wait(pending) print('# 弹幕心跳模块退出,主程序剩余任务处理完毕') if time_end - time_start < 5: print('# 当前网络不稳定,为避免频繁不必要尝试,将自动在5秒后重试') await asyncio.sleep(5)
async def connect(self): danmuji = bilibiliClient(self.printer) task1 = asyncio.ensure_future(danmuji.connectServer()) task2 = asyncio.ensure_future(danmuji.HeartbeatLoop()) self.tasks = [task1, task2] while True: await asyncio.sleep(5) task1 = self.tasks[0] task2 = self.tasks[1] if task1.done() == True or task2.done() == True: print('Connect Fail') if task1.done() == False: task1.cancel() if task2.done() == False: task2.cancel() print('# 重新连接直播间') danmuji = bilibiliClient(self.printer) task11 = asyncio.ensure_future(danmuji.connectServer()) task22 = asyncio.ensure_future(danmuji.HeartbeatLoop()) self.tasks = [task11, task22]
async def connect(self): roomid = 23058 danmuji = bilibiliClient() task1 = asyncio.ensure_future(danmuji.connectServer()) task2 = asyncio.ensure_future(danmuji.HeartbeatLoop()) self.tasks[roomid] = [task1, task2] while True: await asyncio.sleep(10) for roomid in self.tasks: item = self.tasks[roomid] task1 = item[0] task2 = item[1] if task1.done() == True or task2.done() == True: if task1.done() == False: task1.cancel() if task2.done() == False: task2.cancel() print ('# 重新连接直播间 %s' % roomid) with open("log.txt","a+")as f: f.write("reconnect success!!!!!") danmuji = bilibiliClient() task11 = asyncio.ensure_future(danmuji.connectServer()) task22 = asyncio.ensure_future(danmuji.HeartbeatLoop()) self.tasks[roomid] = [task11, task22]
async def create(self): connect.roomids = MultiRoom().get_all() for roomid in connect.roomids: self.danmuji = bilibiliClient(roomid) task1 = asyncio.ensure_future(self.danmuji.connectServer()) task2 = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) connect.tasks[roomid] = [task1, task2] while True: await asyncio.sleep(10) for roomid in connect.tasks: item = connect.tasks[roomid] task1 = item[0] task2 = item[1] if task1.done() == True or task2.done() == True: if task1.done() == False: task1.cancel() if task2.done() == False: task2.cancel() danmuji = bilibiliClient(roomid) task11 = asyncio.ensure_future(danmuji.connectServer()) task22 = asyncio.ensure_future(danmuji.HeartbeatLoop()) connect.tasks[roomid] = [task11, task22]
async def recreate(self): for roomid in connect.tasks: item = connect.tasks[roomid] task1 = item[0] task2 = item[1] task1.cancel() task2.cancel() connect.tasks.clear() connect.roomids = MultiRoom().get_all() Printer().printlist_append(['join_lottery', '', 'user', "获取新的四个分区房间{0}".format(connect.roomids)], True) for roomid in connect.roomids: self.danmuji = bilibiliClient(roomid) task1 = asyncio.ensure_future(self.danmuji.connectServer()) task2 = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) connect.tasks[roomid] = [task1, task2]
async def create(self): area_list = await MultiRoom.get_area_list() tmp = await MultiRoom.get_all(area_list) # 新的战疫分区直播间实际上没有弹幕区 tmp = [x for x in tmp if '战疫' not in x[1]] for i in range(len(tmp)): connect.roomids.append(tmp[i][0]) for n in range(len(tmp)): connect.areas.append(tmp[n][1]) Printer().printer(f"获取到分区列表: {connect.areas}", "Info", "green") ckd_area_list = [int(area[:1]) for area in connect.areas] Statistics().adjust_basis(ckd_area_list) init_time = time.time() for area in connect.areas: self.check_time[area] = init_time for roomid, area in zip(connect.roomids, connect.areas): self.danmuji = bilibiliClient(roomid, area) task1 = asyncio.ensure_future(self.danmuji.connectServer()) task2 = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) connect.tasks[roomid] = [task1, task2] while True: await asyncio.sleep(10) try: for roomid in list(connect.tasks): item = connect.tasks.get(roomid, None) if (item is None) or (not len(item)): Printer().printer(f"房间 {roomid} 任务已被清理,跳过", "Info", "green") continue task1 = item[0] task2 = item[1] if task1.done() == True or task2.done() == True: area = connect.areas[connect.roomids.index(roomid)] Printer().printer(f"[{area}分区] 房间 {roomid} 任务出现异常", "Info", "green") await self.check_area(roomid=roomid, area=area, mandatory_recreate=True) else: # Printer().printer(f"[{area}分区] 房间 {roomid} 任务保持正常", "Info", "green") pass except Exception: Printer().printer(traceback.format_exc(), "Error", "red")
async def recreate(self, area, new_roomid=None): if area in self.handle_area: Printer().printer(f"[{area}分区] 重连任务已在处理", "Info", "green") return else: self.handle_area.append(area) # Printer().printer(f"[{area}分区] 重连任务开始处理", "Info", "green") try: old_roomid = connect.roomids[connect.areas.index(area)] item = connect.tasks[old_roomid] task1 = item[0] task2 = item[1] if not task1.done(): task1.cancel() if not task2.done(): task2.cancel() connect.tasks[old_roomid] = [] if new_roomid is None: self.check_time[area[:1]] = time.time() [new_roomid, new_area] = await MultiRoom.check_state(area) self.check_time[area[:1]] = time.time() else: new_area = area if not new_roomid == old_roomid: connect.roomids.remove(old_roomid) connect.areas.remove(area) del connect.tasks[old_roomid] connect.roomids.append(new_roomid) connect.areas.append(new_area) connect.tasks[new_roomid] = [] Printer().printer(f"更新监听房间列表{connect.roomids} {connect.areas}", "Info", "green") self.danmuji = bilibiliClient(new_roomid, new_area) task11 = asyncio.ensure_future(self.danmuji.connectServer()) task21 = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) connect.tasks[new_roomid] = [task11, task21] except Exception: Printer().printer(traceback.format_exc(), "Error", "red") # Printer().printer(f"[{area}分区] 重连任务处理完毕", "Info", "green") self.handle_area.remove(area)
async def recreate(self, area_name, new_roomid=None): if area_name in self.handle_area: Printer().printer(f"[{area_name}] 重连任务已在处理", "Info", "green") return else: self.handle_area.append(area_name) # Printer().printer(f"[{area_name}] 重连任务开始处理", "Info", "green") try: old_roomid = connect.roomids[connect.area_name.index(area_name)] item = connect.tasks[old_roomid] task1 = item[0] task2 = item[1] if not task1.done(): task1.cancel() if not task2.done(): task2.cancel() connect.tasks[old_roomid] = [] if new_roomid is None: [new_roomid, new_area_name] = await MultiRoom().check_state(area_name) else: new_area_name = area_name if not new_roomid == old_roomid: connect.roomids.remove(old_roomid) connect.area_name.remove(area_name) del connect.tasks[old_roomid] connect.roomids.append(new_roomid) connect.area_name.append(area_name) connect.tasks[new_roomid] = [] Printer().printer( f"更新监听房间列表{connect.roomids} {connect.area_name}", "Info", "green") self.danmuji = bilibiliClient(new_roomid, new_area_name) task11 = asyncio.ensure_future(self.danmuji.connectServer()) task21 = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) connect.tasks[new_roomid] = [task11, task21] except Exception: traceback.print_exc() # Printer().printer(f"[{area_name}] 重连任务处理完毕", "Info", "green") self.handle_area.remove(area_name)
async def create(self): area_list = await MultiRoom.get_area_list() tmp = await MultiRoom.get_all(area_list) init_time = time.time() for area_id in area_list: self.check_time[str(area_id)] = init_time for i in range(len(tmp)): connect.roomids.append(tmp[i][0]) for n in range(len(tmp)): connect.areas.append(tmp[n][1]) for roomid, area in zip(connect.roomids, connect.areas): self.danmuji = bilibiliClient(roomid, area) task1 = asyncio.ensure_future(self.danmuji.connectServer()) task2 = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) connect.tasks[roomid] = [task1, task2] while True: await asyncio.sleep(10 + random.uniform(0.1, 10)) try: for roomid in list(connect.tasks): item = connect.tasks.get(roomid, None) if (item is None) or (not len(item)): Printer().printer(f"房间 {roomid} 任务已被清理,跳过", "Info", "green") continue task1 = item[0] task2 = item[1] if task1.done() == True or task2.done() == True: area = connect.areas[connect.roomids.index(roomid)] Printer().printer(f"[{area}分区] 房间 {roomid} 任务出现异常", "Info", "green") await self.check_area(roomid=roomid, area=area, mandatory_recreate=True) else: # Printer().printer(f"[{area}分区] 房间 {roomid} 任务保持正常", "Info", "green") pass except Exception: Printer().printer(traceback.format_exc(), "Error", "red")
async def create(self): tmp = await MultiRoom().get_all() for i in range(len(tmp)): connect.roomids.append(tmp[i][0]) for n in range(len(tmp)): connect.area_name.append(tmp[n][1]) for roomid, area_name in zip(connect.roomids, connect.area_name): self.danmuji = bilibiliClient(roomid, area_name) task1 = asyncio.ensure_future(self.danmuji.connectServer()) task2 = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) connect.tasks[roomid] = [task1, task2] while True: await asyncio.sleep(10) try: for roomid in list(connect.tasks): item = connect.tasks.get(roomid, None) if (item is None) or (not len(item)): Printer().printer(f"房间 {roomid} 任务已被清理,跳过", "Info", "green") continue task1 = item[0] task2 = item[1] if task1.done() == True or task2.done() == True: area_name = connect.area_name[connect.roomids.index( roomid)] Printer().printer(f"[{area_name}] 房间 {roomid} 任务出现异常", "Info", "green") [ckd_roomid, ckd_area_name ] = await MultiRoom().check_state(roomid=roomid, area=area_name) await self.recreate(new_roomid=ckd_roomid, area_name=area_name) else: # Printer().printer(f"[{area_name}] 房间 {roomid} 任务保持正常", "Info", "green") pass except Exception: traceback.print_exc()