Skip to content

Commit

Permalink
feat: 支持配置获取对话记录间隔时间 #169
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Sep 18, 2024
1 parent 8d7b533 commit 7f5692d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class Config:
continue_play: bool = (
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):
for key in keys.split(","):
Expand Down
3 changes: 3 additions & 0 deletions xiaomusic/static/default/setting.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ <h2>小爱音箱设置面板
<label for="public_port">外网访问端口(0表示跟监听端口一致):</label>
<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>
<input id="delay_sec" type="number" value="3"></input>

Expand Down
31 changes: 22 additions & 9 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ def setup_logger(self):
async def poll_latest_ask(self):
async with ClientSession() as session:
while True:
# self.log.debug(
# f"Listening new message, timestamp: {self.last_timestamp}"
# )
self.log.debug(
f"Listening new message, timestamp: {self.last_timestamp}"
)
session._cookie_jar = self.cookie_jar

# 拉取所有音箱的对话记录
Expand All @@ -171,13 +171,17 @@ async def poll_latest_ask(self):
await asyncio.gather(*tasks)

start = time.perf_counter()
# self.log.debug(f"Polling_event, timestamp: {self.last_timestamp}")
await self.polling_event.wait()
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 self.analytics.send_daily_event()
if self.config.pull_ask_sec <= 1:
if (d := time.perf_counter() - start) < 1:
await asyncio.sleep(1 - d)
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):
await self.login_miboy(session)
Expand Down Expand Up @@ -520,8 +524,17 @@ def _append_music_list(self):
except Exception as 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):
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:
self.session = session
await self.init_all_data(session)
Expand Down

0 comments on commit 7f5692d

Please sign in to comment.