diff --git a/main.py b/main.py index 10faef12..debf5507 100644 --- a/main.py +++ b/main.py @@ -61,7 +61,33 @@ async def web_server_thread(web_server_port): logger.info(f"Web运行在端口:{web_server_port}") logger.info(f"可以直接访问Live2D页, http://127.0.0.1:{web_server_port}/Live2D/") httpd.serve_forever() - + +""" + _oo0oo_ + o8888888o + 88" . "88 + (| -_- |) + 0\ = /0 + ___/`---'\___ + .' \\| |// '. + / \\||| : |||// \ + / _||||| -:- |||||- \ + | | \\\ - /// | | + | \_| ''\---/'' |_/ | + \ .-\__ '-' ___/-. / + ___'. .' /--.--\ `. .'___ + ."" '< `.___\_<|>_/___.' >' "". + | | : `- \`.;`\ _ /`;.`/ - ` : | | + \ \ `_. \_ __\ /__ _/ .-` / / + =====`-.____`.___ \_____/___.-`___.-'===== + `=---=' + + + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + 佛祖保佑 永不宕机 永无BUG +""" + # 点火起飞 def start_server(): @@ -3075,13 +3101,14 @@ def send(): elif platform == "youtube": import pytchat - try: + def get_video_id(): try: - video_id = config.get("room_display_id") + return config.get("room_display_id") except Exception as e: logger.error("获取直播间号失败!\n{0}".format(e)) + return None - live = pytchat.create(video_id=video_id) + def process_chat(live): while live.is_alive(): try: for c in live.get().sync_items(): @@ -3092,10 +3119,6 @@ def send(): # 闲时计数清零 idle_time_auto_clear("comment") - # chat_author makes the chat look like this: "Nightbot: Hello". So the assistant can respond to the user's name - # chat = '[' + c.author.name + ']: ' + chat_raw - # logger.info(chat) - content = chat_raw # 获取弹幕内容 username = c.author.name # 获取发送弹幕的用户昵称 @@ -3114,8 +3137,37 @@ def send(): logger.error(traceback.format_exc()) logger.error("Error receiving chat: {0}".format(e)) my_handle.abnormal_alarm_handle("platform") + break # 退出内部while循环以触发重连机制 + + try: + reconnect_attempts = 0 + last_reconnect_time = None + + while True: + video_id = get_video_id() + if video_id is None: + break + + live = pytchat.create(video_id=video_id) + process_chat(live) + + current_time = time.time() + # 如果重连间隔只有30s内,那就只有3次,如果间隔大于30s,那就无限重连 + if last_reconnect_time and (current_time - last_reconnect_time < 30): + reconnect_attempts += 1 + if reconnect_attempts >= 3: + logger.error('重连失败次数已达上限,退出程序...') + break + logger.warning(f'连接已关闭,间隔小于30秒,尝试重新连接 ({reconnect_attempts}/3)...') + else: + reconnect_attempts = 0 # 重置重连次数 + logger.warning('连接已关闭,尝试重新连接...') + + last_reconnect_time = current_time + except KeyboardInterrupt: logger.warning('程序被强行退出') + finally: logger.warning('关闭连接...') os._exit(0) diff --git a/utils/my_handle.py b/utils/my_handle.py index db264682..33fb3da0 100644 --- a/utils/my_handle.py +++ b/utils/my_handle.py @@ -2954,6 +2954,23 @@ def schedule_handle(self, data): logger.error(traceback.format_exc()) return None + """ +..................................................................................................................... +..............:*.....................................-*=........:+-......................-*+......................... +..............:*%+.:#%%%%%%%%%%%%*..:+++++++-........=@+........+%=-++++++***##%%=......=%@*+++++++++++:............. +.............--.-+:............-@*..=%#+++*@*........=@+.......+%+.:=+++=+%#-:........-#%%%+-------=#%#:............. +.............%#.......+#.......-@*..=%+...-@*+@@@@@@@@@@@@+...=%*:.......-#*:.......:*%*:.+%*-...-*%*-............... +.............%#..::::-*#-:::::.-@*..=%+...-@*........=@+.....+%@+........-#*:.......+*:.....*%@%%%*.................. +.............%#.-****#@%*****+.-%*..=%*:::=@*.-*=....=@+....+%%%+........-##:..........:-*%%%%+-*%%%%#+-::........... +.............%#.....:@@#:......-%*..=%*---+@*.:*%=...=@+...+@==#+.+%@@@@@@@@@@@@@@**%@@#=...#@:.......=*%%........... +.............%#....+%+*%#%*:...-%*..=%+...-@*..-#%=..=@+......=#+........-##:........+******%%#********:............. +.............%#..-%#:.*#:.=%#=.-%*..=%+...-@*...:-:..=@+......=#+........-#*:........::::::*%=-::::::#%:............. +.............%#-#%-...*#....=+:-%*..=%*---+@*........=@+......=#+........-#*:.............=%*:.......#@.............. +.............%#.:.....+#.......-%*..=%#+++*@*........=@+......=#+........-#*:...........-#%+.........#@.............. +.............%#.......::..:====*%+..-*=...:*=...-*++*%#-......=#+..+##############-.-=*%#+:...=++==+%%-.............. +.............*+...........:****+:...............:----:........-*=...................=*=........--==-:................ +..................................................................................................................... + """ # 闲时任务处理 def idle_time_task_handle(self, data): try: