Skip to content

Commit

Permalink
feat: 新增更新提醒
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Sep 24, 2024
1 parent 609cb4f commit 6e8830c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
10 changes: 10 additions & 0 deletions xiaomusic/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from xiaomusic.utils import (
deepcopy_data_no_sensitive_info,
downloadfile,
get_latest_version,
)

xiaomusic = None
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions xiaomusic/static/default/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("🆕");
}
}
});
});

// 拉取播放列表
Expand Down
5 changes: 2 additions & 3 deletions xiaomusic/static/default/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
</head>
<body>
<h2>小爱音箱操控面板
(<a id="version" href="https://github.com/hanxi/xiaomusic/blob/main/CHANGELOG.md">
版本未知
</a>)
(<a id="version" href="https://github.com/hanxi/xiaomusic/blob/main/CHANGELOG.md">版本未知</a>)
<span id="versionnew" class="blink"></span>
</h2>
<hr>

Expand Down
10 changes: 10 additions & 0 deletions xiaomusic/static/default/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ footer {
max-width: 480px;
height: auto;
}

@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0; }
}

.blink {
animation: blink 1s infinite;
}

14 changes: 13 additions & 1 deletion xiaomusic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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():
Expand All @@ -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):
Expand Down

0 comments on commit 6e8830c

Please sign in to comment.