From 6e8830c4e670a7a9ab068201e40b00f9da11cec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Tue, 24 Sep 2024 09:26:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=8F=90=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaomusic/httpserver.py | 10 ++++++++++ xiaomusic/static/default/app.js | 25 +++++++++++++++++++++++++ xiaomusic/static/default/index.html | 5 ++--- xiaomusic/static/default/style.css | 10 ++++++++++ xiaomusic/utils.py | 14 +++++++++++++- xiaomusic/xiaomusic.py | 6 +++--- 6 files changed, 63 insertions(+), 7 deletions(-) diff --git a/xiaomusic/httpserver.py b/xiaomusic/httpserver.py index fec01d1df..fd94d8870 100644 --- a/xiaomusic/httpserver.py +++ b/xiaomusic/httpserver.py @@ -25,6 +25,7 @@ from xiaomusic.utils import ( deepcopy_data_no_sensitive_info, downloadfile, + get_latest_version, ) xiaomusic = None @@ -358,6 +359,15 @@ async def debug_play_by_music_url(request: Request, Verifcation=Depends(verifica raise HTTPException(status_code=400, detail="Invalid JSON") from err +@app.get("/latestversion") +async def latest_version(Verifcation=Depends(verification)): + version = await get_latest_version("xiaomusic") + if version: + return {"ret": "OK", "version": version} + else: + return {"ret": "Fetch version failed"} + + async def file_iterator(file_path, start, end): async with aiofiles.open(file_path, mode="rb") as file: await file.seek(start) diff --git a/xiaomusic/static/default/app.js b/xiaomusic/static/default/app.js index 51847b8e0..aa3f47273 100644 --- a/xiaomusic/static/default/app.js +++ b/xiaomusic/static/default/app.js @@ -86,10 +86,35 @@ $(function(){ }) }); + function compareVersion(version1, version2) { + const v1 = version1.split('.').map(Number); + const v2 = version2.split('.').map(Number); + const len = Math.max(v1.length, v2.length); + + for (let i = 0; i < len; i++) { + const num1 = v1[i] || 0; + const num2 = v2[i] || 0; + if (num1 > num2) return 1; + if (num1 < num2) return -1; + } + return 0; + } + // 拉取版本 $.get("/getversion", function(data, status) { console.log(data, status, data["version"]); $("#version").text(`${data.version}`); + + $.get("/latestversion", function(ret, status) { + console.log(ret, status); + if (ret.ret == "OK") { + const result = compareVersion(ret.version, data.version); + if (result > 0) { + console.log(`${ret.version} is greater than ${data.version}`); + $("#versionnew").text("🆕"); + } + } + }); }); // 拉取播放列表 diff --git a/xiaomusic/static/default/index.html b/xiaomusic/static/default/index.html index e98b13fe5..306762d4f 100644 --- a/xiaomusic/static/default/index.html +++ b/xiaomusic/static/default/index.html @@ -26,9 +26,8 @@

小爱音箱操控面板 - ( - 版本未知 - ) + (版本未知) +


diff --git a/xiaomusic/static/default/style.css b/xiaomusic/static/default/style.css index dba9238c6..e9def464a 100644 --- a/xiaomusic/static/default/style.css +++ b/xiaomusic/static/default/style.css @@ -90,3 +90,13 @@ footer { max-width: 480px; height: auto; } + +@keyframes blink { + 0%, 100% { opacity: 1; } + 50% { opacity: 0; } +} + +.blink { + animation: blink 1s infinite; +} + diff --git a/xiaomusic/utils.py b/xiaomusic/utils.py index da7d8ae1c..075715789 100644 --- a/xiaomusic/utils.py +++ b/xiaomusic/utils.py @@ -137,7 +137,8 @@ def find_best_match(user_input, collection, cutoff=0.6, n=1, extra_search_index= # 如果数量不满足,继续搜索 lower_extra_search_index = { - traditional_to_simple(k.lower()): v for k, v in extra_search_index.items() + traditional_to_simple(k.lower()): v + for k, v in extra_search_index.items() if v not in cur_matched_collection } matches = real_search(user_input, lower_extra_search_index.keys(), cutoff, n) @@ -639,3 +640,14 @@ def list2str(li, verbose=False): return f"{li[:2]} ... {li[-2:]} with len: {len(li)}" else: return f"{li}" + + +async def get_latest_version(package_name: str) -> str: + url = f"https://pypi.org/pypi/{package_name}/json" + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + if response.status == 200: + data = await response.json() + return data["info"]["version"] + else: + return None diff --git a/xiaomusic/xiaomusic.py b/xiaomusic/xiaomusic.py index e9cc1d2c6..d98312d02 100644 --- a/xiaomusic/xiaomusic.py +++ b/xiaomusic/xiaomusic.py @@ -483,7 +483,7 @@ def try_save_tag_cache(self): def ensure_single_thread_for_tag(self): if self._tag_generation_task: - self.log.info(f"tag 更新中,请等待") + self.log.info("tag 更新中,请等待") return not self._tag_generation_task def try_gen_all_music_tag(self, only_items: dict = None): @@ -499,7 +499,7 @@ async def _gen_all_music_tag(self, only_items: dict = None): self._tag_generation_task = True if only_items is None: only_items = self.all_music # 默认更新全部 - + all_music_tags = self.try_load_from_tag_cache() all_music_tags.update(self.all_music_tags) # 保证最新 for name, file_or_url in only_items.items(): @@ -520,7 +520,7 @@ async def _gen_all_music_tag(self, only_items: dict = None): # 刷新 tag cache self.try_save_tag_cache() self._tag_generation_task = False - self.log.info(f"tag 更新完成") + self.log.info("tag 更新完成") # 获取目录下所有歌曲,生成随机播放列表 def _gen_all_music_list(self):