mirror of
https://github.com/xfgryujk/blivechat.git
synced 2026-08-23 20:03:28 +08:00
Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c8e4278c39 | ||
|
|
07b47a26ca | ||
|
|
8d55331e6c | ||
|
|
cae06858fc | ||
|
|
8d4e8e6f35 | ||
|
|
8d40f9f9e5 | ||
|
|
e93f6b2383 | ||
|
|
4c2e216191 | ||
|
|
2a35541dc5 | ||
|
|
a927282e77 | ||
|
|
a7faac5425 | ||
|
|
a22496c355 | ||
|
|
20ec58b965 | ||
|
|
7453d7e890 | ||
|
|
1991e33b9d | ||
|
|
f429a6a03d | ||
|
|
4b98c56965 | ||
|
|
eb43a37ae5 | ||
|
|
1cf6d88460 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -105,3 +105,4 @@ venv.bak/
|
||||
|
||||
|
||||
.idea/
|
||||
data/database.db
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* 兼容YouTube直播评论栏的样式
|
||||
* 金瓜子礼物模仿醒目留言显示
|
||||
* 高亮舰队、房管、主播的用户名
|
||||
* 支持屏蔽弹幕、限制最大速度等设置
|
||||
* 支持屏蔽弹幕、合并相似弹幕等设置
|
||||
* 自带样式生成器
|
||||
|
||||
## 使用方法
|
||||
@@ -23,7 +23,7 @@
|
||||
```
|
||||
3. 用浏览器打开[http://localhost:12450](http://localhost:12450),输入房间ID,保存配置,复制房间URL
|
||||
4. 用样式生成器生成样式,复制CSS
|
||||
5. 在OBS中添加浏览器源,输入URL和自定义CSS,或者可以在首页的样式设置里输入CSS
|
||||
5. 在OBS中添加浏览器源,输入URL和自定义CSS
|
||||
|
||||
### 公共服务器
|
||||
请优先在本地使用,使用公共服务器会有更大的弹幕延迟,而且服务器故障时可能出现直播事故
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import asyncio
|
||||
import datetime
|
||||
import enum
|
||||
import json
|
||||
import logging
|
||||
@@ -12,6 +11,7 @@ import aiohttp
|
||||
import tornado.websocket
|
||||
|
||||
import blivedm.blivedm as blivedm
|
||||
import models.avatar
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -26,77 +26,70 @@ class Command(enum.IntEnum):
|
||||
DEL_SUPER_CHAT = 6
|
||||
|
||||
|
||||
DEFAULT_AVATAR_URL = 'https://static.hdslb.com/images/member/noface.gif'
|
||||
|
||||
_http_session = aiohttp.ClientSession()
|
||||
_avatar_url_cache: Dict[int, str] = {}
|
||||
_last_fetch_avatar_time = datetime.datetime.now()
|
||||
_last_avatar_failed_time = None
|
||||
_uids_to_fetch_avatar = asyncio.Queue(15)
|
||||
|
||||
room_manager: Optional['RoomManager'] = None
|
||||
|
||||
|
||||
async def get_avatar_url(user_id):
|
||||
if user_id in _avatar_url_cache:
|
||||
return _avatar_url_cache[user_id]
|
||||
|
||||
global _last_avatar_failed_time, _last_fetch_avatar_time
|
||||
cur_time = datetime.datetime.now()
|
||||
# 防止获取头像频率太高被ban
|
||||
if (cur_time - _last_fetch_avatar_time).total_seconds() < 0.2:
|
||||
# 由_fetch_avatar_loop过一段时间再获取并缓存
|
||||
try:
|
||||
_uids_to_fetch_avatar.put_nowait(user_id)
|
||||
except asyncio.QueueFull:
|
||||
pass
|
||||
return DEFAULT_AVATAR_URL
|
||||
|
||||
if _last_avatar_failed_time is not None:
|
||||
if (cur_time - _last_avatar_failed_time).total_seconds() < 3 * 60 + 3:
|
||||
# 3分钟以内被ban,解封大约要15分钟
|
||||
return DEFAULT_AVATAR_URL
|
||||
else:
|
||||
_last_avatar_failed_time = None
|
||||
|
||||
_last_fetch_avatar_time = cur_time
|
||||
try:
|
||||
async with _http_session.get('https://api.bilibili.com/x/space/acc/info',
|
||||
params={'mid': user_id}) as r:
|
||||
if r.status != 200: # 可能会被B站ban
|
||||
logger.warning('Failed to fetch avatar: status=%d %s uid=%d', r.status, r.reason, user_id)
|
||||
_last_avatar_failed_time = cur_time
|
||||
return DEFAULT_AVATAR_URL
|
||||
data = await r.json()
|
||||
except aiohttp.ClientConnectionError:
|
||||
return DEFAULT_AVATAR_URL
|
||||
url = data['data']['face']
|
||||
if not url.endswith('noface.gif'):
|
||||
url += '@48w_48h'
|
||||
_avatar_url_cache[user_id] = url
|
||||
|
||||
if len(_avatar_url_cache) > 50000:
|
||||
for _, key in zip(range(100), _avatar_url_cache):
|
||||
del _avatar_url_cache[key]
|
||||
|
||||
return url
|
||||
|
||||
|
||||
async def _fetch_avatar_loop():
|
||||
while True:
|
||||
try:
|
||||
user_id = await _uids_to_fetch_avatar.get()
|
||||
if user_id in _avatar_url_cache:
|
||||
continue
|
||||
# 延时长一些使实时弹幕有机会获取头像
|
||||
await asyncio.sleep(0.4 - (datetime.datetime.now() - _last_fetch_avatar_time).total_seconds())
|
||||
asyncio.ensure_future(get_avatar_url(user_id))
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
asyncio.ensure_future(_fetch_avatar_loop())
|
||||
def init():
|
||||
global room_manager
|
||||
room_manager = RoomManager()
|
||||
|
||||
|
||||
class Room(blivedm.BLiveClient):
|
||||
# 重新定义parse_XXX是为了减少对字段名的依赖,防止B站改字段名
|
||||
def __parse_danmaku(self, command):
|
||||
info = command['info']
|
||||
if info[3]:
|
||||
room_id = info[3][3]
|
||||
medal_level = info[3][0]
|
||||
else:
|
||||
room_id = medal_level = 0
|
||||
return self._on_receive_danmaku(blivedm.DanmakuMessage(
|
||||
None, None, None, info[0][4], None, None, info[0][9], None,
|
||||
info[1],
|
||||
info[2][0], info[2][1], info[2][2], None, None, info[2][5], info[2][6], None,
|
||||
medal_level, None, None, room_id, None, None,
|
||||
info[4][0], None, None,
|
||||
None, None,
|
||||
info[7]
|
||||
))
|
||||
|
||||
def __parse_gift(self, command):
|
||||
data = command['data']
|
||||
return self._on_receive_gift(blivedm.GiftMessage(
|
||||
data['giftName'], data['num'], data['uname'], data['face'], None,
|
||||
data['uid'], data['timestamp'], None, None,
|
||||
None, None, None, data['coin_type'], data['total_coin']
|
||||
))
|
||||
|
||||
def __parse_buy_guard(self, command):
|
||||
data = command['data']
|
||||
return self._on_buy_guard(blivedm.GuardBuyMessage(
|
||||
data['uid'], data['username'], None, None, None,
|
||||
None, None, data['start_time'], None
|
||||
))
|
||||
|
||||
def __parse_super_chat(self, command):
|
||||
data = command['data']
|
||||
return self._on_super_chat(blivedm.SuperChatMessage(
|
||||
data['price'], data['message'], None, data['start_time'],
|
||||
None, None, data['id'], None,
|
||||
None, data['uid'], data['user_info']['uname'],
|
||||
data['user_info']['face'], None,
|
||||
None, None,
|
||||
None, None, None,
|
||||
None
|
||||
))
|
||||
|
||||
_COMMAND_HANDLERS = {
|
||||
**blivedm.BLiveClient._COMMAND_HANDLERS,
|
||||
'DANMU_MSG': __parse_danmaku,
|
||||
'SEND_GIFT': __parse_gift,
|
||||
'GUARD_BUY': __parse_buy_guard,
|
||||
'SUPER_CHAT_MESSAGE': __parse_super_chat
|
||||
}
|
||||
|
||||
def __init__(self, room_id):
|
||||
super().__init__(room_id, session=_http_session, heartbeat_interval=10)
|
||||
self.clients: List['ChatHandler'] = []
|
||||
@@ -111,7 +104,10 @@ class Room(blivedm.BLiveClient):
|
||||
def send_message(self, cmd, data):
|
||||
body = json.dumps({'cmd': cmd, 'data': data})
|
||||
for client in self.clients:
|
||||
client.write_message(body)
|
||||
try:
|
||||
client.write_message(body)
|
||||
except tornado.websocket.WebSocketClosedError:
|
||||
pass
|
||||
|
||||
async def _on_receive_danmaku(self, danmaku: blivedm.DanmakuMessage):
|
||||
asyncio.ensure_future(self.__on_receive_danmaku(danmaku))
|
||||
@@ -126,7 +122,7 @@ class Room(blivedm.BLiveClient):
|
||||
else:
|
||||
author_type = 0
|
||||
self.send_message(Command.ADD_TEXT, {
|
||||
'avatarUrl': await get_avatar_url(danmaku.uid),
|
||||
'avatarUrl': await models.avatar.get_avatar_url(danmaku.uid),
|
||||
'timestamp': danmaku.timestamp,
|
||||
'authorName': danmaku.uname,
|
||||
'authorType': author_type,
|
||||
@@ -140,10 +136,12 @@ class Room(blivedm.BLiveClient):
|
||||
})
|
||||
|
||||
async def _on_receive_gift(self, gift: blivedm.GiftMessage):
|
||||
avatar_url = gift.face.replace('http:', '').replace('https:', '')
|
||||
models.avatar.update_avatar_cache(gift.uid, avatar_url)
|
||||
if gift.coin_type != 'gold': # 丢人
|
||||
return
|
||||
self.send_message(Command.ADD_GIFT, {
|
||||
'avatarUrl': gift.face,
|
||||
'avatarUrl': avatar_url,
|
||||
'timestamp': gift.timestamp,
|
||||
'authorName': gift.uname,
|
||||
'giftName': gift.gift_name,
|
||||
@@ -156,14 +154,16 @@ class Room(blivedm.BLiveClient):
|
||||
|
||||
async def __on_buy_guard(self, message: blivedm.GuardBuyMessage):
|
||||
self.send_message(Command.ADD_MEMBER, {
|
||||
'avatarUrl': await get_avatar_url(message.uid),
|
||||
'avatarUrl': await models.avatar.get_avatar_url(message.uid),
|
||||
'timestamp': message.start_time,
|
||||
'authorName': message.username
|
||||
})
|
||||
|
||||
async def _on_super_chat(self, message: blivedm.SuperChatMessage):
|
||||
avatar_url = message.face.replace('http:', '').replace('https:', '')
|
||||
models.avatar.update_avatar_cache(message.uid, avatar_url)
|
||||
self.send_message(Command.ADD_SUPER_CHAT, {
|
||||
'avatarUrl': message.face,
|
||||
'avatarUrl': avatar_url,
|
||||
'timestamp': message.start_time,
|
||||
'authorName': message.uname,
|
||||
'price': message.price,
|
||||
@@ -226,9 +226,6 @@ class RoomManager:
|
||||
del self._rooms[room_id]
|
||||
|
||||
|
||||
room_manager = RoomManager()
|
||||
|
||||
|
||||
# noinspection PyAbstractClass
|
||||
class ChatHandler(tornado.websocket.WebSocketHandler):
|
||||
def __init__(self, *args, **kwargs):
|
||||
@@ -266,7 +263,7 @@ class ChatHandler(tornado.websocket.WebSocketHandler):
|
||||
# 测试用
|
||||
def send_test_message(self):
|
||||
base_data = {
|
||||
'avatarUrl': 'https://i0.hdslb.com/bfs/face/29b6be8aa611e70a3d3ac219cdaf5e72b604f2de.jpg@48w_48h',
|
||||
'avatarUrl': '//i0.hdslb.com/bfs/face/29b6be8aa611e70a3d3ac219cdaf5e72b604f2de.jpg@48w_48h',
|
||||
'timestamp': time.time(),
|
||||
'authorName': 'xfgryujk',
|
||||
}
|
||||
@@ -312,4 +309,7 @@ class ChatHandler(tornado.websocket.WebSocketHandler):
|
||||
|
||||
def send_message(self, cmd, data):
|
||||
body = json.dumps({'cmd': cmd, 'data': data})
|
||||
self.write_message(body)
|
||||
try:
|
||||
self.write_message(body)
|
||||
except tornado.websocket.WebSocketClosedError:
|
||||
pass
|
||||
2
blivedm
2
blivedm
Submodule blivedm updated: 87651f044c...15669a2084
43
config.py
Normal file
43
config.py
Normal file
@@ -0,0 +1,43 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import configparser
|
||||
import logging
|
||||
import os
|
||||
from typing import *
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CONFIG_PATH = os.path.join('data', 'config.ini')
|
||||
|
||||
_config: Optional['AppConfig'] = None
|
||||
|
||||
|
||||
def init():
|
||||
reload()
|
||||
|
||||
|
||||
def reload():
|
||||
config = AppConfig()
|
||||
if config.load(CONFIG_PATH):
|
||||
global _config
|
||||
_config = config
|
||||
|
||||
|
||||
def get_config():
|
||||
return _config
|
||||
|
||||
|
||||
class AppConfig:
|
||||
def __init__(self):
|
||||
self.database_url = 'sqlite:///data/database.db'
|
||||
|
||||
def load(self, path):
|
||||
config = configparser.ConfigParser()
|
||||
config.read(path)
|
||||
try:
|
||||
app_section = config['app']
|
||||
self.database_url = app_section['database_url']
|
||||
except (KeyError, ValueError):
|
||||
logger.exception('Failed to load config:')
|
||||
return False
|
||||
return True
|
||||
8
data/config.ini
Normal file
8
data/config.ini
Normal file
@@ -0,0 +1,8 @@
|
||||
[app]
|
||||
# See https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls
|
||||
database_url = sqlite:///data/database.db
|
||||
|
||||
|
||||
# DON'T modify this section
|
||||
[DEFAULT]
|
||||
database_url = sqlite:///data/database.db
|
||||
6
frontend/package-lock.json
generated
6
frontend/package-lock.json
generated
@@ -9723,6 +9723,7 @@
|
||||
"fsevents": {
|
||||
"version": "1.2.9",
|
||||
"resolved": "https://registry.npm.taobao.org/fsevents/download/fsevents-1.2.9.tgz",
|
||||
"integrity": "sha512-oeyj2H3EjjonWcFjD5NvZNE9Rqe4UW+nQBU2HNeKw0koVLEFIhtyETyAakeAM3de7Z/SW5kcA+fZUait9EApnw==",
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"requires": {
|
||||
@@ -18954,6 +18955,11 @@
|
||||
"integrity": "sha1-qVT5Ma66UI0we78Gnv8MAclhFvc=",
|
||||
"dev": true
|
||||
},
|
||||
"serialize-javascript": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-2.1.1.tgz",
|
||||
"integrity": "sha512-MPLPRpD4FNqWq9tTIjYG5LesFouDhdyH0EPY3gVK4DRD5+g4aDqdNSzLIwceulo3Yj+PL1bPh6laE5+H6LTcrQ=="
|
||||
},
|
||||
"serve-index": {
|
||||
"version": "1.9.1",
|
||||
"resolved": "https://registry.npm.taobao.org/serve-index/download/serve-index-1.9.1.tgz",
|
||||
|
||||
BIN
frontend/public/static/img/tutorial/tutorial-1.png
Normal file
BIN
frontend/public/static/img/tutorial/tutorial-1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 69 KiB |
BIN
frontend/public/static/img/tutorial/tutorial-2.png
Normal file
BIN
frontend/public/static/img/tutorial/tutorial-2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 78 KiB |
BIN
frontend/public/static/img/tutorial/tutorial-3.png
Normal file
BIN
frontend/public/static/img/tutorial/tutorial-3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 166 KiB |
BIN
frontend/public/static/img/tutorial/tutorial-4.png
Normal file
BIN
frontend/public/static/img/tutorial/tutorial-4.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
BIN
frontend/public/static/img/tutorial/tutorial-5.png
Normal file
BIN
frontend/public/static/img/tutorial/tutorial-5.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 78 KiB |
@@ -1,5 +1,3 @@
|
||||
import axios from 'axios'
|
||||
|
||||
import {mergeConfig} from '@/utils'
|
||||
|
||||
export const DEFAULT_CONFIG = {
|
||||
@@ -15,9 +13,7 @@ export const DEFAULT_CONFIG = {
|
||||
blockNotMobileVerified: true,
|
||||
blockKeywords: '',
|
||||
blockUsers: '',
|
||||
blockMedalLevel: 0,
|
||||
|
||||
css: ''
|
||||
blockMedalLevel: 0
|
||||
}
|
||||
|
||||
export function setLocalConfig (config) {
|
||||
@@ -31,27 +27,3 @@ export function getLocalConfig () {
|
||||
}
|
||||
return mergeConfig(JSON.parse(window.localStorage.config), DEFAULT_CONFIG)
|
||||
}
|
||||
|
||||
export async function createRemoteConfig (config) {
|
||||
config = mergeConfig(config, DEFAULT_CONFIG)
|
||||
return (await axios.post('/config', config)).data
|
||||
}
|
||||
|
||||
export async function setRemoteConfig (id, config) {
|
||||
config = mergeConfig(config, DEFAULT_CONFIG)
|
||||
return (await axios.put(`/config/${id}`, config)).data
|
||||
}
|
||||
|
||||
export async function getRemoteConfig (id) {
|
||||
let config = (await axios.get(`/config/${id}`)).data
|
||||
return mergeConfig(config, DEFAULT_CONFIG)
|
||||
}
|
||||
|
||||
export default {
|
||||
DEFAULT_CONFIG,
|
||||
setLocalConfig,
|
||||
getLocalConfig,
|
||||
createRemoteConfig,
|
||||
setRemoteConfig,
|
||||
getRemoteConfig
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
<script>
|
||||
import ImgShadow from './ImgShadow.vue'
|
||||
import utils from '@/utils'
|
||||
import * as utils from '@/utils'
|
||||
|
||||
export default {
|
||||
name: 'LegacyPaidMessage',
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
<script>
|
||||
import ImgShadow from './ImgShadow.vue'
|
||||
import * as constants from './constants'
|
||||
import utils from '@/utils'
|
||||
import * as utils from '@/utils'
|
||||
|
||||
export default {
|
||||
name: 'PaidMessage',
|
||||
|
||||
@@ -17,10 +17,12 @@
|
||||
></author-badge>
|
||||
</span>
|
||||
</yt-live-chat-author-chip>
|
||||
<span id="message" class="style-scope yt-live-chat-text-message-renderer">{{content}}</span>
|
||||
<el-badge :value="repeated" :max="99" v-show="repeated > 1" class="style-scope yt-live-chat-text-message-renderer"
|
||||
:style="{'--repeated-mark-color': repeatedMarkColor}"
|
||||
></el-badge>
|
||||
<span id="message" class="style-scope yt-live-chat-text-message-renderer">
|
||||
{{content}}
|
||||
<el-badge :value="repeated" :max="99" v-show="repeated > 1" class="style-scope yt-live-chat-text-message-renderer"
|
||||
:style="{'--repeated-mark-color': repeatedMarkColor}"
|
||||
></el-badge>
|
||||
</span>
|
||||
</div>
|
||||
</yt-live-chat-text-message-renderer>
|
||||
</template>
|
||||
@@ -29,7 +31,7 @@
|
||||
import ImgShadow from './ImgShadow.vue'
|
||||
import AuthorBadge from './AuthorBadge.vue'
|
||||
import * as constants from './constants'
|
||||
import utils from '@/utils'
|
||||
import * as utils from '@/utils'
|
||||
|
||||
// HSL
|
||||
const REPEATED_MARK_COLOR_START = [210, 100.0, 62.5]
|
||||
@@ -77,11 +79,11 @@ export default {
|
||||
</script>
|
||||
|
||||
<style>
|
||||
yt-live-chat-text-message-renderer>#content>.el-badge {
|
||||
margin-left: 10px;
|
||||
yt-live-chat-text-message-renderer>#content>#message>.el-badge {
|
||||
margin-left: 5px;
|
||||
}
|
||||
|
||||
yt-live-chat-text-message-renderer>#content>.el-badge .el-badge__content {
|
||||
yt-live-chat-text-message-renderer>#content>#message>.el-badge .el-badge__content {
|
||||
font-size: 12px !important;
|
||||
line-height: 18px !important;
|
||||
text-shadow: none !important;
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
<yt-live-chat-item-list-renderer class="style-scope yt-live-chat-renderer" allow-scroll>
|
||||
<div id="item-scroller" ref="scroller" class="style-scope yt-live-chat-item-list-renderer animated" @scroll="onScroll">
|
||||
<div ref="itemOffset" id="item-offset" class="style-scope yt-live-chat-item-list-renderer" style="height: 0px;">
|
||||
<div ref="items" id="items" class="style-scope yt-live-chat-item-list-renderer" style="overflow: hidden; transform: translateY(0px);">
|
||||
<div ref="items" id="items" class="style-scope yt-live-chat-item-list-renderer" style="overflow: hidden"
|
||||
:style="{transform: `translateY(${Math.floor(scrollPixelsRemaining)}px)`}"
|
||||
>
|
||||
<template v-for="message in messages">
|
||||
<text-message :key="message.id" v-if="message.type === MESSAGE_TYPE_TEXT"
|
||||
class="style-scope yt-live-chat-item-list-renderer"
|
||||
@@ -31,7 +33,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import config from '@/api/config'
|
||||
import * as config from '@/api/config'
|
||||
import Ticker from './Ticker.vue'
|
||||
import TextMessage from './TextMessage.vue'
|
||||
import LegacyPaidMessage from './LegacyPaidMessage.vue'
|
||||
@@ -102,6 +104,7 @@ export default {
|
||||
window.clearTimeout(this.emitSmoothedMessageTimerId)
|
||||
this.emitSmoothedMessageTimerId = null
|
||||
}
|
||||
this.clearMessages()
|
||||
},
|
||||
watch: {
|
||||
css(val) {
|
||||
@@ -115,28 +118,65 @@ export default {
|
||||
addMessages(messages) {
|
||||
this.enqueueMessages(messages)
|
||||
},
|
||||
mergeSimilar(content) {
|
||||
let remainNum = 5
|
||||
for (let arr of [this.messagesBuffer, this.messages]) {
|
||||
for (let i = arr.length - 1; i >= 0 && --remainNum > 0; i--) {
|
||||
let message = arr[i]
|
||||
let longer, shorter
|
||||
if (message.content.length > content.length) {
|
||||
longer = message.content
|
||||
shorter = content
|
||||
} else {
|
||||
longer = content
|
||||
shorter = message.content
|
||||
}
|
||||
if (longer.indexOf(shorter) !== -1 // 长的包含短的
|
||||
&& longer.length - shorter.length < shorter.length // 长度差较小
|
||||
) {
|
||||
message.repeated++
|
||||
return true
|
||||
mergeSimilarText(content) {
|
||||
content = content.trim().toLowerCase()
|
||||
let res = false
|
||||
this.forEachRecentMessage(5, message => {
|
||||
if (message.type !== constants.MESSAGE_TYPE_TEXT) {
|
||||
return true
|
||||
}
|
||||
let messageContent = message.content.trim().toLowerCase()
|
||||
let longer, shorter
|
||||
if (messageContent.length > content.length) {
|
||||
longer = messageContent
|
||||
shorter = content
|
||||
} else {
|
||||
longer = content
|
||||
shorter = messageContent
|
||||
}
|
||||
if (longer.indexOf(shorter) !== -1 // 长的包含短的
|
||||
&& longer.length - shorter.length < shorter.length // 长度差较小
|
||||
) {
|
||||
message.repeated++
|
||||
res = true
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return res
|
||||
},
|
||||
mergeSimilarGift(authorName, price) {
|
||||
let res = false
|
||||
this.forEachRecentMessage(5, message => {
|
||||
if (message.type === constants.MESSAGE_TYPE_SUPER_CHAT
|
||||
&& message.content === ''
|
||||
&& message.authorName === authorName
|
||||
) {
|
||||
message.price += price
|
||||
res = true
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
return res
|
||||
},
|
||||
forEachRecentMessage(num, callback) {
|
||||
// 从新到老遍历num条消息
|
||||
for (let i = this.smoothedMessageQueue.length - 1; i >= 0 && num > 0; i--) {
|
||||
let messageGroup = this.smoothedMessageQueue[i]
|
||||
for (let j = messageGroup.length - 1; j >= 0 && num-- > 0; j--) {
|
||||
if (!callback(messageGroup[j])) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
for (let arr of [this.messagesBuffer, this.messages]) {
|
||||
for (let i = arr.length - 1; i >= 0 && num-- > 0; i--) {
|
||||
if (!callback(arr[i])) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
},
|
||||
delMessage(id) {
|
||||
this.delMessages([id])
|
||||
@@ -152,12 +192,26 @@ export default {
|
||||
clearMessages() {
|
||||
this.messages = []
|
||||
this.paidMessages = []
|
||||
this.smoothedMessageQueue = []
|
||||
this.messagesBuffer = []
|
||||
this.isSmoothed = true
|
||||
this.lastSmoothChatMessageAddMs = null
|
||||
this.chatRateMs = 1000
|
||||
this.lastSmoothScrollUpdate = null
|
||||
this.scrollTimeRemainingMs = this.scrollPixelsRemaining = 0
|
||||
this.smoothScrollRafHandle = null
|
||||
this.preinsertHeight = 0
|
||||
this.maybeResizeScrollContainer()
|
||||
if (!this.atBottom) {
|
||||
this.scrollToBottom()
|
||||
}
|
||||
},
|
||||
|
||||
enqueueMessages(messages) {
|
||||
if (this.lastEnqueueTime) {
|
||||
let interval = new Date() - this.lastEnqueueTime
|
||||
if (interval > 0) {
|
||||
// 理论上B站发包间隔1S,如果不过滤间隔太短的会导致消息平滑失效
|
||||
if (interval > 100) {
|
||||
this.enqueueIntervals.push(interval)
|
||||
if (this.enqueueIntervals.length > 5) {
|
||||
this.enqueueIntervals.splice(0, this.enqueueIntervals.length - 5)
|
||||
@@ -263,7 +317,7 @@ export default {
|
||||
}
|
||||
this.messagesBuffer.push(message)
|
||||
if (message.type !== constants.MESSAGE_TYPE_TEXT) {
|
||||
this.paidMessages.push(message)
|
||||
this.paidMessages.unshift(message)
|
||||
}
|
||||
},
|
||||
handleDelMessage(message) {
|
||||
@@ -315,7 +369,6 @@ export default {
|
||||
// 计算剩余像素
|
||||
this.scrollPixelsRemaining += this.$refs.items.clientHeight - this.preinsertHeight
|
||||
this.scrollToBottom()
|
||||
this.$refs.items.style.transform = `translateY(${Math.floor(this.scrollPixelsRemaining)}px)`
|
||||
|
||||
// 计算是否平滑滚动、剩余时间
|
||||
if (!this.lastSmoothChatMessageAddMs) {
|
||||
@@ -354,7 +407,6 @@ export default {
|
||||
|| this.scrollTimeRemainingMs <= 0 // 时间已结束
|
||||
) {
|
||||
this.resetSmoothScroll()
|
||||
this.$refs.items.style.transform = 'translateY(0px)'
|
||||
return
|
||||
}
|
||||
|
||||
@@ -369,7 +421,6 @@ export default {
|
||||
}
|
||||
this.lastSmoothScrollUpdate = time
|
||||
this.smoothScrollRafHandle = window.requestAnimationFrame(this.smoothScroll)
|
||||
this.$refs.items.style.transform = `translateY(${Math.floor(this.scrollPixelsRemaining)}px)`
|
||||
},
|
||||
resetSmoothScroll() {
|
||||
this.scrollTimeRemainingMs = this.scrollPixelsRemaining = 0
|
||||
@@ -390,7 +441,7 @@ export default {
|
||||
}
|
||||
},
|
||||
scrollToBottom() {
|
||||
this.$refs.scroller.scrollTop = this.$refs.scroller.scrollHeight
|
||||
this.$refs.scroller.scrollTop = Math.pow(2, 24)
|
||||
this.atBottom = true
|
||||
},
|
||||
onScroll() {
|
||||
|
||||
@@ -2,6 +2,7 @@ export default {
|
||||
sidebar: {
|
||||
home: 'Home',
|
||||
stylegen: 'Style generator',
|
||||
help: 'Help',
|
||||
projectAddress: 'Project address',
|
||||
giftRecord: 'Super Chat record'
|
||||
},
|
||||
@@ -27,17 +28,12 @@ export default {
|
||||
blockUsers: 'Block users',
|
||||
blockMedalLevel: 'Block medal level lower than',
|
||||
|
||||
style: 'Style',
|
||||
|
||||
roomUrl: 'Room URL',
|
||||
copy: 'Copy',
|
||||
saveConfig: 'Save config',
|
||||
enterRoom: 'Enter room',
|
||||
exportConfig: 'Export config',
|
||||
importConfig: 'Import config',
|
||||
|
||||
failedToSave: 'Failed to save: ',
|
||||
successfullySaved: 'Successfully saved',
|
||||
failedToParseConfig: 'Failed to parse config: '
|
||||
},
|
||||
stylegen: {
|
||||
@@ -106,5 +102,13 @@ export default {
|
||||
result: 'Result',
|
||||
copy: 'Copy',
|
||||
resetConfig: 'Reset config'
|
||||
},
|
||||
help: {
|
||||
help: 'Help',
|
||||
p1: '1. Copy the room ID from the Bilibili live room webpage',
|
||||
p2: '2. Enter the room ID into the room ID on the home page. Copy the room URL after saving the configuration',
|
||||
p3: '3. Generate styles with the style generator. Copy the CSS',
|
||||
p4: '4. Add browser source in OBS',
|
||||
p5: '5. Enter the previously copied room URL at URL, and enter the previously copied CSS at custom CSS'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
export default {
|
||||
export default {
|
||||
sidebar: {
|
||||
home: 'トップページ',
|
||||
stylegen: 'スタイルジェネレータ',
|
||||
help: 'ヘルプ',
|
||||
projectAddress: 'プロジェクトアドレス',
|
||||
giftRecord: 'スーパーチャット記録'
|
||||
},
|
||||
@@ -27,17 +28,12 @@
|
||||
blockUsers: 'ブロックユーザー',
|
||||
blockMedalLevel: 'ブロック勲章等級がx未満',
|
||||
|
||||
style: 'スタイル',
|
||||
|
||||
roomUrl: 'ルームのURL',
|
||||
copy: 'コピー',
|
||||
saveConfig: 'コンフィグを保存する',
|
||||
enterRoom: 'ルームに入る',
|
||||
exportConfig: 'コンフィグの導出',
|
||||
importConfig: 'コンフィグの導入',
|
||||
|
||||
failedToSave: '保存に失敗しました:',
|
||||
successfullySaved: '保存に成功しました',
|
||||
failedToParseConfig: 'コンフィグ解析に失敗しました'
|
||||
},
|
||||
stylegen: {
|
||||
@@ -106,5 +102,13 @@
|
||||
result: '結果',
|
||||
copy: 'コピー',
|
||||
resetConfig: 'デフォルトに戻す'
|
||||
},
|
||||
help: {
|
||||
help: 'ヘルプ',
|
||||
p1: '1. ビリビリの生放送ウェブから生放送ルームIDをこぴーする',
|
||||
p2: '2. ホームページでコピーしたIDを入力し、配置を保存すると、ルームのURLをこぴーする',
|
||||
p3: '3. スタイルジェネレータでお好みのコメント様子を選び、出力したCSSをコピーする',
|
||||
p4: '4. OBSでブラウザを新規作成する',
|
||||
p5: '5. プロパティでこぴーしたURLを入力し、カスタムCSSでスタイルジェネレータのCSSを入力する'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ export default {
|
||||
sidebar: {
|
||||
home: '首页',
|
||||
stylegen: '样式生成器',
|
||||
help: '帮助',
|
||||
projectAddress: '项目地址',
|
||||
giftRecord: '打赏记录'
|
||||
},
|
||||
@@ -27,17 +28,12 @@ export default {
|
||||
blockUsers: '屏蔽用户',
|
||||
blockMedalLevel: '屏蔽当前直播间勋章等级低于',
|
||||
|
||||
style: '样式',
|
||||
|
||||
roomUrl: '房间URL',
|
||||
copy: '复制',
|
||||
saveConfig: '保存配置',
|
||||
enterRoom: '进入房间',
|
||||
exportConfig: '导出配置',
|
||||
importConfig: '导入配置',
|
||||
|
||||
failedToSave: '保存失败:',
|
||||
successfullySaved: '保存成功',
|
||||
failedToParseConfig: '配置解析失败:'
|
||||
},
|
||||
stylegen: {
|
||||
@@ -90,8 +86,8 @@ export default {
|
||||
scContentLineLineHeight: 'Super Chat内容行高(0为默认)',
|
||||
scContentLineColor: 'Super Chat内容颜色',
|
||||
showNewMemberBg: '显示新舰长背景',
|
||||
showScTicker: '显示Super Chat贴纸',
|
||||
showOtherThings: '显示Super Chat贴纸之外的内容',
|
||||
showScTicker: '显示Super Chat固定栏',
|
||||
showOtherThings: '显示Super Chat固定栏之外的内容',
|
||||
|
||||
animation: '动画',
|
||||
animateIn: '进入动画',
|
||||
@@ -106,5 +102,13 @@ export default {
|
||||
result: '结果',
|
||||
copy: '复制',
|
||||
resetConfig: '恢复默认设置'
|
||||
},
|
||||
help: {
|
||||
help: '帮助',
|
||||
p1: '1. 从B站直播间网页复制房间ID',
|
||||
p2: '2. 把房间ID输入到首页的房间ID,保存配置后复制房间URL',
|
||||
p3: '3. 使用样式生成器生成样式,复制CSS',
|
||||
p4: '4. 在OBS中添加浏览器源',
|
||||
p5: '5. URL处输入之前复制的房间URL,自定义CSS处输入之前复制的CSS'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
<el-menu-item :index="$router.resolve({name: 'stylegen'}).href">
|
||||
<i class="el-icon-brush"></i>{{$t('sidebar.stylegen')}}
|
||||
</el-menu-item>
|
||||
<el-menu-item :index="$router.resolve({name: 'help'}).href">
|
||||
<i class="el-icon-question"></i>{{$t('sidebar.help')}}
|
||||
</el-menu-item>
|
||||
<a href="https://github.com/xfgryujk/blivechat" target="_blank">
|
||||
<el-menu-item>
|
||||
<i class="el-icon-share"></i>{{$t('sidebar.projectAddress')}}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
</router-link>
|
||||
</div>
|
||||
<div class="version">
|
||||
v1.2.3
|
||||
v1.3.0
|
||||
</div>
|
||||
<sidebar></sidebar>
|
||||
</el-aside>
|
||||
|
||||
@@ -9,6 +9,7 @@ import App from './App.vue'
|
||||
import Layout from './layout'
|
||||
import Home from './views/Home.vue'
|
||||
import StyleGenerator from './views/StyleGenerator'
|
||||
import Help from './views/Help'
|
||||
import Room from './views/Room.vue'
|
||||
import NotFound from './views/NotFound.vue'
|
||||
|
||||
@@ -37,7 +38,8 @@ const router = new VueRouter({
|
||||
component: Layout,
|
||||
children: [
|
||||
{path: '', component: Home},
|
||||
{path: 'stylegen', name: 'stylegen', component: StyleGenerator}
|
||||
{path: 'stylegen', name: 'stylegen', component: StyleGenerator},
|
||||
{path: 'help', name: 'help', component: Help}
|
||||
]
|
||||
},
|
||||
{path: '/room/:roomId', name: 'room', component: Room},
|
||||
|
||||
@@ -6,6 +6,21 @@ export function mergeConfig (config, defaultConfig) {
|
||||
return res
|
||||
}
|
||||
|
||||
export function toBool (val) {
|
||||
if (typeof val === 'string') {
|
||||
return val !== 'false' && val !== ''
|
||||
}
|
||||
return !!val
|
||||
}
|
||||
|
||||
export function toInt (val, _default) {
|
||||
let res = parseInt(val)
|
||||
if (isNaN(res)) {
|
||||
res = _default
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
export function formatCurrency (price) {
|
||||
return new Intl.NumberFormat('zh-CN', {
|
||||
minimumFractionDigits: price < 100 ? 2 : 0
|
||||
@@ -17,9 +32,3 @@ export function getTimeTextMinSec (date) {
|
||||
let sec = ('00' + date.getSeconds()).slice(-2)
|
||||
return `${min}:${sec}`
|
||||
}
|
||||
|
||||
export default {
|
||||
mergeConfig,
|
||||
formatCurrency,
|
||||
getTimeTextMinSec
|
||||
}
|
||||
|
||||
21
frontend/src/views/Help.vue
Normal file
21
frontend/src/views/Help.vue
Normal file
@@ -0,0 +1,21 @@
|
||||
<template>
|
||||
<div>
|
||||
<h1>{{$t('help.help')}}</h1>
|
||||
<p>{{$t('help.p1')}}</p>
|
||||
<p><el-image src="/static/img/tutorial/tutorial-1.png"></el-image></p>
|
||||
<p>{{$t('help.p2')}}</p>
|
||||
<p><el-image src="/static/img/tutorial/tutorial-2.png"></el-image></p>
|
||||
<p>{{$t('help.p3')}}</p>
|
||||
<p><el-image src="/static/img/tutorial/tutorial-3.png"></el-image></p>
|
||||
<p>{{$t('help.p4')}}</p>
|
||||
<p><el-image src="/static/img/tutorial/tutorial-4.png"></el-image></p>
|
||||
<p>{{$t('help.p5')}}</p>
|
||||
<p><el-image src="/static/img/tutorial/tutorial-5.png"></el-image></p>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'Help'
|
||||
}
|
||||
</script>
|
||||
@@ -50,21 +50,14 @@
|
||||
<el-slider v-model="form.blockMedalLevel" show-input :min="0" :max="20"></el-slider>
|
||||
</el-form-item>
|
||||
</el-tab-pane>
|
||||
|
||||
<el-tab-pane :label="$t('home.style')">
|
||||
<el-form-item label="CSS">
|
||||
<el-input v-model="form.css" type="textarea" :rows="20"></el-input>
|
||||
</el-form-item>
|
||||
</el-tab-pane>
|
||||
</el-tabs>
|
||||
|
||||
|
||||
<el-divider></el-divider>
|
||||
<el-form-item :label="$t('home.roomUrl')" v-show="roomUrl">
|
||||
<el-input ref="roomUrlInput" readonly :value="roomUrl" style="width: calc(100% - 6em); margin-right: 1em;"></el-input>
|
||||
<el-form-item :label="$t('home.roomUrl')">
|
||||
<el-input ref="roomUrlInput" readonly :value="roomUrl" style="width: calc(100% - 8em); margin-right: 1em;"></el-input>
|
||||
<el-button type="primary" @click="copyUrl">{{$t('home.copy')}}</el-button>
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button type="primary" @click="saveConfig">{{$t('home.saveConfig')}}</el-button>
|
||||
<el-button type="primary" :disabled="!roomUrl" @click="enterRoom">{{$t('home.enterRoom')}}</el-button>
|
||||
<el-button type="primary" @click="exportConfig">{{$t('home.exportConfig')}}</el-button>
|
||||
<el-button type="primary" @click="importConfig">{{$t('home.importConfig')}}</el-button>
|
||||
@@ -73,10 +66,11 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import _ from 'lodash'
|
||||
import download from 'downloadjs'
|
||||
|
||||
import {mergeConfig} from '@/utils'
|
||||
import config from '@/api/config'
|
||||
import * as config from '@/api/config'
|
||||
|
||||
export default {
|
||||
name: 'Home',
|
||||
@@ -85,40 +79,27 @@ export default {
|
||||
form: {
|
||||
roomId: parseInt(window.localStorage.roomId || '1'),
|
||||
...config.getLocalConfig()
|
||||
},
|
||||
roomUrl: ''
|
||||
}
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
roomUrl() {
|
||||
if (this.form.roomId === '') {
|
||||
return ''
|
||||
}
|
||||
let query = {...this.form}
|
||||
delete query.roomId
|
||||
let resolved = this.$router.resolve({name: 'room', params: {roomId: this.form.roomId}, query})
|
||||
return `${window.location.protocol}//${window.location.host}${resolved.href}`
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
roomUrl: _.debounce(function() {
|
||||
window.localStorage.roomId = this.form.roomId
|
||||
config.setLocalConfig(this.form)
|
||||
}, 500)
|
||||
},
|
||||
methods: {
|
||||
saveConfig() {
|
||||
this.$refs.form.validate(async valid => {
|
||||
if (!valid) {
|
||||
return
|
||||
}
|
||||
window.localStorage.roomId = this.form.roomId
|
||||
config.setLocalConfig(this.form)
|
||||
|
||||
try {
|
||||
if (window.localStorage.configId) {
|
||||
try {
|
||||
await config.setRemoteConfig(window.localStorage.configId, this.form)
|
||||
} catch (e) { // 404
|
||||
window.localStorage.configId = (await config.createRemoteConfig(this.form)).id
|
||||
}
|
||||
} else {
|
||||
window.localStorage.configId = (await config.createRemoteConfig(this.form)).id
|
||||
}
|
||||
} catch (e) {
|
||||
this.$message.error(this.$t('home.failedToSave') + e)
|
||||
return
|
||||
}
|
||||
this.$message({message: this.$t('home.successfullySaved'), type: 'success'})
|
||||
|
||||
let resolved = this.$router.resolve({name: 'room', params: {roomId: this.form.roomId},
|
||||
query: {config_id: window.localStorage.configId}})
|
||||
this.roomUrl = `http://${window.location.host}${resolved.href}`
|
||||
})
|
||||
},
|
||||
enterRoom() {
|
||||
window.open(this.roomUrl, `room ${this.form.roomId}`, 'menubar=0,location=0,scrollbars=0,toolbar=0,width=600,height=600')
|
||||
},
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
<template>
|
||||
<chat-renderer ref="renderer" :css="config.css" :maxNumber="config.maxNumber"></chat-renderer>
|
||||
<chat-renderer ref="renderer" :maxNumber="config.maxNumber"></chat-renderer>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import config from '@/api/config'
|
||||
import {mergeConfig, toBool, toInt} from '@/utils'
|
||||
import * as config from '@/api/config'
|
||||
import ChatRenderer from '@/components/ChatRenderer'
|
||||
import * as constants from '@/components/ChatRenderer/constants'
|
||||
|
||||
@@ -42,21 +43,35 @@ export default {
|
||||
},
|
||||
created() {
|
||||
this.wsConnect()
|
||||
if (this.$route.query.config_id) {
|
||||
this.updateConfig(this.$route.query.config_id)
|
||||
}
|
||||
this.updateConfig()
|
||||
},
|
||||
beforeDestroy() {
|
||||
this.isDestroying = true
|
||||
this.websocket.close()
|
||||
},
|
||||
methods: {
|
||||
async updateConfig(configId) {
|
||||
try {
|
||||
this.config = await config.getRemoteConfig(configId)
|
||||
} catch (e) {
|
||||
this.$message.error('获取配置失败:' + e)
|
||||
updateConfig() {
|
||||
let cfg = {}
|
||||
// 留空的使用默认值
|
||||
for (let i in this.$route.query) {
|
||||
if (this.$route.query[i] !== '') {
|
||||
cfg[i] = this.$route.query[i]
|
||||
}
|
||||
}
|
||||
cfg = mergeConfig(cfg, config.DEFAULT_CONFIG)
|
||||
|
||||
cfg.minGiftPrice = toInt(cfg.minGiftPrice, config.DEFAULT_CONFIG.minGiftPrice)
|
||||
cfg.mergeSimilarDanmaku = toBool(cfg.mergeSimilarDanmaku)
|
||||
cfg.showDanmaku = toBool(cfg.showDanmaku)
|
||||
cfg.showGift = toBool(cfg.showGift)
|
||||
cfg.maxNumber = toInt(cfg.maxNumber, config.DEFAULT_CONFIG.maxNumber)
|
||||
cfg.blockGiftDanmaku = toBool(cfg.blockGiftDanmaku)
|
||||
cfg.blockLevel = toInt(cfg.blockLevel, config.DEFAULT_CONFIG.blockLevel)
|
||||
cfg.blockNewbie = toBool(cfg.blockNewbie)
|
||||
cfg.blockNotMobileVerified = toBool(cfg.blockNotMobileVerified)
|
||||
cfg.blockMedalLevel = toInt(cfg.blockMedalLevel, config.DEFAULT_CONFIG.blockMedalLevel)
|
||||
|
||||
this.config = cfg
|
||||
},
|
||||
wsConnect() {
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
|
||||
@@ -100,7 +115,7 @@ export default {
|
||||
let time = data.timestamp ? new Date(data.timestamp * 1000) : new Date()
|
||||
switch (cmd) {
|
||||
case COMMAND_ADD_TEXT:
|
||||
if (!this.config.showDanmaku || !this.filterTextMessage(data) || this.mergeSimilar(data.content)) {
|
||||
if (!this.config.showDanmaku || !this.filterTextMessage(data) || this.mergeSimilarText(data.content)) {
|
||||
break
|
||||
}
|
||||
message = {
|
||||
@@ -123,6 +138,9 @@ export default {
|
||||
if (price < this.config.minGiftPrice) { // 丢人
|
||||
break
|
||||
}
|
||||
if (this.mergeSimilarGift(data.authorName, price)) {
|
||||
break
|
||||
}
|
||||
message = {
|
||||
id: `gift_${this.nextId++}`,
|
||||
type: constants.MESSAGE_TYPE_SUPER_CHAT,
|
||||
@@ -135,7 +153,7 @@ export default {
|
||||
break
|
||||
}
|
||||
case COMMAND_ADD_MEMBER:
|
||||
if (!this.config.showGift || !this.filterSuperChatMessage(data)) {
|
||||
if (!this.config.showGift || !this.filterNewMemberMessage(data)) {
|
||||
break
|
||||
}
|
||||
message = {
|
||||
@@ -149,7 +167,7 @@ export default {
|
||||
}
|
||||
break
|
||||
case COMMAND_ADD_SUPER_CHAT:
|
||||
if (!this.config.showGift) {
|
||||
if (!this.config.showGift || !this.filterSuperChatMessage(data)) {
|
||||
break
|
||||
}
|
||||
if (data.price < this.config.minGiftPrice) { // 丢人
|
||||
@@ -162,7 +180,7 @@ export default {
|
||||
authorName: data.authorName,
|
||||
price: data.price,
|
||||
time: time,
|
||||
content: data.content
|
||||
content: data.content.trim()
|
||||
}
|
||||
break
|
||||
case COMMAND_DEL_SUPER_CHAT:
|
||||
@@ -196,6 +214,9 @@ export default {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return this.filterNewMemberMessage(data)
|
||||
},
|
||||
filterNewMemberMessage(data) {
|
||||
for (let user of this.blockUsers) {
|
||||
if (data.authorName === user) {
|
||||
return false
|
||||
@@ -203,11 +224,17 @@ export default {
|
||||
}
|
||||
return true
|
||||
},
|
||||
mergeSimilar(content) {
|
||||
mergeSimilarText(content) {
|
||||
if (!this.config.mergeSimilarDanmaku) {
|
||||
return false
|
||||
}
|
||||
return this.$refs.renderer.mergeSimilar(content)
|
||||
return this.$refs.renderer.mergeSimilarText(content)
|
||||
},
|
||||
mergeSimilarGift(authorName, price) {
|
||||
if (!this.config.mergeSimilarDanmaku) {
|
||||
return false
|
||||
}
|
||||
return this.$refs.renderer.mergeSimilarGift(authorName, price)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -200,8 +200,8 @@
|
||||
<script>
|
||||
import _ from 'lodash'
|
||||
|
||||
import stylegen from './stylegen'
|
||||
import fonts from './fonts'
|
||||
import * as stylegen from './stylegen'
|
||||
import * as fonts from './fonts'
|
||||
import ChatRenderer from '@/components/ChatRenderer'
|
||||
import * as constants from '@/components/ChatRenderer/constants'
|
||||
|
||||
@@ -266,8 +266,8 @@ const EXAMPLE_MESSAGES = [
|
||||
{
|
||||
...legacyPaidMessageTemplate,
|
||||
id: nextId++,
|
||||
authorName: '吾乐KANA',
|
||||
content: 'Welcome 吾乐KANA!'
|
||||
authorName: '少年Pi',
|
||||
content: 'Welcome 少年Pi!'
|
||||
},
|
||||
{
|
||||
...paidMessageTemplate,
|
||||
@@ -281,14 +281,14 @@ const EXAMPLE_MESSAGES = [
|
||||
id: nextId++,
|
||||
authorName: 'streamer主播',
|
||||
authorType: constants.AUTHRO_TYPE_OWNER,
|
||||
content: '感谢石油佬送的小电视'
|
||||
content: '老板大气,老板身体健康'
|
||||
},
|
||||
{
|
||||
...paidMessageTemplate,
|
||||
id: nextId++,
|
||||
authorName: '夏色祭保護協会会長',
|
||||
price: 28,
|
||||
content: 'Sent 礼花x1'
|
||||
price: 30,
|
||||
content: '言いたいことがあるんだよ!'
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {mergeConfig} from '@/utils'
|
||||
import fonts from './fonts'
|
||||
import * as fonts from './fonts'
|
||||
|
||||
export const DEFAULT_CONFIG = {
|
||||
showOutlines: true,
|
||||
@@ -404,10 +404,3 @@ yt-live-chat-legacy-paid-message-renderer {
|
||||
animation-fill-mode: both;
|
||||
}`
|
||||
}
|
||||
|
||||
export default {
|
||||
DEFAULT_CONFIG,
|
||||
setLocalConfig,
|
||||
getLocalConfig,
|
||||
getStyle
|
||||
}
|
||||
|
||||
60
main.py
60
main.py
@@ -1,7 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import webbrowser
|
||||
@@ -9,55 +8,72 @@ import webbrowser
|
||||
import tornado.ioloop
|
||||
import tornado.web
|
||||
|
||||
import api.chat
|
||||
import api.main
|
||||
import config
|
||||
import models.avatar
|
||||
import models.database
|
||||
import update
|
||||
import views.chat
|
||||
import views.config
|
||||
import views.main
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
WEB_ROOT = os.path.join(os.path.dirname(__file__), 'frontend', 'dist')
|
||||
|
||||
routes = [
|
||||
(r'/chat', api.chat.ChatHandler),
|
||||
|
||||
(r'/((css|fonts|img|js|static)/.*)', tornado.web.StaticFileHandler, {'path': WEB_ROOT}),
|
||||
(r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': WEB_ROOT}),
|
||||
(r'/.*', api.main.MainHandler, {'path': WEB_ROOT})
|
||||
]
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
|
||||
init_logging(args.debug)
|
||||
config.init()
|
||||
models.database.init(args.debug)
|
||||
models.avatar.init()
|
||||
api.chat.init()
|
||||
update.check_update()
|
||||
|
||||
run_server(args.host, args.port, args.debug)
|
||||
|
||||
|
||||
def parse_args():
|
||||
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='服务器端口,默认为12450', type=int, default=12450)
|
||||
parser.add_argument('--debug', help='调试模式', action='store_true')
|
||||
args = parser.parse_args()
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def init_logging(debug):
|
||||
logging.basicConfig(
|
||||
format='{asctime} {levelname} [{name}]: {message}',
|
||||
datefmt='%Y-%m-%d %H:%M:%S',
|
||||
style='{',
|
||||
level=logging.INFO if not args.debug else logging.DEBUG
|
||||
level=logging.INFO if not debug else logging.DEBUG
|
||||
)
|
||||
|
||||
asyncio.ensure_future(update.check_update())
|
||||
|
||||
def run_server(host, port, debug):
|
||||
app = tornado.web.Application(
|
||||
[
|
||||
(r'/chat', views.chat.ChatHandler),
|
||||
(r'/config', views.config.ConfigsHandler),
|
||||
(r'/config/(.+)', views.config.ConfigHandler),
|
||||
|
||||
(r'/((css|fonts|img|js|static)/.*)', tornado.web.StaticFileHandler, {'path': WEB_ROOT}),
|
||||
(r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': WEB_ROOT}),
|
||||
(r'/.*', views.main.MainHandler, {'path': WEB_ROOT})
|
||||
],
|
||||
websocket_ping_interval=30,
|
||||
debug=args.debug,
|
||||
routes,
|
||||
websocket_ping_interval=10,
|
||||
debug=debug,
|
||||
autoreload=False
|
||||
)
|
||||
try:
|
||||
app.listen(args.port, args.host)
|
||||
app.listen(port, host)
|
||||
except OSError:
|
||||
logger.warning('Address is used %s:%d', args.host, args.port)
|
||||
logger.warning('Address is used %s:%d', host, port)
|
||||
return
|
||||
finally:
|
||||
url = 'http://localhost' if args.port == 80 else f'http://localhost:{args.port}'
|
||||
url = 'http://localhost' if port == 80 else f'http://localhost:{port}'
|
||||
webbrowser.open(url)
|
||||
logger.info('Server started: %s:%d', args.host, args.port)
|
||||
logger.info('Server started: %s:%d', host, port)
|
||||
tornado.ioloop.IOLoop.current().start()
|
||||
|
||||
|
||||
|
||||
189
models/avatar.py
Normal file
189
models/avatar.py
Normal file
@@ -0,0 +1,189 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import asyncio
|
||||
import datetime
|
||||
import logging
|
||||
from typing import *
|
||||
|
||||
import aiohttp
|
||||
import sqlalchemy
|
||||
import sqlalchemy.exc
|
||||
|
||||
import models.database
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
DEFAULT_AVATAR_URL = '//static.hdslb.com/images/member/noface.gif'
|
||||
|
||||
_main_event_loop = asyncio.get_event_loop()
|
||||
_http_session = aiohttp.ClientSession()
|
||||
# user_id -> avatar_url
|
||||
_avatar_url_cache: Dict[int, str] = {}
|
||||
# (user_id, future)
|
||||
_fetch_task_queue = asyncio.Queue(15)
|
||||
_last_fetch_failed_time: Optional[datetime.datetime] = None
|
||||
|
||||
|
||||
def init():
|
||||
asyncio.ensure_future(_get_avatar_url_from_web_consumer())
|
||||
|
||||
|
||||
async def get_avatar_url(user_id):
|
||||
avatar_url = get_avatar_url_from_memory(user_id)
|
||||
if avatar_url is not None:
|
||||
return avatar_url
|
||||
avatar_url = await get_avatar_url_from_database(user_id)
|
||||
if avatar_url is not None:
|
||||
return avatar_url
|
||||
return await get_avatar_url_from_web(user_id)
|
||||
|
||||
|
||||
def get_avatar_url_from_memory(user_id):
|
||||
return _avatar_url_cache.get(user_id, None)
|
||||
|
||||
|
||||
def get_avatar_url_from_database(user_id) -> Awaitable[Optional[str]]:
|
||||
return asyncio.get_event_loop().run_in_executor(
|
||||
None, _do_get_avatar_url_from_database, user_id
|
||||
)
|
||||
|
||||
|
||||
def _do_get_avatar_url_from_database(user_id):
|
||||
try:
|
||||
with models.database.get_session() as session:
|
||||
user = session.query(BilibiliUser).filter(BilibiliUser.uid == user_id).one_or_none()
|
||||
if user is None:
|
||||
return None
|
||||
avatar_url = user.avatar_url
|
||||
|
||||
# 如果离上次更新太久就更新所有缓存
|
||||
if (datetime.datetime.now() - user.update_time).days >= 3:
|
||||
def refresh_cache():
|
||||
try:
|
||||
del _avatar_url_cache[user_id]
|
||||
except KeyError:
|
||||
pass
|
||||
get_avatar_url_from_web(user_id)
|
||||
|
||||
_main_event_loop.call_soon(refresh_cache)
|
||||
else:
|
||||
# 否则只更新内存缓存
|
||||
_update_avatar_cache_in_memory(user_id, avatar_url)
|
||||
except sqlalchemy.exc.OperationalError:
|
||||
# SQLite会锁整个文件,忽略就行
|
||||
return None
|
||||
except sqlalchemy.exc.SQLAlchemyError:
|
||||
logger.exception('_do_get_avatar_url_from_database failed:')
|
||||
return None
|
||||
return avatar_url
|
||||
|
||||
|
||||
def get_avatar_url_from_web(user_id) -> Awaitable[str]:
|
||||
future = _main_event_loop.create_future()
|
||||
try:
|
||||
_fetch_task_queue.put_nowait((user_id, future))
|
||||
except asyncio.QueueFull:
|
||||
future.set_result(DEFAULT_AVATAR_URL)
|
||||
return future
|
||||
|
||||
|
||||
async def _get_avatar_url_from_web_consumer():
|
||||
while True:
|
||||
try:
|
||||
user_id, future = await _fetch_task_queue.get()
|
||||
|
||||
# 先查缓存,防止队列中出现相同uid时重复获取
|
||||
avatar_url = get_avatar_url_from_memory(user_id)
|
||||
if avatar_url is not None:
|
||||
continue
|
||||
|
||||
# 防止在被ban的时候获取
|
||||
global _last_fetch_failed_time
|
||||
if _last_fetch_failed_time is not None:
|
||||
cur_time = datetime.datetime.now()
|
||||
if (cur_time - _last_fetch_failed_time).total_seconds() < 3 * 60 + 3:
|
||||
# 3分钟以内被ban则先返回默认头像,解封大约要15分钟
|
||||
future.set_result(DEFAULT_AVATAR_URL)
|
||||
continue
|
||||
else:
|
||||
_last_fetch_failed_time = None
|
||||
|
||||
asyncio.ensure_future(_get_avatar_url_from_web_coroutine(user_id, future))
|
||||
|
||||
# 限制频率,防止被B站ban
|
||||
await asyncio.sleep(0.2)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
async def _get_avatar_url_from_web_coroutine(user_id, future):
|
||||
try:
|
||||
avatar_url = await _do_get_avatar_url_from_web(user_id)
|
||||
except BaseException as e:
|
||||
future.set_exception(e)
|
||||
return
|
||||
future.set_result(avatar_url)
|
||||
|
||||
|
||||
async def _do_get_avatar_url_from_web(user_id):
|
||||
try:
|
||||
async with _http_session.get('https://api.bilibili.com/x/space/acc/info',
|
||||
params={'mid': user_id}) as r:
|
||||
if r.status != 200:
|
||||
logger.warning('Failed to fetch avatar: status=%d %s uid=%d', r.status, r.reason, user_id)
|
||||
if r.status == 412:
|
||||
# 被B站ban了
|
||||
global _last_fetch_failed_time
|
||||
_last_fetch_failed_time = datetime.datetime.now()
|
||||
return DEFAULT_AVATAR_URL
|
||||
data = await r.json()
|
||||
except (aiohttp.ClientConnectionError, asyncio.TimeoutError):
|
||||
return DEFAULT_AVATAR_URL
|
||||
|
||||
avatar_url = data['data']['face'].replace('http:', '').replace('https:', '')
|
||||
if not avatar_url.endswith('noface.gif'):
|
||||
avatar_url += '@48w_48h'
|
||||
|
||||
update_avatar_cache(user_id, avatar_url)
|
||||
return avatar_url
|
||||
|
||||
|
||||
def update_avatar_cache(user_id, avatar_url):
|
||||
_update_avatar_cache_in_memory(user_id, avatar_url)
|
||||
asyncio.get_event_loop().run_in_executor(
|
||||
None, _update_avatar_cache_in_database, user_id, avatar_url
|
||||
)
|
||||
|
||||
|
||||
def _update_avatar_cache_in_memory(user_id, avatar_url):
|
||||
_avatar_url_cache[user_id] = avatar_url
|
||||
if len(_avatar_url_cache) > 50000:
|
||||
for _, key in zip(range(100), _avatar_url_cache):
|
||||
del _avatar_url_cache[key]
|
||||
|
||||
|
||||
def _update_avatar_cache_in_database(user_id, avatar_url):
|
||||
try:
|
||||
with models.database.get_session() as session:
|
||||
user = session.query(BilibiliUser).filter(BilibiliUser.uid == user_id).one_or_none()
|
||||
if user is None:
|
||||
user = BilibiliUser(uid=user_id, avatar_url=avatar_url,
|
||||
update_time=datetime.datetime.now())
|
||||
session.add(user)
|
||||
else:
|
||||
user.avatar_url = avatar_url
|
||||
user.update_time = datetime.datetime.now()
|
||||
session.commit()
|
||||
except (sqlalchemy.exc.OperationalError, sqlalchemy.exc.IntegrityError):
|
||||
# SQLite会锁整个文件,忽略就行,另外还有多线程导致ID重复的问题
|
||||
pass
|
||||
except sqlalchemy.exc.SQLAlchemyError:
|
||||
logger.exception('_update_avatar_cache_in_database failed:')
|
||||
|
||||
|
||||
class BilibiliUser(models.database.OrmBase):
|
||||
__tablename__ = 'bilibili_users'
|
||||
uid = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
|
||||
avatar_url = sqlalchemy.Column(sqlalchemy.Text)
|
||||
update_time = sqlalchemy.Column(sqlalchemy.DateTime)
|
||||
34
models/database.py
Normal file
34
models/database.py
Normal file
@@ -0,0 +1,34 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import contextlib
|
||||
from typing import *
|
||||
|
||||
import sqlalchemy.ext.declarative
|
||||
import sqlalchemy.orm
|
||||
|
||||
import config
|
||||
|
||||
OrmBase = sqlalchemy.ext.declarative.declarative_base()
|
||||
engine = None
|
||||
DbSession: Optional[Type[sqlalchemy.orm.Session]] = None
|
||||
|
||||
|
||||
def init(debug):
|
||||
cfg = config.get_config()
|
||||
global engine, DbSession
|
||||
engine = sqlalchemy.create_engine(cfg.database_url, echo=debug)
|
||||
DbSession = sqlalchemy.orm.sessionmaker(bind=engine)
|
||||
|
||||
OrmBase.metadata.create_all(engine)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def get_session():
|
||||
session = DbSession()
|
||||
try:
|
||||
yield session
|
||||
except:
|
||||
session.rollback()
|
||||
raise
|
||||
finally:
|
||||
session.close()
|
||||
@@ -1,2 +1,3 @@
|
||||
aiohttp==3.5.4
|
||||
sqlalchemy==1.3.13
|
||||
tornado==6.0.2
|
||||
|
||||
27
update.py
27
update.py
@@ -1,15 +1,24 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import asyncio
|
||||
|
||||
import aiohttp
|
||||
|
||||
VERSION = 'v1.2.3'
|
||||
VERSION = 'v1.3.0'
|
||||
|
||||
|
||||
async def check_update():
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get('https://api.github.com/repos/xfgryujk/blivechat/releases/latest') as r:
|
||||
data = await r.json()
|
||||
if data['name'] != VERSION:
|
||||
print('New version available:', data['name'])
|
||||
print(data['body'])
|
||||
print('Download:', data['html_url'])
|
||||
def check_update():
|
||||
asyncio.ensure_future(_do_check_update())
|
||||
|
||||
|
||||
async def _do_check_update():
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get('https://api.github.com/repos/xfgryujk/blivechat/releases/latest') as r:
|
||||
data = await r.json()
|
||||
if data['name'] != VERSION:
|
||||
print('New version available:', data['name'])
|
||||
print(data['body'])
|
||||
print('Download:', data['html_url'])
|
||||
except aiohttp.ClientConnectionError:
|
||||
print('Failed to check update: connection failed')
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import json
|
||||
import uuid
|
||||
|
||||
import views.base
|
||||
from typing import *
|
||||
|
||||
MAX_CONFIG_SIZE = 100 * 1024
|
||||
|
||||
configs: Dict[str, dict] = {}
|
||||
|
||||
|
||||
# noinspection PyAbstractClass
|
||||
class ConfigsHandler(views.base.ApiHandler):
|
||||
async def post(self):
|
||||
if not isinstance(self.json_args, dict):
|
||||
self.set_status(400)
|
||||
return
|
||||
|
||||
config = self.json_args
|
||||
config_id = str(uuid.uuid4())
|
||||
config['id'] = config_id
|
||||
config_str = json.dumps(config)
|
||||
if len(config_str) > MAX_CONFIG_SIZE:
|
||||
self.set_status(413)
|
||||
return
|
||||
|
||||
configs[config_id] = config
|
||||
self.write(config_str)
|
||||
self.set_status(201)
|
||||
self.set_header('Content-Type', 'application/json; charset=UTF-8')
|
||||
|
||||
if len(configs) > 10000:
|
||||
for _, key in zip(range(100), configs):
|
||||
del configs[key]
|
||||
|
||||
|
||||
# noinspection PyAbstractClass
|
||||
class ConfigHandler(views.base.ApiHandler):
|
||||
async def put(self, config_id):
|
||||
if config_id not in configs:
|
||||
self.set_status(404)
|
||||
return
|
||||
if not isinstance(self.json_args, dict):
|
||||
self.set_status(400)
|
||||
return
|
||||
|
||||
config = self.json_args
|
||||
config['id'] = config_id
|
||||
config_str = json.dumps(config)
|
||||
if len(config_str) > MAX_CONFIG_SIZE:
|
||||
self.set_status(413)
|
||||
return
|
||||
|
||||
configs[config_id] = config
|
||||
self.write(config_str)
|
||||
self.set_header('Content-Type', 'application/json; charset=UTF-8')
|
||||
|
||||
async def get(self, config_id):
|
||||
config = configs.get(config_id, None)
|
||||
if config is None:
|
||||
self.set_status(404)
|
||||
return
|
||||
self.write(config)
|
||||
Reference in New Issue
Block a user