Skip to content

Commit

Permalink
feat: 新增源脚本检查更新功能
Browse files Browse the repository at this point in the history
当客户端检查更新时,如果服务器的脚本与客户端的脚本不一致时会向客户端发送更新源脚本事件
  • Loading branch information
lerdb committed Jun 12, 2024
1 parent faeea6c commit 3ea3c75
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
12 changes: 11 additions & 1 deletion common/lx_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from aiohttp.web import Response
import ujson as json
import re
from common.utils import createMD5

logger = log('lx_script')

Expand Down Expand Up @@ -99,7 +100,16 @@ async def generate_script_response(request):
r = '\n'.join(newScriptLines)

r = re.sub(r'const MUSIC_QUALITY = {[^}]+}', f'const MUSIC_QUALITY = JSON.parse(\'{json.dumps(config.read_config("common.download_config.quality"))}\')', r)


# 用于检查更新
md5 = createMD5(r)
r = r.replace(r"const SCRIPT_MD5 = ''", f"const SCRIPT_MD5 = '{md5}'")
if (request.query.get('checkUpdate')):
if (request.query.get('checkUpdate') == md5):
return {'code': 0, 'msg': 'success', 'data': None}, 200
updateUrl = f"{'https' if config.read_config('common.ssl_info.is_https') else 'http'}://{request.host}/script{('?key=' + request.query.get('key')) if request.query.get('key') else ''}"
return {'code': 0, 'msg': 'success', 'data': {'updateMsg': f'源脚本有更新啦,更新地址:\n{updateUrl}', 'updateUrl': updateUrl}}, 200

return Response(text = r, content_type = 'text/javascript',
headers = {
'Content-Disposition': f'''attachment; filename={
Expand Down
29 changes: 27 additions & 2 deletions lx-music-source-example.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*!
* @name 替换为你的音乐源名称
* @description 替换为你的音乐源介绍
* @version v2.0.0
* @author Folltoshe & helloplhm-qwq
* @version v2.0.1
* @author Folltoshe & helloplhm-qwq & lerdb
* @repository https://github.com/lxmusics/lx-music-api-server
*/

Expand Down Expand Up @@ -30,6 +30,9 @@ MUSIC_SOURCE.push('local')
*/
const { EVENT_NAMES, request, on, send, utils, env, version } = globalThis.lx

// MD5值,用来检查更新
const SCRIPT_MD5 = ''

/**
* URL请求
*
Expand Down Expand Up @@ -214,6 +217,26 @@ const handleGetMusicLyric = async (source, musicInfo) => {
}
}

// 检查源脚本是否有更新
const checkUpdate = async () => {
const request = await httpFetch(`${API_URL}/script?key=${API_KEY}&checkUpdate=${SCRIPT_MD5}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'User-Agent': `${env ? `lx-music-${env}/${version}` : `lx-music-request/${version}`}`
},
})
const { body } = request

if (!body || body.code !== 0) console.log('checkUpdate failed')
else {
console.log('checkUpdate success')
if (body.data != null) {
globalThis.lx.send(lx.EVENT_NAMES.updateAlert, { log: body.data.updateMsg, updateUrl: body.data.updateUrl })
}
}
}

// 生成歌曲信息
const musicSources = {}
MUSIC_SOURCE.forEach(item => {
Expand Down Expand Up @@ -257,5 +280,7 @@ on(EVENT_NAMES.request, ({ action, source, info }) => {
}
})

// 检查更新
checkUpdate()
// 向 LX Music 发送初始化成功事件
send(EVENT_NAMES.inited, { status: true, openDevTools: DEV_ENABLE, sources: musicSources })

0 comments on commit 3ea3c75

Please sign in to comment.