mirror of
https://github.com/xfgryujk/blivechat.git
synced 2024-12-26 21:00:15 +08:00
添加获取头像间隔、缓存等配置
This commit is contained in:
parent
986f008fae
commit
c6f5411efc
20
config.py
20
config.py
@ -47,10 +47,16 @@ def get_config():
|
|||||||
class AppConfig:
|
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.allow_translate_rooms = {}
|
|
||||||
self.tornado_xheaders = False
|
self.tornado_xheaders = False
|
||||||
self.loader_url = ''
|
self.loader_url = ''
|
||||||
|
|
||||||
|
self.fetch_avatar_interval = 3.5
|
||||||
|
self.fetch_avatar_max_queue_size = 2
|
||||||
|
self.avatar_cache_size = 50000
|
||||||
|
|
||||||
|
self.enable_translate = True
|
||||||
|
self.allow_translate_rooms = set()
|
||||||
|
self.translation_cache_size = 50000
|
||||||
self.translator_configs = []
|
self.translator_configs = []
|
||||||
|
|
||||||
def load(self, path):
|
def load(self, path):
|
||||||
@ -68,11 +74,17 @@ class AppConfig:
|
|||||||
def _load_app_config(self, config):
|
def _load_app_config(self, config):
|
||||||
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.allow_translate_rooms = _str_to_list(app_section['allow_translate_rooms'], int, set)
|
|
||||||
self.tornado_xheaders = app_section.getboolean('tornado_xheaders')
|
self.tornado_xheaders = app_section.getboolean('tornado_xheaders')
|
||||||
self.loader_url = app_section['loader_url']
|
self.loader_url = app_section['loader_url']
|
||||||
|
|
||||||
|
self.fetch_avatar_interval = app_section.getfloat('fetch_avatar_interval')
|
||||||
|
self.fetch_avatar_max_queue_size = app_section.getint('fetch_avatar_max_queue_size')
|
||||||
|
self.avatar_cache_size = app_section.getint('avatar_cache_size')
|
||||||
|
|
||||||
|
self.enable_translate = app_section.getboolean('enable_translate')
|
||||||
|
self.allow_translate_rooms = _str_to_list(app_section['allow_translate_rooms'], int, set)
|
||||||
|
self.translation_cache_size = app_section.getint('translation_cache_size')
|
||||||
|
|
||||||
def _load_translator_configs(self, config):
|
def _load_translator_configs(self, config):
|
||||||
app_section = config['app']
|
app_section = config['app']
|
||||||
section_names = _str_to_list(app_section['translator_configs'])
|
section_names = _str_to_list(app_section['translator_configs'])
|
||||||
|
@ -6,6 +6,29 @@
|
|||||||
# See https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls
|
# See https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls
|
||||||
database_url = sqlite:///data/database.db
|
database_url = sqlite:///data/database.db
|
||||||
|
|
||||||
|
# 如果使用了nginx之类的反向代理服务器,设置为true
|
||||||
|
# Set to true if you are using a reverse proxy server such as nginx
|
||||||
|
tornado_xheaders = false
|
||||||
|
|
||||||
|
# 加载器URL,本地使用时加载器可以让你先运行OBS再运行blivechat。如果为空,不使用加载器
|
||||||
|
# **自建服务器时强烈建议不使用加载器**,否则可能因为混合HTTP和HTTPS等原因加载不出来
|
||||||
|
# Use a loader so that you can run OBS before blivechat. If empty, no loader is used
|
||||||
|
loader_url = https://xfgryujk.sinacloud.net/blivechat/loader.html
|
||||||
|
|
||||||
|
|
||||||
|
# 获取头像间隔时间(秒)。如果小于3秒有很大概率被服务器拉黑
|
||||||
|
# Interval between fetching avatar (s). At least 3 seconds is recommended
|
||||||
|
fetch_avatar_interval = 3.5
|
||||||
|
|
||||||
|
# 获取头像最大队列长度,注意最长等待时间等于 最大队列长度 * 请求间隔时间
|
||||||
|
# Maximum queue length for fetching avatar
|
||||||
|
fetch_avatar_max_queue_size = 2
|
||||||
|
|
||||||
|
# 头像缓存数量
|
||||||
|
# Number of avatar caches
|
||||||
|
avatar_cache_size = 50000
|
||||||
|
|
||||||
|
|
||||||
# 允许自动翻译到日语
|
# 允许自动翻译到日语
|
||||||
# Enable auto translate to Japanese
|
# Enable auto translate to Japanese
|
||||||
enable_translate = true
|
enable_translate = true
|
||||||
@ -15,14 +38,9 @@ enable_translate = true
|
|||||||
# Example: allow_translate_rooms = 4895312,22347054,21693691
|
# Example: allow_translate_rooms = 4895312,22347054,21693691
|
||||||
allow_translate_rooms =
|
allow_translate_rooms =
|
||||||
|
|
||||||
# 如果使用了nginx之类的反向代理服务器,设置为true
|
# 翻译缓存数量
|
||||||
# Set to true if you are using a reverse proxy server such as nginx
|
# Number of translation caches
|
||||||
tornado_xheaders = false
|
translation_cache_size = 50000
|
||||||
|
|
||||||
# 加载器URL,本地使用时加载器可以让你先运行OBS再运行blivechat。如果为空,不使用加载器
|
|
||||||
# **自建服务器时强烈建议不使用加载器**,否则可能因为混合HTTP和HTTPS等原因加载不出来
|
|
||||||
# Use a loader so that you can run OBS before blivechat. If empty, no loader is used
|
|
||||||
loader_url = https://xfgryujk.sinacloud.net/blivechat/loader.html
|
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
@ -10,6 +10,7 @@ import aiohttp
|
|||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
import sqlalchemy.exc
|
import sqlalchemy.exc
|
||||||
|
|
||||||
|
import config
|
||||||
import models.database
|
import models.database
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -24,12 +25,15 @@ _avatar_url_cache: Dict[int, str] = {}
|
|||||||
# 正在获取头像的Future,user_id -> Future
|
# 正在获取头像的Future,user_id -> Future
|
||||||
_uid_fetch_future_map: Dict[int, asyncio.Future] = {}
|
_uid_fetch_future_map: Dict[int, asyncio.Future] = {}
|
||||||
# 正在获取头像的user_id队列
|
# 正在获取头像的user_id队列
|
||||||
_uid_queue_to_fetch = asyncio.Queue(15)
|
_uid_queue_to_fetch = None
|
||||||
# 上次被B站ban时间
|
# 上次被B站ban时间
|
||||||
_last_fetch_banned_time: Optional[datetime.datetime] = None
|
_last_fetch_banned_time: Optional[datetime.datetime] = None
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
|
cfg = config.get_config()
|
||||||
|
global _uid_queue_to_fetch
|
||||||
|
_uid_queue_to_fetch = asyncio.Queue(cfg.fetch_avatar_max_queue_size)
|
||||||
asyncio.ensure_future(_get_avatar_url_from_web_consumer())
|
asyncio.ensure_future(_get_avatar_url_from_web_consumer())
|
||||||
|
|
||||||
|
|
||||||
@ -124,7 +128,8 @@ async def _get_avatar_url_from_web_consumer():
|
|||||||
asyncio.ensure_future(_get_avatar_url_from_web_coroutine(user_id, future))
|
asyncio.ensure_future(_get_avatar_url_from_web_coroutine(user_id, future))
|
||||||
|
|
||||||
# 限制频率,防止被B站ban
|
# 限制频率,防止被B站ban
|
||||||
await asyncio.sleep(0.2)
|
cfg = config.get_config()
|
||||||
|
await asyncio.sleep(cfg.fetch_avatar_interval)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('_get_avatar_url_from_web_consumer error:')
|
logger.exception('_get_avatar_url_from_web_consumer error:')
|
||||||
|
|
||||||
@ -178,7 +183,8 @@ def update_avatar_cache(user_id, avatar_url):
|
|||||||
|
|
||||||
def _update_avatar_cache_in_memory(user_id, avatar_url):
|
def _update_avatar_cache_in_memory(user_id, avatar_url):
|
||||||
_avatar_url_cache[user_id] = avatar_url
|
_avatar_url_cache[user_id] = avatar_url
|
||||||
while len(_avatar_url_cache) > 50000:
|
cfg = config.get_config()
|
||||||
|
while len(_avatar_url_cache) > cfg.avatar_cache_size:
|
||||||
_avatar_url_cache.pop(next(iter(_avatar_url_cache)), None)
|
_avatar_url_cache.pop(next(iter(_avatar_url_cache)), None)
|
||||||
|
|
||||||
|
|
||||||
|
@ -124,7 +124,8 @@ def _on_translate_done(key, future):
|
|||||||
if res is None:
|
if res is None:
|
||||||
return
|
return
|
||||||
_translate_cache[key] = res
|
_translate_cache[key] = res
|
||||||
while len(_translate_cache) > 50000:
|
cfg = config.get_config()
|
||||||
|
while len(_translate_cache) > cfg.translation_cache_size:
|
||||||
_translate_cache.pop(next(iter(_translate_cache)), None)
|
_translate_cache.pop(next(iter(_translate_cache)), None)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user