添加对应方法,加入输出管理器

This commit is contained in:
Jerry Yan 2022-07-15 16:10:07 +08:00
parent d0e067ae44
commit 49238a09e0
7 changed files with 76 additions and 42 deletions

View File

@ -6,6 +6,7 @@ from common.items import TabInfo
from config import ConfigManager from config import ConfigManager
from browser import BrowserManager from browser import BrowserManager
from proxy import ProxyManager from proxy import ProxyManager
from output import OutputManager
_log = logging.getLogger("CoreManager") _log = logging.getLogger("CoreManager")
_log.setLevel(logging.DEBUG) _log.setLevel(logging.DEBUG)
@ -15,6 +16,7 @@ class CoreManager(metaclass=Singleton):
config_manager: "ConfigManager" config_manager: "ConfigManager"
browser_manager: "BrowserManager" browser_manager: "BrowserManager"
proxy_manager: "ProxyManager" proxy_manager: "ProxyManager"
output_manager: "OutputManager"
def __del__(self): def __del__(self):
""" """
@ -35,6 +37,13 @@ class CoreManager(metaclass=Singleton):
pass pass
finally: finally:
_log.debug("析构Mitm代理管理器完毕") _log.debug("析构Mitm代理管理器完毕")
try:
_log.debug("析构输出管理器")
self.output_manager.terminate()
except:
pass
finally:
_log.debug("析构输出管理器完毕")
def __init__(self): def __init__(self):
""" """
@ -50,6 +59,9 @@ class CoreManager(metaclass=Singleton):
_log.debug("初始化浏览器管理器") _log.debug("初始化浏览器管理器")
self.browser_manager = BrowserManager(self.config_manager) self.browser_manager = BrowserManager(self.config_manager)
_log.info("初始化浏览器管理器完毕") _log.info("初始化浏览器管理器完毕")
_log.debug("初始化输出管理器")
self.output_manager = OutputManager(self.config_manager)
_log.info("初始化输出管理器完毕")
self._open_config_tabs() self._open_config_tabs()
def restart(self): def restart(self):
@ -57,11 +69,28 @@ class CoreManager(metaclass=Singleton):
self.__del__() self.__del__()
self.__init__() self.__init__()
def open_tab(self, tab_info: "TabInfo"): def open_tab(self, url: "str", tab_type: "int" = TabInfo.TAB_TYPE_OTHER):
tab_info = TabInfo()
tab_info.url = url
tab_info.tab_type = TabInfo.TAB_TYPE_LIVE
self.browser_manager.open_tab(tab_info) self.browser_manager.open_tab(tab_info)
def close_tab(self, tab_info: "TabInfo"): def close_tab(self, url):
self.browser_manager.close_tab(tab_info) handler = self.browser_manager.find_tab_handler_by_url(url)
if handler is not None:
tab_info = TabInfo()
tab_info.tab_handler = handler
self.browser_manager.close_tab(tab_info)
def refresh_tab(self, tab_info):
...
def on_broadcast(self, room_id: str):
live_url = "https://live.douyin.com/" + room_id
tab_info = TabInfo()
tab_info.url = live_url
tab_info.tab_type = TabInfo.TAB_TYPE_LIVE
self.browser_manager.create_or_refresh(tab_info)
def _open_config_tabs(self): def _open_config_tabs(self):
rooms = self.config_manager.config["douyin"]["rooms"] rooms = self.config_manager.config["douyin"]["rooms"]
@ -73,7 +102,4 @@ class CoreManager(metaclass=Singleton):
live_url = "https://live.douyin.com/" + room live_url = "https://live.douyin.com/" + room
else: else:
live_url = room live_url = room
tab_info = TabInfo() self.open_tab(live_url, TabInfo.TAB_TYPE_LIVE)
tab_info.url = live_url
tab_info.tab_type = TabInfo.TAB_TYPE_LIVE
self.open_tab(tab_info)

View File

@ -1,8 +1,3 @@
import traceback
from datetime import datetime
from config.helper import config
class Base: class Base:
instance = None instance = None

View File

@ -1,43 +1,52 @@
from messages.base import Base from typing import TYPE_CHECKING
from messages.chat import ChatMessage
from messages.control import ControlMessage if TYPE_CHECKING:
from messages.fansclub import FansclubMessage from messages.base import Base
from messages.gift import GiftMessage from messages.chat import ChatMessage
from messages.like import LikeMessage from messages.control import ControlMessage
from messages.member import MemberMessage from messages.fansclub import FansclubMessage
from messages.roomuserseq import RoomUserSeqMessage from messages.gift import GiftMessage
from messages.social import SocialMessage from messages.like import LikeMessage
from messages.member import MemberMessage
from messages.roomuserseq import RoomUserSeqMessage
from messages.social import SocialMessage
from config import ConfigManager
class IOutput(): class IOutput():
_config_manager: "ConfigManager"
def __del__(self): def __del__(self):
self.terminate() self.terminate()
def output(self, message_type: str, message_obj: Base): def __init__(self, config_manager: "ConfigManager"):
self._config_manager = config_manager
def output(self, message_type: str, message_obj: "Base"):
... ...
def chat_output(self, message: ChatMessage): def chat_output(self, message: "ChatMessage"):
... ...
def like_output(self, message: LikeMessage): def like_output(self, message: "LikeMessage"):
... ...
def member_output(self, message: MemberMessage): def member_output(self, message: "MemberMessage"):
... ...
def social_output(self, message: SocialMessage): def social_output(self, message: "SocialMessage"):
... ...
def gift_output(self, message: GiftMessage): def gift_output(self, message: "GiftMessage"):
... ...
def userseq_output(self, message: RoomUserSeqMessage): def userseq_output(self, message: "RoomUserSeqMessage"):
... ...
def control_output(self, message: ControlMessage): def control_output(self, message: "ControlMessage"):
... ...
def fansclub_output(self, message: FansclubMessage): def fansclub_output(self, message: "FansclubMessage"):
... ...
def other_output(self, message_type: str, message_raw: bytes): def other_output(self, message_type: str, message_raw: bytes):

View File

@ -0,0 +1 @@
from .manager import OutputManager

View File

@ -2,17 +2,17 @@ import os
import time import time
import traceback import traceback
from config.helper import config
from output.IOutput import IOutput from output.IOutput import IOutput
class DebugWriter(IOutput): class DebugWriter(IOutput):
def __init__(self): def __init__(self, config_manager):
super(DebugWriter, self).__init__(config_manager)
# 获取对应配置文件 # 获取对应配置文件
self.unknown_output_dir = config()['output']['debug']['save_path']['unknown'] self.unknown_output_dir = self._config_manager.config['output']['debug']['save_path']
if not os.path.isdir(self.unknown_output_dir): if not os.path.isdir(self.unknown_output_dir):
os.makedirs(self.unknown_output_dir) os.makedirs(self.unknown_output_dir)
self.error_output_dir = config()['output']['debug']['save_path']['error'] self.error_output_dir = os.path.join(self._config_manager.config['output']['debug']['save_path'], "error")
if not os.path.isdir(self.error_output_dir): if not os.path.isdir(self.error_output_dir):
os.makedirs(self.error_output_dir) os.makedirs(self.error_output_dir)

View File

@ -2,7 +2,7 @@ import gzip
import threading import threading
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from config.helper import config from common import Singleton
from messages.chat import ChatMessage from messages.chat import ChatMessage
from messages.control import ControlMessage from messages.control import ControlMessage
from messages.fansclub import FansclubMessage from messages.fansclub import FansclubMessage
@ -21,9 +21,11 @@ if TYPE_CHECKING:
from typing import Type, Optional, List from typing import Type, Optional, List
from output.IOutput import IOutput from output.IOutput import IOutput
from proxy.common import MessagePayload from proxy.common import MessagePayload
from config import ConfigManager
class OutputManager(): class OutputManager(metaclass=Singleton):
_config_manager: "ConfigManager"
_mapping: "dict[str, Type[IOutput]]" = { _mapping: "dict[str, Type[IOutput]]" = {
"print": Print, "print": Print,
"xml": XMLWriter, "xml": XMLWriter,
@ -33,14 +35,15 @@ class OutputManager():
_thread: "Optional[threading.Thread]"= None _thread: "Optional[threading.Thread]"= None
_should_exit = threading.Event() _should_exit = threading.Event()
def __init__(self): def __init__(self, config_manager: "ConfigManager"):
_config = config()['output']['use'] self._config_manager = config_manager
_config = self._config_manager.config['output']['use']
if type(_config) != list: if type(_config) != list:
_config = [_config] _config = [_config]
for _c in _config: for _c in _config:
if _c not in self._mapping: if _c not in self._mapping:
raise Exception("不支持的输出方式") raise Exception("不支持的输出方式")
self._writer.append(self._mapping[_c]()) self._writer.append(self._mapping[_c](self._config_manager))
def __del__(self): def __del__(self):
self.terminate() self.terminate()

View File

@ -1,4 +1,3 @@
from config.helper import config
from output.IOutput import IOutput from output.IOutput import IOutput
from typing import IO from typing import IO
import time import time
@ -9,10 +8,11 @@ class XMLWriter(IOutput):
可输出与B站弹幕姬兼容的xml弹幕格式可用于转成ass字幕 可输出与B站弹幕姬兼容的xml弹幕格式可用于转成ass字幕
""" """
def __init__(self): def __init__(self, config_manager):
super(XMLWriter, self).__init__(config_manager)
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" = self._config_manager.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: