This commit is contained in:
2026-03-14 07:57:27 +08:00
parent 17947229a1
commit 0e24b9026b

View File

@@ -9,24 +9,22 @@ import db
import re
path = "logs"
room_id_re = re.compile(rf"{path}/(\d+).jsonl$")
room_id_re = re.compile(rf"{path}/(\d+)\.jsonl$")
notify_qq_group = {
"12571885": lambda msg: notify_group(msg, ["831867573", "138981147"]),
"147482": lambda msg: notify_group(msg, ["175545447"]),
"20571": ["891117762"],
"12571885": lambda room, is_online: notify_group(room, ["831867573", "138981147"], is_online),
"147482": lambda room, is_online: notify_group(room, ["175545447"], is_online),
"20571": lambda room, is_online: notify_group(room, ["891117762"], is_online),
}
class Room:
position = 0
# 0 unkonwn, 1 live, 2 pending
state = 0
state_changed = False
def __init__(self, room_id: str) -> None:
self.room_id = room_id
self.db_room = db.get_room(room_id)
self.position = 0
self.state = 0
self.state_changed = False
def reset(self):
self.position = 0
@@ -41,30 +39,31 @@ class Room:
_rooms: dict[str, Room] = {}
def notify_group(room: Room, groups: list[str]):
def notify_group(room: Room, groups: list[str], is_online: bool):
status = "开锅了" if is_online else "下锅了"
msg = "\n".join([
f"{room.db_room.liver_name} 下锅了",
f"{room.db_room.liver_name} {status}",
f"https://live.bilibili.com/{room.room_id}"
])
for qq_group_id in groups:
seng_qq_group_msg(qq_group_id, msg)
send_qq_group_msg(qq_group_id, msg)
def notify_group_and_mc(room: Room, groups: list[str]):
def notify_group_and_mc(room: Room, groups: list[str], is_online: bool):
notify_group(room, groups, is_online)
try:
msg = "\n".join([
f"{room.db_room.liver_name} 开锅了",
f"{room.db_room.liver_name} {'开锅了' if is_online else '下锅了'}",
f"https://live.bilibili.com/{room.room_id}"
])
try:
requests.post("http://mc1:5000/msg", json={"msg": msg})
except BaseException as e:
print(f"notify mc error: {e}")
for qq_group_id in groups:
seng_qq_group_msg(qq_group_id, msg)
def get_room(src_path):
room_id = room_id_re.findall(src_path)[0]
src_path_str = str(src_path)
room_id = room_id_re.findall(src_path_str)[0]
room = _rooms.get(room_id, None)
if room is None:
room = Room(room_id)
@@ -78,25 +77,27 @@ class MyHandler(FileSystemEventHandler):
if event.is_directory:
return
if not room_id_re.match(event.src_path):
src_path_str = str(event.src_path)
if not room_id_re.search(src_path_str):
return
room: Room = get_room(event.src_path)
room: Room = get_room(src_path_str)
try:
with open(event.src_path, "r", encoding="utf-8") as f:
if room.position > os.path.getsize(event.src_path):
file_size = os.path.getsize(event.src_path)
if room.position > file_size:
room.reset()
f.seek(room.position)
room.position = os.path.getsize(event.src_path)
for line in f:
try:
data = json.loads(line)
except json.decoder.JSONDecodeError:
continue
self._handle_data(room, data)
room.position = os.path.getsize(event.src_path)
except UnicodeDecodeError:
pass
@@ -106,11 +107,11 @@ class MyHandler(FileSystemEventHandler):
if event.is_directory:
return
if not room_id_re.match(event.src_path):
src_path_str = str(event.src_path)
if not room_id_re.search(src_path_str):
return
room: Room = get_room(event.src_path)
room.position = os.path.getsize(event.src_path)
room: Room = get_room(src_path_str)
try:
with open(event.src_path, "r", encoding="utf-8") as f:
@@ -119,7 +120,9 @@ class MyHandler(FileSystemEventHandler):
data = json.loads(line)
except json.decoder.JSONDecodeError:
continue
self._handle_data(data)
self._handle_data(room, data)
room.position = os.path.getsize(event.src_path)
except UnicodeDecodeError:
pass
@@ -129,10 +132,12 @@ class MyHandler(FileSystemEventHandler):
if event.is_directory:
return
if not room_id_re.match(event.src_path):
src_path_str = str(event.src_path)
if not room_id_re.search(src_path_str):
return
room = _rooms.get(event.src_path, Room())
room_id = room_id_re.findall(src_path_str)[0]
room = _rooms.get(room_id, Room(room_id))
room.reset()
def _handle_data(self, room: Room, data):
@@ -159,32 +164,44 @@ class MyHandler(FileSystemEventHandler):
f"{room.db_room.liver_name} 开锅了!",
f"https://live.bilibili.com/{room.room_id}"
])
try:
requests.post("http://turntf:18846/notify", json={"msg": msg})
except BaseException as e:
print(f"notify turntf error: {e}")
for notify_group in notify_qq_group.get(room.room_id, []):
seng_qq_group_msg(notify_group, msg)
notify_func = notify_qq_group.get(room.room_id)
if notify_func:
notify_func(room, True)
elif room.state == 2:
msg = "\n".join([
f"{room.db_room.liver_name} 下锅了!",
f"https://live.bilibili.com/{room.room_id}"
])
try:
requests.post("http://turntf:18846/notify", json={"msg": msg})
except BaseException as e:
print(f"notify turntf error: {e}")
notify_qq_group.get(room.room_id, lambda msg: None)(room)
notify_func = notify_qq_group.get(room.room_id)
if notify_func:
notify_func(room, False)
def seng_qq_group_msg(group_id: str, msg: str):
def send_qq_group_msg(group_id: str, msg: str):
try:
requests.post("http://napcat:3000/send_group_msg", json={
"group_id": group_id,
"message": msg
})
except BaseException as e:
print(f"send qq group msg error: {e}")
def main():
for f in os.listdir(path):
f = path+"/"+str(f)
if not room_id_re.match(f):
if not room_id_re.search(f):
continue
get_room(f).position = os.path.getsize(f)