添加命令行选项

This commit is contained in:
John Smith 2019-05-26 17:14:59 +08:00
parent 036c462485
commit 2999b83ae8
3 changed files with 36 additions and 18 deletions

View File

@ -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

20
chat.py
View File

@ -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)

30
main.py
View File

@ -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()