Skip to content

Commit

Permalink
Merge pull request #929 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
youtube监听增加重连机制,短时间内多次重连判断为无法连接,超过30s则无限重连
  • Loading branch information
Ikaros-521 authored Jul 20, 2024
2 parents 804ae60 + 213d74f commit 196eb53
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
68 changes: 60 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand All @@ -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 # 获取发送弹幕的用户昵称

Expand All @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions utils/my_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2954,6 +2954,23 @@ def schedule_handle(self, data):
logger.error(traceback.format_exc())
return None

"""
.....................................................................................................................
..............:*.....................................-*=........:+-......................-*+.........................
..............:*%+.:#%%%%%%%%%%%%*..:+++++++-........=@+........+%=-++++++***##%%=......=%@*+++++++++++:.............
.............--.-+:............-@*..=%#+++*@*........=@+.......+%+.:=+++=+%#-:........-#%%%+-------=#%#:.............
.............%#.......+#.......-@*..=%+...-@*+@@@@@@@@@@@@+...=%*:.......-#*:.......:*%*:.+%*-...-*%*-...............
.............%#..::::-*#-:::::.-@*..=%+...-@*........=@+.....+%@+........-#*:.......+*:.....*%@%%%*..................
.............%#.-****#@%*****+.-%*..=%*:::=@*.-*=....=@+....+%%%+........-##:..........:-*%%%%+-*%%%%#+-::...........
.............%#.....:@@#:......-%*..=%*---+@*.:*%=...=@+...+@==#+.+%@@@@@@@@@@@@@@**%@@#=...#@:.......=*%%...........
.............%#....+%+*%#%*:...-%*..=%+...-@*..-#%=..=@+......=#+........-##:........+******%%#********:.............
.............%#..-%#:.*#:.=%#=.-%*..=%+...-@*...:-:..=@+......=#+........-#*:........::::::*%=-::::::#%:.............
.............%#-#%-...*#....=+:-%*..=%*---+@*........=@+......=#+........-#*:.............=%*:.......#@..............
.............%#.:.....+#.......-%*..=%#+++*@*........=@+......=#+........-#*:...........-#%+.........#@..............
.............%#.......::..:====*%+..-*=...:*=...-*++*%#-......=#+..+##############-.-=*%#+:...=++==+%%-..............
.............*+...........:****+:...............:----:........-*=...................=*=........--==-:................
.....................................................................................................................
"""
# 闲时任务处理
def idle_time_task_handle(self, data):
try:
Expand Down

0 comments on commit 196eb53

Please sign in to comment.