6 Commits
tmp ... dev

Author SHA1 Message Date
tursom
0e24b9026b update 2026-03-14 07:57:27 +08:00
tursom
17947229a1 update 2025-09-18 17:55:35 +08:00
tursom
9600449f41 update 2025-09-18 13:56:00 +08:00
tursom
1632d7358c update 2025-08-27 15:00:55 +08:00
tursom
9b94393a13 update 2025-08-27 14:59:15 +08:00
tursom
09a2d65a34 自定义开播通知群 2025-08-27 14:56:01 +08:00
3 changed files with 84 additions and 25 deletions

5
db.py
View File

@@ -2,8 +2,9 @@
import mysql.connector
import schedule
class Room:
def __init__(self, room_id, liver_uid, liver_name):
def __init__(self, room_id: str, liver_uid, liver_name):
self.room_id = room_id
self.liver_uid = liver_uid
self.liver_name = liver_name
@@ -27,7 +28,7 @@ def flush_room_info():
rooms[room_id] = Room(room_id, liver_uid, liver_name)
def get_room(room_id) -> Room:
def get_room(room_id: str) -> Room:
return rooms.get(room_id, Room(room_id, 0, str(room_id)))

View File

@@ -9,18 +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 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) -> None:
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
@@ -35,8 +39,31 @@ class Room:
_rooms: dict[str, Room] = {}
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} {status}",
f"https://live.bilibili.com/{room.room_id}"
])
for qq_group_id in groups:
send_qq_group_msg(qq_group_id, msg)
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} {'开锅了' if is_online else '下锅了'}",
f"https://live.bilibili.com/{room.room_id}"
])
requests.post("http://mc1:5000/msg", json={"msg": msg})
except BaseException as e:
print(f"notify mc error: {e}")
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)
@@ -50,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
@@ -78,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:
@@ -91,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
@@ -101,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):
@@ -131,20 +164,44 @@ class MyHandler(FileSystemEventHandler):
f"{room.db_room.liver_name} 开锅了!",
f"https://live.bilibili.com/{room.room_id}"
])
requests.post("http://turntf:18846/notify", json={"msg": msg})
try:
requests.post("http://turntf:18846/notify", json={"msg": msg})
except BaseException as e:
print(f"notify turntf error: {e}")
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}"
])
requests.post("http://turntf:18846/notify", json={"msg": msg})
try:
requests.post("http://turntf:18846/notify", json={"msg": msg})
except BaseException as e:
print(f"notify turntf error: {e}")
notify_func = notify_qq_group.get(room.room_id)
if notify_func:
notify_func(room, False)
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)

View File

@@ -5,5 +5,6 @@ redis
PyYAML
watchdog
requests
mysql
mysql-connector-python
schedule
requests