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.database
|
2022-02-15 00:18:46 +08:00
|
|
|
|
import services.avatar
|
|
|
|
|
import services.chat
|
|
|
|
|
import services.translate
|
2019-09-16 20:48:03 +08:00
|
|
|
|
import update
|
2023-07-29 01:22:21 +08:00
|
|
|
|
import utils.request
|
2019-05-22 01:11:23 +08:00
|
|
|
|
|
2019-05-22 14:10:27 +08:00
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2020-02-03 16:18:21 +08:00
|
|
|
|
routes = [
|
2020-08-19 23:16:01 +08:00
|
|
|
|
(r'/api/server_info', api.main.ServerInfoHandler),
|
2022-02-27 22:05:37 +08:00
|
|
|
|
(r'/api/emoticon', api.main.UploadEmoticonHandler),
|
|
|
|
|
|
2020-08-19 23:16:01 +08:00
|
|
|
|
(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
|
|
|
|
|
2022-03-01 22:13:25 +08:00
|
|
|
|
(rf'{api.main.EMOTICON_BASE_URL}/(.*)', tornado.web.StaticFileHandler, {'path': api.main.EMOTICON_UPLOAD_PATH}),
|
|
|
|
|
(r'/(.*)', api.main.MainHandler, {'path': config.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()
|
2023-07-29 01:22:21 +08:00
|
|
|
|
|
2023-07-29 12:48:57 +08:00
|
|
|
|
utils.request.init()
|
2020-02-03 16:18:21 +08:00
|
|
|
|
models.database.init(args.debug)
|
2023-07-29 01:22:21 +08:00
|
|
|
|
|
2022-02-15 00:18:46 +08:00
|
|
|
|
services.avatar.init()
|
|
|
|
|
services.translate.init()
|
|
|
|
|
services.chat.init()
|
2023-07-29 01:22:21 +08:00
|
|
|
|
|
2020-02-03 16:18:21 +08:00
|
|
|
|
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):
|
2022-03-01 22:13:25 +08:00
|
|
|
|
filename = os.path.join(config.BASE_PATH, 'log', 'blivechat.log')
|
2020-09-03 20:01:54 +08:00
|
|
|
|
stream_handler = logging.StreamHandler()
|
|
|
|
|
file_handler = logging.handlers.TimedRotatingFileHandler(
|
2022-03-01 22:13:25 +08:00
|
|
|
|
filename, encoding='utf-8', when='midnight', backupCount=7, delay=True
|
2020-09-03 20:01:54 +08:00
|
|
|
|
)
|
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
|
|
|
|
)
|
|
|
|
|
|
2020-09-14 21:50:40 +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,
|
2022-02-27 22:05:37 +08:00
|
|
|
|
xheaders=cfg.tornado_xheaders,
|
|
|
|
|
max_body_size=1024 * 1024,
|
|
|
|
|
max_buffer_size=1024 * 1024
|
2020-08-22 18:38:52 +08:00
|
|
|
|
)
|
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:
|
2020-02-04 16:26:28 +08:00
|
|
|
|
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()
|