Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
xfgryujk committed Oct 11, 2020
2 parents 54aac23 + cce51a6 commit e7d8fd0
Show file tree
Hide file tree
Showing 39 changed files with 2,932 additions and 2,404 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ venv.bak/

.idea/
data/database.db
*.log*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
### 二、公共服务器
请优先在本地使用,使用公共服务器会有更大的弹幕延迟,而且服务器故障时可能发生直播事故

* [第三方公共服务器](http://chat.bilisc.com/)
* [公共服务器](http://chat.bilisc.com/)
* [仅样式生成器](https://style.vtbs.moe/)

### 三、源代码版(自建服务器或在Windows以外平台)
Expand Down
110 changes: 105 additions & 5 deletions api/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import aiohttp
import tornado.websocket

import api.base
import blivedm.blivedm as blivedm
import config
import models.avatar
Expand Down Expand Up @@ -100,6 +101,10 @@ def __init__(self, room_id):
self.clients: List['ChatHandler'] = []
self.auto_translate_count = 0

async def init_room(self):
await super().init_room()
return True

def stop_and_close(self):
if self.is_running:
future = self.stop()
Expand Down Expand Up @@ -246,13 +251,10 @@ async def _translate_and_response(self, text, msg_id):
return
self.send_message_if(
lambda client: client.auto_translate,
Command.UPDATE_TRANSLATION,
[
# 0: id
Command.UPDATE_TRANSLATION, make_translation_message(
msg_id,
# 1: translation
translation
]
)
)


Expand Down Expand Up @@ -289,6 +291,15 @@ def make_text_message(avatar_url, timestamp, author_name, author_type, content,
]


def make_translation_message(msg_id, translation):
return [
# 0: id
msg_id,
# 1: translation
translation
]


class RoomManager:
def __init__(self):
self._rooms: Dict[int, Room] = {}
Expand Down Expand Up @@ -506,3 +517,92 @@ async def send_test_message(self):
gift_data['totalCoin'] = 1245000
gift_data['giftName'] = '小电视飞船'
self.send_message(Command.ADD_GIFT, gift_data)


# noinspection PyAbstractClass
class RoomInfoHandler(api.base.ApiHandler):
_host_server_list_cache = blivedm.DEFAULT_DANMAKU_SERVER_LIST

async def get(self):
room_id = int(self.get_query_argument('roomId'))
logger.info('Client %s is getting room info %d', self.request.remote_ip, room_id)
room_id, owner_uid = await self._get_room_info(room_id)
host_server_list = await self._get_server_host_list(room_id)
if owner_uid == 0:
# 缓存3分钟
self.set_header('Cache-Control', 'private, max-age=180')
else:
# 缓存1天
self.set_header('Cache-Control', 'private, max-age=86400')
self.write({
'roomId': room_id,
'ownerUid': owner_uid,
'hostServerList': host_server_list
})

@staticmethod
async def _get_room_info(room_id):
try:
async with _http_session.get(blivedm.ROOM_INIT_URL, params={'room_id': room_id}
) as res:
if res.status != 200:
logger.warning('room %d _get_room_info failed: %d %s', room_id,
res.status, res.reason)
return room_id, 0
data = await res.json()
except aiohttp.ClientConnectionError:
logger.exception('room %d _get_room_info failed', room_id)
return room_id, 0

if data['code'] != 0:
logger.warning('room %d _get_room_info failed: %s', room_id, data['msg'])
return room_id, 0

room_info = data['data']['room_info']
return room_info['room_id'], room_info['uid']

@classmethod
async def _get_server_host_list(cls, _room_id):
return cls._host_server_list_cache

# 连接其他host必须要key
# try:
# async with _http_session.get(blivedm.DANMAKU_SERVER_CONF_URL, params={'id': room_id, 'type': 0}
# ) as res:
# if res.status != 200:
# logger.warning('room %d _get_server_host_list failed: %d %s', room_id,
# res.status, res.reason)
# return cls._host_server_list_cache
# data = await res.json()
# except aiohttp.ClientConnectionError:
# logger.exception('room %d _get_server_host_list failed', room_id)
# return cls._host_server_list_cache
#
# if data['code'] != 0:
# logger.warning('room %d _get_server_host_list failed: %s', room_id, data['msg'])
# return cls._host_server_list_cache
#
# host_server_list = data['data']['host_list']
# if not host_server_list:
# logger.warning('room %d _get_server_host_list failed: host_server_list is empty')
# return cls._host_server_list_cache
#
# cls._host_server_list_cache = host_server_list
# return host_server_list


# noinspection PyAbstractClass
class AvatarHandler(api.base.ApiHandler):
async def get(self):
uid = int(self.get_query_argument('uid'))
avatar_url = await models.avatar.get_avatar_url_or_none(uid)
if avatar_url is None:
avatar_url = models.avatar.DEFAULT_AVATAR_URL
# 缓存3分钟
self.set_header('Cache-Control', 'private, max-age=180')
else:
# 缓存1天
self.set_header('Cache-Control', 'private, max-age=86400')
self.write({
'avatarUrl': avatar_url
})
1 change: 1 addition & 0 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import update


# noinspection PyAbstractClass
class MainHandler(tornado.web.StaticFileHandler):
"""为了使用Vue Router的history模式,把不存在的文件请求转发到index.html"""
async def get(self, path, include_body=True):
Expand Down
2 changes: 1 addition & 1 deletion blivedm
Submodule blivedm updated 1 files
+40 −11 blivedm.py
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"downloadjs": "^1.4.7",
"element-ui": "^2.9.1",
"lodash": "^4.17.19",
"pako": "^1.0.11",
"vue": "^2.6.10",
"vue-i18n": "^8.11.2",
"vue-router": "^3.0.6"
Expand Down
Loading

0 comments on commit e7d8fd0

Please sign in to comment.