留一个退出后清理的逻辑,虽然windows上用不了
This commit is contained in:
parent
dc1800e492
commit
788c8cba9d
10
main.py
10
main.py
@ -1,5 +1,6 @@
|
|||||||
import threading
|
import threading
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import atexit
|
||||||
|
|
||||||
from config.helper import config
|
from config.helper import config
|
||||||
from handler.http_server import app
|
from handler.http_server import app
|
||||||
@ -15,5 +16,14 @@ if __name__ == '__main__':
|
|||||||
api_thread.start()
|
api_thread.start()
|
||||||
browser_manager = init_browser_manager()
|
browser_manager = init_browser_manager()
|
||||||
output_manager = OutputManager()
|
output_manager = OutputManager()
|
||||||
|
|
||||||
|
|
||||||
|
def terminate():
|
||||||
|
print("terminate")
|
||||||
|
browser_manager.terminate()
|
||||||
|
output_manager.terminate()
|
||||||
|
|
||||||
|
|
||||||
|
atexit.register(terminate)
|
||||||
output_manager.start_loop()
|
output_manager.start_loop()
|
||||||
api_thread.join()
|
api_thread.join()
|
||||||
|
@ -40,7 +40,7 @@ class OutputManager():
|
|||||||
self._writer.append(self._mapping[_c]())
|
self._writer.append(self._mapping[_c]())
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
...
|
self.terminate()
|
||||||
|
|
||||||
def decode_payload(self, message: MessagePayload):
|
def decode_payload(self, message: MessagePayload):
|
||||||
try:
|
try:
|
||||||
@ -107,3 +107,7 @@ class OutputManager():
|
|||||||
while True:
|
while True:
|
||||||
message = MESSAGE_QUEUE.get()
|
message = MESSAGE_QUEUE.get()
|
||||||
self.decode_payload(message)
|
self.decode_payload(message)
|
||||||
|
|
||||||
|
def terminate(self):
|
||||||
|
for writer in self._writer:
|
||||||
|
writer.terminate()
|
||||||
|
@ -8,29 +8,30 @@ class XMLWriter(IOutput):
|
|||||||
"""
|
"""
|
||||||
可输出与B站弹幕姬兼容的xml弹幕格式,可用于转成ass字幕
|
可输出与B站弹幕姬兼容的xml弹幕格式,可用于转成ass字幕
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.file_mappings: "dict[str, IO[str]]" = {}
|
self._file_mappings: "dict[str, IO[str]]" = {}
|
||||||
self.time_mappings: "dict[str, float]" = {}
|
self.time_mappings: "dict[str, float]" = {}
|
||||||
self._file_name_pattern: "str" = config()['output']['xml']['file_pattern']
|
self._file_name_pattern: "str" = config()['output']['xml']['file_pattern']
|
||||||
|
|
||||||
def _get_fd_by_room_id(self, room_id: str) -> IO[str]:
|
def _get_fd_by_room_id(self, room_id: str) -> IO[str]:
|
||||||
if room_id in self.file_mappings:
|
if room_id in self._file_mappings:
|
||||||
return self.file_mappings[room_id]
|
return self._file_mappings[room_id]
|
||||||
cur_ts = time.time()
|
cur_ts = time.time()
|
||||||
fd = open(self._file_name_pattern.format_map({
|
fd = open(self._file_name_pattern.format_map({
|
||||||
"room_id": room_id,
|
"room_id": room_id,
|
||||||
"ts": cur_ts
|
"ts": cur_ts
|
||||||
}), "w", encoding="UTF-8")
|
}), "w", encoding="UTF-8")
|
||||||
self.file_mappings[room_id] = fd
|
self._file_mappings[room_id] = fd
|
||||||
self.time_mappings[room_id] = cur_ts
|
self.time_mappings[room_id] = cur_ts
|
||||||
return fd
|
return fd
|
||||||
|
|
||||||
def _close_fd_by_room_id(self, room_id: str):
|
def _close_fd_by_room_id(self, room_id: str):
|
||||||
if room_id in self.file_mappings:
|
if room_id in self._file_mappings:
|
||||||
fd = self.file_mappings[room_id]
|
fd = self._file_mappings[room_id]
|
||||||
if not fd.closed:
|
if not fd.closed:
|
||||||
fd.close()
|
fd.close()
|
||||||
del self.file_mappings[room_id]
|
del self._file_mappings[room_id]
|
||||||
if room_id in self.time_mappings:
|
if room_id in self.time_mappings:
|
||||||
del self.time_mappings[room_id]
|
del self.time_mappings[room_id]
|
||||||
|
|
||||||
@ -50,9 +51,9 @@ class XMLWriter(IOutput):
|
|||||||
if fd is None:
|
if fd is None:
|
||||||
return
|
return
|
||||||
cur_time = time.time()
|
cur_time = time.time()
|
||||||
_c = """<d p="{:.2f},1,24,16777215,{:.0f},0,{},0" user="{}">{}</d>\r\n""".format(
|
_c = """<d p="{:.2f},1,24,{},{:.0f},0,{},0" user="{}">{}</d>\r\n""".format(
|
||||||
self._get_bias_ts_by_room_id(message.room_id, cur_time),
|
self._get_bias_ts_by_room_id(message.room_id, cur_time), message.room_id,
|
||||||
cur_time*1000, message.user().id, message.user().nickname, message.content
|
cur_time * 1000, message.user().id, message.user().nickname, message.content
|
||||||
)
|
)
|
||||||
fd.write(_c)
|
fd.write(_c)
|
||||||
|
|
||||||
@ -66,3 +67,9 @@ class XMLWriter(IOutput):
|
|||||||
message.user().nickname, message.gift.name, message.instance.repeatCount
|
message.user().nickname, message.gift.name, message.instance.repeatCount
|
||||||
)
|
)
|
||||||
fd.write(_c)
|
fd.write(_c)
|
||||||
|
|
||||||
|
def terminate(self):
|
||||||
|
print("保存所有弹幕文件中...")
|
||||||
|
for _room_id in self._file_mappings:
|
||||||
|
self._close_fd_by_room_id(_room_id)
|
||||||
|
print("保存完毕")
|
||||||
|
Reference in New Issue
Block a user