From 7ccbd6ce79e66dd1e82b084f7efc90eefc0735c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Sun, 15 Sep 2024 23:10:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E8=B0=B7?= =?UTF-8?q?=E6=AD=8C=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaomusic/analytics.py | 26 +++++++++++++++++--------- xiaomusic/xiaomusic.py | 6 +++--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/xiaomusic/analytics.py b/xiaomusic/analytics.py index 50244e9..728c984 100644 --- a/xiaomusic/analytics.py +++ b/xiaomusic/analytics.py @@ -1,3 +1,4 @@ +import asyncio from datetime import datetime from ga4mp import GtagMP @@ -26,27 +27,34 @@ class Analytics: self.gtag = gtag self.log.info("analytics init ok") - def send_startup_event(self): + async def run_with_timeout(self, func, *args, **kwargs): try: - self._send_startup_event() + return await asyncio.wait_for(func(*args, **kwargs), 3) + except asyncio.TimeoutError as e: + self.log.warning(f"analytics run_with_timeout failed {e}") + return None + + async def send_startup_event(self): + try: + await self.run_with_timeout(self._send_startup_event) except Exception as e: self.log.warning(f"analytics send_startup_event failed {e}") self.init() - def _send_startup_event(self): + async def _send_startup_event(self): event = self.gtag.create_new_event(name="startup") event.set_event_param(name="version", value=__version__) event_list = [event] self.gtag.send(events=event_list) - def send_daily_event(self): + async def send_daily_event(self): try: - self._send_daily_event() + await self.run_with_timeout(self._send_daily_event) except Exception as e: self.log.warning(f"analytics send_daily_event failed {e}") self.init() - def _send_daily_event(self): + async def _send_daily_event(self): current_date = datetime.now().strftime("%Y-%m-%d") if self.current_date == current_date: return @@ -58,14 +66,14 @@ class Analytics: self.gtag.send(events=event_list) self.current_date = current_date - def send_play_event(self, name, sec): + async def send_play_event(self, name, sec): try: - self._send_play_event(name, sec) + await self.run_with_timeout(self._send_play_event, name, sec) except Exception as e: self.log.warning(f"analytics send_play_event failed {e}") self.init() - def _send_play_event(self, name, sec): + async def _send_play_event(self, name, sec): event = self.gtag.create_new_event(name="play") event.set_event_param(name="version", value=__version__) event.set_event_param(name="music", value=name) diff --git a/xiaomusic/xiaomusic.py b/xiaomusic/xiaomusic.py index eee0f0a..b1321c7 100644 --- a/xiaomusic/xiaomusic.py +++ b/xiaomusic/xiaomusic.py @@ -87,7 +87,6 @@ class XiaoMusic: # 启动统计 self.analytics = Analytics(self.log) - self.analytics.send_startup_event() debug_config = deepcopy_data_no_sensitive_info(self.config) self.log.info(f"Startup OK. {debug_config}") @@ -178,7 +177,7 @@ class XiaoMusic: # sleep to avoid too many request # self.log.debug(f"Sleep {d}, timestamp: {self.last_timestamp}") await asyncio.sleep(1 - d) - self.analytics.send_daily_event() + await self.analytics.send_daily_event() async def init_all_data(self, session): await self.login_miboy(session) @@ -522,6 +521,7 @@ class XiaoMusic: self.log.exception(f"Execption {e}") async def run_forever(self): + await self.analytics.send_startup_event() async with ClientSession() as session: self.session = session await self.init_all_data(session) @@ -1073,7 +1073,7 @@ class XiaoMusicDevice: return self.log.info(f"【{name}】已经开始播放了") - self.xiaomusic.analytics.send_play_event(name, sec) + await self.xiaomusic.analytics.send_play_event(name, sec) # 设置下一首歌曲的播放定时器 if sec <= 1: