blivechat/main.py

104 lines
2.8 KiB
Python
Raw Normal View History

2019-05-21 19:15:12 +08:00
# -*- coding: utf-8 -*-
2019-05-26 17:14:59 +08:00
import argparse
2019-05-22 14:10:27 +08:00
import logging
2020-09-03 20:01:54 +08:00
import logging.handlers
2019-05-21 19:15:12 +08:00
import os
2019-09-16 20:48:03 +08:00
import webbrowser
2019-05-21 19:15:12 +08:00
import tornado.ioloop
import tornado.web
2020-02-03 16:18:21 +08:00
import api.chat
import api.main
import config
import models.avatar
import models.database
2020-02-06 12:48:20 +08:00
import models.translate
2019-09-16 20:48:03 +08:00
import update
2019-05-22 01:11:23 +08:00
2019-05-22 14:10:27 +08:00
logger = logging.getLogger(__name__)
2020-09-03 20:01:54 +08:00
BASE_PATH = os.path.dirname(os.path.realpath(__file__))
WEB_ROOT = os.path.join(BASE_PATH, 'frontend', 'dist')
LOG_FILE_NAME = os.path.join(BASE_PATH, 'log', 'blivechat.log')
2019-05-21 19:15:12 +08:00
2020-02-03 16:18:21 +08:00
routes = [
2020-08-19 23:16:01 +08:00
(r'/api/server_info', api.main.ServerInfoHandler),
(r'/api/chat', api.chat.ChatHandler),
2020-09-13 21:24:20 +08:00
(r'/api/room_info', api.chat.RoomInfoHandler),
(r'/api/avatar_url', api.chat.AvatarHandler),
2020-08-19 23:16:01 +08:00
2020-08-18 21:48:33 +08:00
(r'/(.*)', api.main.MainHandler, {'path': WEB_ROOT, 'default_filename': 'index.html'})
2020-02-03 16:18:21 +08:00
]
2019-05-21 19:15:12 +08:00
def main():
2020-02-03 16:18:21 +08:00
args = parse_args()
init_logging(args.debug)
config.init()
models.database.init(args.debug)
models.avatar.init()
2020-02-06 12:48:20 +08:00
models.translate.init()
2020-02-03 16:18:21 +08:00
api.chat.init()
update.check_update()
run_server(args.host, args.port, args.debug)
def parse_args():
2020-09-03 20:01:54 +08:00
parser = argparse.ArgumentParser(description='用于OBS的仿YouTube风格的bilibili直播评论栏')
2019-05-26 17:14:59 +08:00
parser.add_argument('--host', help='服务器host默认为127.0.0.1', default='127.0.0.1')
2019-07-10 15:30:35 +08:00
parser.add_argument('--port', help='服务器端口默认为12450', type=int, default=12450)
2019-05-26 17:14:59 +08:00
parser.add_argument('--debug', help='调试模式', action='store_true')
2020-02-03 16:18:21 +08:00
return parser.parse_args()
2019-05-26 17:14:59 +08:00
2020-02-03 16:18:21 +08:00
def init_logging(debug):
2020-09-03 20:01:54 +08:00
stream_handler = logging.StreamHandler()
file_handler = logging.handlers.TimedRotatingFileHandler(
LOG_FILE_NAME, encoding='utf-8', when='midnight', backupCount=7, delay=True
)
2021-07-17 13:03:45 +08:00
# noinspection PyArgumentList
2019-05-22 14:10:27 +08:00
logging.basicConfig(
format='{asctime} {levelname} [{name}]: {message}',
datefmt='%Y-%m-%d %H:%M:%S',
style='{',
2020-09-03 20:01:54 +08:00
level=logging.INFO if not debug else logging.DEBUG,
handlers=[stream_handler, file_handler]
2019-05-22 14:10:27 +08:00
)
# 屏蔽访问日志
logging.getLogger('tornado.access').setLevel(logging.WARNING)
2019-09-16 20:48:03 +08:00
2020-02-03 16:18:21 +08:00
def run_server(host, port, debug):
2019-05-26 17:14:59 +08:00
app = tornado.web.Application(
2020-02-03 16:18:21 +08:00
routes,
websocket_ping_interval=10,
debug=debug,
2019-05-26 17:14:59 +08:00
autoreload=False
)
2020-08-22 18:38:52 +08:00
cfg = config.get_config()
2019-09-16 20:48:03 +08:00
try:
2020-08-22 18:38:52 +08:00
app.listen(
port,
host,
xheaders=cfg.tornado_xheaders
)
2019-09-16 20:48:03 +08:00
except OSError:
2020-02-03 16:18:21 +08:00
logger.warning('Address is used %s:%d', host, port)
2019-09-16 20:48:03 +08:00
return
finally:
url = 'http://localhost/' if port == 80 else f'http://localhost:{port}/'
# 防止更新版本后浏览器加载缓存
url += '?_v=' + update.VERSION
2019-09-16 20:48:03 +08:00
webbrowser.open(url)
2020-02-03 16:18:21 +08:00
logger.info('Server started: %s:%d', host, port)
2019-05-21 19:15:12 +08:00
tornado.ioloop.IOLoop.current().start()
if __name__ == '__main__':
main()