Skip to content

Commit

Permalink
refactor: 优化谷歌统计
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Sep 15, 2024
1 parent 30926c6 commit 7ccbd6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
26 changes: 17 additions & 9 deletions xiaomusic/analytics.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from datetime import datetime

from ga4mp import GtagMP
Expand Down Expand Up @@ -26,27 +27,34 @@ def init(self):
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
Expand All @@ -58,14 +66,14 @@ def _send_daily_event(self):
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)
Expand Down
6 changes: 3 additions & 3 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def __init__(self, config: Config):

# 启动统计
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}")
Expand Down Expand Up @@ -178,7 +177,7 @@ async def poll_latest_ask(self):
# 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)
Expand Down Expand Up @@ -522,6 +521,7 @@ def _append_music_list(self):
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)
Expand Down Expand Up @@ -1073,7 +1073,7 @@ async def _playmusic(self, name):
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:
Expand Down

0 comments on commit 7ccbd6c

Please sign in to comment.