From 2999b83ae88384ff2acfe0d407f6ecc8518f880a Mon Sep 17 00:00:00 2001 From: John Smith Date: Sun, 26 May 2019 17:14:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=91=BD=E4=BB=A4=E8=A1=8C?= =?UTF-8?q?=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ chat.py | 20 +++++++++++--------- main.py | 30 +++++++++++++++++++++--------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 9892bda..db0df13 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,10 @@ ```sh python3 main.py ``` + 或者可以指定host和端口号: + ```sh + python3 main.py --host 127.0.0.1 --port 80 + ``` 3. 用浏览器打开[http://localhost](http://localhost),输入房间号,进入,复制房间URL(其实就是http://localhost/room/<房间ID>) 4. (可选)在[https://chatv2.septapus.com/](https://chatv2.septapus.com/)生成样式,复制CSS 5. 在OBS中添加浏览器源,输入URL和自定义CSS diff --git a/chat.py b/chat.py index 531ed84..484f597 100644 --- a/chat.py +++ b/chat.py @@ -22,15 +22,15 @@ class Command(enum.IntEnum): ADD_VIP = 3 -http_session = aiohttp.ClientSession() +_http_session = aiohttp.ClientSession() _avatar_url_cache: Dict[int, str] = {} async def get_avatar_url(user_id): if user_id in _avatar_url_cache: return _avatar_url_cache[user_id] - async with http_session.get('https://api.bilibili.com/x/space/acc/info', - params={'mid': user_id}) as r: + async with _http_session.get('https://api.bilibili.com/x/space/acc/info', + params={'mid': user_id}) as r: data = await r.json() url = data['data']['face'] if not url.endswith('noface.gif'): @@ -48,7 +48,7 @@ class Room(blivedm.BLiveClient): _COMMAND_HANDLERS = blivedm.BLiveClient._COMMAND_HANDLERS.copy() def __init__(self, room_id): - super().__init__(room_id, session=http_session) + super().__init__(room_id, session=_http_session) self.future = None self.clients: List['ChatHandler'] = [] self.owner_id = None @@ -139,8 +139,8 @@ class RoomManager: room.start() room.clients.append(client) - # 测试用 - # self.__send_test_message(room) + if client.application.settings['debug']: + self.__send_test_message(room) def del_client(self, room_id, client: 'ChatHandler'): if room_id not in self._rooms: @@ -231,6 +231,8 @@ class ChatHandler(tornado.websocket.WebSocketHandler): if self.room_id is not None: room_manager.del_client(self.room_id, self) - # 测试用 - # def check_origin(self, origin): - # return True + # 跨域测试用 + def check_origin(self, origin): + if self.application.settings['debug']: + return True + return super().check_origin(origin) diff --git a/main.py b/main.py index eccbf2e..05effb9 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import argparse import logging import os @@ -21,21 +22,32 @@ class MainHandler(tornado.web.StaticFileHandler): def main(): + parser = argparse.ArgumentParser(description='用于OBS的仿YouTube风格的bilibili直播聊天层') + parser.add_argument('--host', help='服务器host,默认为127.0.0.1', default='127.0.0.1') + parser.add_argument('--port', help='服务器端口,默认为80', type=int, default=80) + parser.add_argument('--debug', help='调试模式', action='store_true') + args = parser.parse_args() + logging.basicConfig( format='{asctime} {levelname} [{name}]: {message}', datefmt='%Y-%m-%d %H:%M:%S', style='{', - level=logging.INFO + level=logging.INFO if not args.debug else logging.DEBUG ) - app = tornado.web.Application([ - (r'/chat', chat.ChatHandler), - (r'/((css|img|js)/.*)', tornado.web.StaticFileHandler, {'path': WEB_ROOT}), - (r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': WEB_ROOT}), - (r'/.*', MainHandler, {'path': WEB_ROOT}) - ], websocket_ping_interval=30) - app.listen(80, '127.0.0.1') - logger.info('服务器启动:127.0.0.1:80') + app = tornado.web.Application( + [ + (r'/chat', chat.ChatHandler), + (r'/((css|img|js)/.*)', tornado.web.StaticFileHandler, {'path': WEB_ROOT}), + (r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': WEB_ROOT}), + (r'/.*', MainHandler, {'path': WEB_ROOT}) + ], + websocket_ping_interval=30, + debug=args.debug, + autoreload=False + ) + app.listen(args.port, args.host) + logger.info('服务器启动:%s:%d', args.host, args.port) tornado.ioloop.IOLoop.current().start()