mirror of
https://github.com/xfgryujk/blivechat.git
synced 2025-04-03 16:10:35 +08:00
添加允许自动翻译的房间配置
This commit is contained in:
parent
96070ac473
commit
127344802d
132
api/chat.py
132
api/chat.py
@ -149,34 +149,21 @@ class Room(blivedm.BLiveClient):
|
|||||||
|
|
||||||
id_ = uuid.uuid4().hex
|
id_ = uuid.uuid4().hex
|
||||||
# 为了节省带宽用list而不是dict
|
# 为了节省带宽用list而不是dict
|
||||||
self.send_message(Command.ADD_TEXT, [
|
self.send_message(Command.ADD_TEXT, make_text_message(
|
||||||
# 0: avatarUrl
|
|
||||||
await models.avatar.get_avatar_url(danmaku.uid),
|
await models.avatar.get_avatar_url(danmaku.uid),
|
||||||
# 1: timestamp
|
|
||||||
int(danmaku.timestamp / 1000),
|
int(danmaku.timestamp / 1000),
|
||||||
# 2: authorName
|
|
||||||
danmaku.uname,
|
danmaku.uname,
|
||||||
# 3: authorType
|
|
||||||
author_type,
|
author_type,
|
||||||
# 4: content
|
|
||||||
danmaku.msg,
|
danmaku.msg,
|
||||||
# 5: privilegeType
|
|
||||||
danmaku.privilege_type,
|
danmaku.privilege_type,
|
||||||
# 6: isGiftDanmaku
|
danmaku.msg_type,
|
||||||
1 if danmaku.msg_type else 0,
|
|
||||||
# 7: authorLevel
|
|
||||||
danmaku.user_level,
|
danmaku.user_level,
|
||||||
# 8: isNewbie
|
danmaku.urank < 10000,
|
||||||
1 if danmaku.urank < 10000 else 0,
|
danmaku.mobile_verify,
|
||||||
# 9: isMobileVerified
|
|
||||||
1 if danmaku.mobile_verify else 0,
|
|
||||||
# 10: medalLevel
|
|
||||||
0 if danmaku.room_id != self.room_id else danmaku.medal_level,
|
0 if danmaku.room_id != self.room_id else danmaku.medal_level,
|
||||||
# 11: id
|
|
||||||
id_,
|
id_,
|
||||||
# 12: translation
|
|
||||||
translation
|
translation
|
||||||
])
|
))
|
||||||
|
|
||||||
if need_translate:
|
if need_translate:
|
||||||
await self._translate_and_response(danmaku.msg, id_)
|
await self._translate_and_response(danmaku.msg, id_)
|
||||||
@ -245,8 +232,10 @@ class Room(blivedm.BLiveClient):
|
|||||||
})
|
})
|
||||||
|
|
||||||
def _need_translate(self, text):
|
def _need_translate(self, text):
|
||||||
|
cfg = config.get_config()
|
||||||
return (
|
return (
|
||||||
config.get_config().enable_translate
|
cfg.enable_translate
|
||||||
|
and (not cfg.allow_translate_rooms or self.room_id in cfg.allow_translate_rooms)
|
||||||
and self.auto_translate_count > 0
|
and self.auto_translate_count > 0
|
||||||
and models.translate.need_translate(text)
|
and models.translate.need_translate(text)
|
||||||
)
|
)
|
||||||
@ -267,6 +256,39 @@ class Room(blivedm.BLiveClient):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def make_text_message(avatar_url, timestamp, author_name, author_type, content, privilege_type,
|
||||||
|
is_gift_danmaku, author_level, is_newbie, is_mobile_verified, medal_level,
|
||||||
|
id_, translation):
|
||||||
|
return [
|
||||||
|
# 0: avatarUrl
|
||||||
|
avatar_url,
|
||||||
|
# 1: timestamp
|
||||||
|
timestamp,
|
||||||
|
# 2: authorName
|
||||||
|
author_name,
|
||||||
|
# 3: authorType
|
||||||
|
author_type,
|
||||||
|
# 4: content
|
||||||
|
content,
|
||||||
|
# 5: privilegeType
|
||||||
|
privilege_type,
|
||||||
|
# 6: isGiftDanmaku
|
||||||
|
1 if is_gift_danmaku else 0,
|
||||||
|
# 7: authorLevel
|
||||||
|
author_level,
|
||||||
|
# 8: isNewbie
|
||||||
|
1 if is_newbie else 0,
|
||||||
|
# 9: isMobileVerified
|
||||||
|
1 if is_mobile_verified else 0,
|
||||||
|
# 10: medalLevel
|
||||||
|
medal_level,
|
||||||
|
# 11: id
|
||||||
|
id_,
|
||||||
|
# 12: translation
|
||||||
|
translation
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class RoomManager:
|
class RoomManager:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._rooms: Dict[int, Room] = {}
|
self._rooms: Dict[int, Room] = {}
|
||||||
@ -285,8 +307,7 @@ class RoomManager:
|
|||||||
if client.auto_translate:
|
if client.auto_translate:
|
||||||
room.auto_translate_count += 1
|
room.auto_translate_count += 1
|
||||||
|
|
||||||
if client.application.settings['debug']:
|
await client.on_join_room()
|
||||||
await client.send_test_message()
|
|
||||||
|
|
||||||
def del_client(self, room_id, client: 'ChatHandler'):
|
def del_client(self, room_id, client: 'ChatHandler'):
|
||||||
room = self._rooms.get(room_id, None)
|
room = self._rooms.get(room_id, None)
|
||||||
@ -391,6 +412,41 @@ class ChatHandler(tornado.websocket.WebSocketHandler):
|
|||||||
return True
|
return True
|
||||||
return super().check_origin(origin)
|
return super().check_origin(origin)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def has_joined_room(self):
|
||||||
|
return self.room_id is not None
|
||||||
|
|
||||||
|
def send_message(self, cmd, data):
|
||||||
|
body = json.dumps({'cmd': cmd, 'data': data})
|
||||||
|
try:
|
||||||
|
self.write_message(body)
|
||||||
|
except tornado.websocket.WebSocketClosedError:
|
||||||
|
self.on_close()
|
||||||
|
|
||||||
|
async def on_join_room(self):
|
||||||
|
if self.application.settings['debug']:
|
||||||
|
await self.send_test_message()
|
||||||
|
|
||||||
|
# 不允许自动翻译的提示
|
||||||
|
if self.auto_translate:
|
||||||
|
cfg = config.get_config()
|
||||||
|
if cfg.allow_translate_rooms and self.room_id not in cfg.allow_translate_rooms:
|
||||||
|
self.send_message(Command.ADD_TEXT, make_text_message(
|
||||||
|
models.avatar.DEFAULT_AVATAR_URL,
|
||||||
|
int(time.time()),
|
||||||
|
'blivechat',
|
||||||
|
2,
|
||||||
|
'Translation is not allowed in this room, please download to use translation',
|
||||||
|
0,
|
||||||
|
False,
|
||||||
|
60,
|
||||||
|
False,
|
||||||
|
True,
|
||||||
|
0,
|
||||||
|
uuid.uuid4().hex,
|
||||||
|
''
|
||||||
|
))
|
||||||
|
|
||||||
# 测试用
|
# 测试用
|
||||||
async def send_test_message(self):
|
async def send_test_message(self):
|
||||||
base_data = {
|
base_data = {
|
||||||
@ -398,34 +454,21 @@ class ChatHandler(tornado.websocket.WebSocketHandler):
|
|||||||
'timestamp': int(time.time()),
|
'timestamp': int(time.time()),
|
||||||
'authorName': 'xfgryujk',
|
'authorName': 'xfgryujk',
|
||||||
}
|
}
|
||||||
text_data = [
|
text_data = make_text_message(
|
||||||
# 0: avatarUrl
|
|
||||||
base_data['avatarUrl'],
|
base_data['avatarUrl'],
|
||||||
# 1: timestamp
|
|
||||||
base_data['timestamp'],
|
base_data['timestamp'],
|
||||||
# 2: authorName
|
|
||||||
base_data['authorName'],
|
base_data['authorName'],
|
||||||
# 3: authorType
|
|
||||||
0,
|
0,
|
||||||
# 4: content
|
|
||||||
'我能吞下玻璃而不伤身体',
|
'我能吞下玻璃而不伤身体',
|
||||||
# 5: privilegeType
|
|
||||||
0,
|
0,
|
||||||
# 6: isGiftDanmaku
|
False,
|
||||||
0,
|
|
||||||
# 7: authorLevel
|
|
||||||
20,
|
20,
|
||||||
# 8: isNewbie
|
False,
|
||||||
|
True,
|
||||||
0,
|
0,
|
||||||
# 9: isMobileVerified
|
|
||||||
1,
|
|
||||||
# 10: medalLevel
|
|
||||||
0,
|
|
||||||
# 11: id
|
|
||||||
uuid.uuid4().hex,
|
uuid.uuid4().hex,
|
||||||
# 12: translation
|
|
||||||
''
|
''
|
||||||
]
|
)
|
||||||
member_data = {
|
member_data = {
|
||||||
**base_data,
|
**base_data,
|
||||||
'id': uuid.uuid4().hex,
|
'id': uuid.uuid4().hex,
|
||||||
@ -463,14 +506,3 @@ class ChatHandler(tornado.websocket.WebSocketHandler):
|
|||||||
gift_data['totalCoin'] = 1245000
|
gift_data['totalCoin'] = 1245000
|
||||||
gift_data['giftName'] = '小电视飞船'
|
gift_data['giftName'] = '小电视飞船'
|
||||||
self.send_message(Command.ADD_GIFT, gift_data)
|
self.send_message(Command.ADD_GIFT, gift_data)
|
||||||
|
|
||||||
@property
|
|
||||||
def has_joined_room(self):
|
|
||||||
return self.room_id is not None
|
|
||||||
|
|
||||||
def send_message(self, cmd, data):
|
|
||||||
body = json.dumps({'cmd': cmd, 'data': data})
|
|
||||||
try:
|
|
||||||
self.write_message(body)
|
|
||||||
except tornado.websocket.WebSocketClosedError:
|
|
||||||
self.on_close()
|
|
||||||
|
21
config.py
21
config.py
@ -13,14 +13,20 @@ _config: Optional['AppConfig'] = None
|
|||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
reload()
|
if reload():
|
||||||
|
return
|
||||||
|
logger.warning('Using default config')
|
||||||
|
global _config
|
||||||
|
_config = AppConfig()
|
||||||
|
|
||||||
|
|
||||||
def reload():
|
def reload():
|
||||||
config = AppConfig()
|
config = AppConfig()
|
||||||
if config.load(CONFIG_PATH):
|
if not config.load(CONFIG_PATH):
|
||||||
global _config
|
return False
|
||||||
_config = config
|
global _config
|
||||||
|
_config = config
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def get_config():
|
def get_config():
|
||||||
@ -31,6 +37,7 @@ class AppConfig:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.database_url = 'sqlite:///data/database.db'
|
self.database_url = 'sqlite:///data/database.db'
|
||||||
self.enable_translate = True
|
self.enable_translate = True
|
||||||
|
self.allow_translate_rooms = {}
|
||||||
|
|
||||||
def load(self, path):
|
def load(self, path):
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
@ -39,6 +46,12 @@ class AppConfig:
|
|||||||
app_section = config['app']
|
app_section = config['app']
|
||||||
self.database_url = app_section['database_url']
|
self.database_url = app_section['database_url']
|
||||||
self.enable_translate = app_section.getboolean('enable_translate')
|
self.enable_translate = app_section.getboolean('enable_translate')
|
||||||
|
allow_translate_rooms = app_section['allow_translate_rooms'].strip()
|
||||||
|
if allow_translate_rooms == '':
|
||||||
|
self.allow_translate_rooms = {}
|
||||||
|
else:
|
||||||
|
allow_translate_rooms = allow_translate_rooms.split(',')
|
||||||
|
self.allow_translate_rooms = set(map(lambda id_: int(id_.strip()), allow_translate_rooms))
|
||||||
except (KeyError, ValueError):
|
except (KeyError, ValueError):
|
||||||
logger.exception('Failed to load config:')
|
logger.exception('Failed to load config:')
|
||||||
return False
|
return False
|
||||||
|
@ -3,9 +3,13 @@
|
|||||||
database_url = sqlite:///data/database.db
|
database_url = sqlite:///data/database.db
|
||||||
# Enable auto translate to Japanese
|
# Enable auto translate to Japanese
|
||||||
enable_translate = true
|
enable_translate = true
|
||||||
|
# Comma separated room IDs in which translation are not allowed. If empty, all are allowed
|
||||||
|
# Example: allow_translate_rooms = 4895312,22347054,21693691
|
||||||
|
allow_translate_rooms =
|
||||||
|
|
||||||
|
|
||||||
# DON'T modify this section
|
# DON'T modify this section
|
||||||
[DEFAULT]
|
[DEFAULT]
|
||||||
database_url = sqlite:///data/database.db
|
database_url = sqlite:///data/database.db
|
||||||
enable_translate = true
|
enable_translate = true
|
||||||
|
allow_translate_rooms =
|
||||||
|
Loading…
Reference in New Issue
Block a user