From cc298b02a44e288320cefa7f5fb4b019b43e0f0a Mon Sep 17 00:00:00 2001 From: TZFC Date: Thu, 11 Jan 2024 02:18:26 -0500 Subject: [PATCH] perform checkin on DM_INTRERACTION keyword --- EventHandler/OTHER_handler.py | 33 +++++++++++++++++++++++++++++++++ Utils/SendReportCheckin.py | 4 ++-- master.py | 2 +- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/EventHandler/OTHER_handler.py b/EventHandler/OTHER_handler.py index ff018ee..89b6f48 100644 --- a/EventHandler/OTHER_handler.py +++ b/EventHandler/OTHER_handler.py @@ -1,8 +1,13 @@ +import asyncio from datetime import datetime from json import loads +from Utils.Checkin import record_checkin from Utils.EVENT_IDX import * from Utils.RecordDanmaku import record_danmaku +from Utils.SendReportCheckin import send_report_checkin +from Utils.TopCheckin import get_top_checkin +from web.UpdatePage import update_page async def handle_dm_interaction(event, database, master_config, room_info): @@ -11,6 +16,34 @@ async def handle_dm_interaction(event, database, master_config, room_info): data = loads(event['data']['data']['data']) content = "--" + data['combo'][0]['content'] id = event['data']['data']['id'] + if any(live_end_word in data['combo'][0]['content'] for live_end_word in {"晚安", "午安", "拜拜"}): + info = await room_info['live_room'].get_room_info() + live_status = info['room_info']['live_status'] + if live_status == LIVE_STATUS_STREAMING \ + and room_info['room_config']["feature_flags"]["checkin"] \ + and not room_info['state']['pre-checkin']: + async with asyncio.TaskGroup() as tg: + with database.cursor() as cursor: + sql = "SELECT start FROM liveTime WHERE room_id = %s AND end IS NULL AND summary IS NULL" + val = (room_id,) + cursor.execute(sql, val) + start_time = cursor.fetchall()[0][0] + # 统计直播间发言人 + await record_checkin(start_time=start_time, + end_time=datetime.fromtimestamp(event['data']["info"][TIMESTAMP_IDX]['ts']), + master=room_info['room_config']['master'], + room_id=room_id, + checkin_days=room_info['room_config']['checkin_days'], + database=database) + top_uid_name_count = await get_top_checkin(master_uid=room_info['master_credential'].dedeuserid, + room_id=room_id, database=database) + tg.create_task(send_report_checkin(live_room=room_info['live_room'], + top_uid_username_count=top_uid_name_count)) + tg.create_task(update_page(target=f"/var/www/html/{room_id}.html", + checkin_days=room_info['room_config']['checkin_days'], + content=top_uid_name_count)) + room_info['state']['pre-checkin'] = True + return await record_danmaku(name="他们都在说", received_uid=0, time=datetime.now().replace(microsecond=0), medal_room=room_id, medal_level=99, text=content, message_type=TEXT_TYPE, room_id=room_id, danmu_id=id, database=database) diff --git a/Utils/SendReportCheckin.py b/Utils/SendReportCheckin.py index 4dfcc66..07c62fc 100644 --- a/Utils/SendReportCheckin.py +++ b/Utils/SendReportCheckin.py @@ -8,6 +8,6 @@ async def send_report_checkin(live_room, top_uid_username_count): if rank >= 10: break text = f"第{rank + 1}名 {username} 打卡{count}次" - for chunk in range(len(text)//20+1): + for chunk in range(len(text) // 20 + 1): await asyncio.sleep(5) - await live_room.send_danmaku(Danmaku(text[chunk*20:(chunk+1)*20]), reply_mid=uid) + await live_room.send_danmaku(Danmaku(text[chunk * 20:(chunk + 1) * 20]), reply_mid=uid) diff --git a/master.py b/master.py index c7b0f5c..5a6288a 100644 --- a/master.py +++ b/master.py @@ -81,7 +81,7 @@ async def live_end(event): @__live_danmaku.on("DISCONNECT") async def disconnect(event): __event_room_id = event['room_display_id'] - await refresh_credentials(masters=[roomInfos[__event_room_id]['room_config']['master'],], + await refresh_credentials(masters=[roomInfos[__event_room_id]['room_config']['master'], ], room_infos=roomInfos, database=mydb)