This commit is contained in:
tursom 2024-10-11 14:00:52 +08:00
parent 7366d055e2
commit 91edac61e0

View File

@ -8,7 +8,8 @@ import json
import db import db
import re import re
room_id_re = re.compile(r"\/(\d+).jsonl") path = "logs"
room_id_re = re.compile(rf"{path}/(\d+).jsonl")
class Room: class Room:
@ -36,7 +37,12 @@ _rooms: dict[str, Room] = {}
def get_room(src_path): def get_room(src_path):
room_id = room_id_re.findall(src_path)[0] room_id = room_id_re.findall(src_path)[0]
return _rooms.get(room_id, Room(room_id)) room = _rooms.get(room_id, None)
if room is None:
room = Room(room_id)
_rooms[room_id] = room
return room
class MyHandler(FileSystemEventHandler): class MyHandler(FileSystemEventHandler):
@ -44,7 +50,7 @@ class MyHandler(FileSystemEventHandler):
if event.is_directory: if event.is_directory:
return return
if not event.src_path.endswith(".jsonl"): if not room_id_re.match(event.src_path):
return return
room: Room = get_room(event.src_path) room: Room = get_room(event.src_path)
@ -67,7 +73,7 @@ class MyHandler(FileSystemEventHandler):
if event.is_directory: if event.is_directory:
return return
if not event.src_path.endswith(".jsonl"): if not room_id_re.match(event.src_path):
return return
room: Room = get_room(event.src_path) room: Room = get_room(event.src_path)
@ -84,7 +90,7 @@ class MyHandler(FileSystemEventHandler):
if event.is_directory: if event.is_directory:
return return
if not event.src_path.endswith(".jsonl"): if not room_id_re.match(event.src_path):
return return
room = _rooms.get(event.src_path, Room()) room = _rooms.get(event.src_path, Room())
@ -118,8 +124,15 @@ class MyHandler(FileSystemEventHandler):
requests.post("http://turntf:18846/notify", json={"msg": msg}) requests.post("http://turntf:18846/notify", json={"msg": msg})
def main(): def main():
path = "logs" for f in os.listdir(path):
f = path+"/"+str(f)
if not room_id_re.match(f):
continue
get_room(f).position = os.path.getsize(f)
event_handler = MyHandler() event_handler = MyHandler()
observer = Observer() observer = Observer()
observer.schedule(event_handler, path, recursive=True) observer.schedule(event_handler, path, recursive=True)