feat: 支持配置获取对话记录间隔时间 #169

This commit is contained in:
涵曦 2024-09-19 00:45:08 +08:00
parent 8d7b5337eb
commit 7f5692d6cd
3 changed files with 26 additions and 9 deletions

View File

@ -148,6 +148,7 @@ class Config:
continue_play: bool = ( continue_play: bool = (
os.getenv("XIAOMUSIC_CONTINUE_PLAY", "false").lower() == "true" os.getenv("XIAOMUSIC_CONTINUE_PLAY", "false").lower() == "true"
) )
pull_ask_sec: int = int(os.getenv("XIAOMUSIC_PULL_ASK_SEC", "1"))
def append_keyword(self, keys, action): def append_keyword(self, keys, action):
for key in keys.split(","): for key in keys.split(","):

View File

@ -154,6 +154,9 @@ var vConsole = new window.VConsole();
<label for="public_port">外网访问端口(0表示跟监听端口一致):</label> <label for="public_port">外网访问端口(0表示跟监听端口一致):</label>
<input id="public_port" type="number" value="0"></input> <input id="public_port" type="number" value="0"></input>
<label for="pull_ask_sec">获取对话记录间隔(秒):</label>
<input id="pull_ask_sec" type="number" value="1"></input>
<label for="delay_sec">下一首歌延迟播放秒数:</label> <label for="delay_sec">下一首歌延迟播放秒数:</label>
<input id="delay_sec" type="number" value="3"></input> <input id="delay_sec" type="number" value="3"></input>

View File

@ -158,9 +158,9 @@ class XiaoMusic:
async def poll_latest_ask(self): async def poll_latest_ask(self):
async with ClientSession() as session: async with ClientSession() as session:
while True: while True:
# self.log.debug( self.log.debug(
# f"Listening new message, timestamp: {self.last_timestamp}" f"Listening new message, timestamp: {self.last_timestamp}"
# ) )
session._cookie_jar = self.cookie_jar session._cookie_jar = self.cookie_jar
# 拉取所有音箱的对话记录 # 拉取所有音箱的对话记录
@ -171,13 +171,17 @@ class XiaoMusic:
await asyncio.gather(*tasks) await asyncio.gather(*tasks)
start = time.perf_counter() start = time.perf_counter()
# self.log.debug(f"Polling_event, timestamp: {self.last_timestamp}")
await self.polling_event.wait() await self.polling_event.wait()
if self.config.pull_ask_sec <= 1:
if (d := time.perf_counter() - start) < 1: if (d := time.perf_counter() - start) < 1:
# sleep to avoid too many request
# self.log.debug(f"Sleep {d}, timestamp: {self.last_timestamp}")
await asyncio.sleep(1 - d) await asyncio.sleep(1 - d)
await self.analytics.send_daily_event() else:
sleep_sec = 0
while True:
await asyncio.sleep(1)
sleep_sec = sleep_sec + 1
if sleep_sec >= self.config.pull_ask_sec:
break
async def init_all_data(self, session): async def init_all_data(self, session):
await self.login_miboy(session) await self.login_miboy(session)
@ -520,8 +524,17 @@ class XiaoMusic:
except Exception as e: except Exception as e:
self.log.exception(f"Execption {e}") self.log.exception(f"Execption {e}")
async def analytics_task_daily(self):
while True:
await self.analytics.send_daily_event()
await asyncio.sleep(3600)
async def run_forever(self): async def run_forever(self):
await self.analytics.send_startup_event() await self.analytics.send_startup_event()
analytics_task = asyncio.create_task(self.analytics_task_daily())
assert (
analytics_task is not None
) # to keep the reference to task, do not remove this
async with ClientSession() as session: async with ClientSession() as session:
self.session = session self.session = session
await self.init_all_data(session) await self.init_all_data(session)