Просмотр исходного кода

improve updating core (thanks to @vernesong)

CN_SZTL 6 лет назад
Родитель
Сommit
cdc07be069

+ 1 - 0
README.md

@@ -63,3 +63,4 @@
 ##### [luci-app-unblockmusic](https://github.com/maxlicheng/luci-app-unblockmusic)的开发者:[maxlicheng](https://github.com/maxlicheng)
 ##### IPSet劫持方式指导:[恩山692049#125楼](https://www.right.com.cn/forum/forum.php?mod=viewthread&tid=692049&page=9#pid4104303) [Lean](https://github.com/coolsnowwolf/lede/tree/master/package/lean/luci-app-unblockmusic) [rufengsuixing](https://github.com/rufengsuixing/luci-app-unblockmusic) [binsee](https://github.com/binsee/luci-app-unblockmusic)
 ##### Hosts劫持方式指导:[UnblockNeteaseMusic](https://github.com/nondanee/UnblockNeteaseMusic) [云音乐安卓又搞事啦](https://jixun.moe/post/netease-android-hosts-bypass/)
+##### 核心程序版本检测方法指导:[vernesong](https://github.com/vernesong)

+ 39 - 8
luasrc/controller/unblockneteasemusic.lua

@@ -10,16 +10,47 @@ function index()
 	end
 
 	entry({"admin", "services", "unblockneteasemusic"},firstchild(), _("解除网易云音乐播放限制"), 50).dependent = false
-	
-	entry({"admin", "services", "unblockneteasemusic", "general"},cbi("unblockneteasemusic"), _("Base Setting"), 1)
-	entry({"admin", "services", "unblockneteasemusic", "log"},form("unblockneteasemusiclog"), _("Log"), 2)
-  
+
+	entry({"admin", "services", "unblockneteasemusic", "general"},cbi("unblockneteasemusic"), _("基本设定"), 1)
+	entry({"admin", "services", "unblockneteasemusic", "updt_core"},form("unblockneteasemusic_upcore"), _("更新核心"), 2).leaf = true
+	entry({"admin", "services", "unblockneteasemusic", "log"},form("unblockneteasemusiclog"), _("日志"), 3)
+
 	entry({"admin", "services", "unblockneteasemusic", "status"},call("act_status")).leaf=true
+	entry({"admin", "services", "unblockneteasemusic", "update_core"},call("act_update_core"))
 end
 
 function act_status()
-  local e={}
-  e.running=luci.sys.call("ps |grep unblockneteasemusic |grep app.js |grep -v grep >/dev/null")==0
-  luci.http.prepare_content("application/json")
-  luci.http.write_json(e)
+	local e={}
+	e.running=luci.sys.call("ps |grep unblockneteasemusic |grep app.js |grep -v grep >/dev/null")==0
+	luci.http.prepare_content("application/json")
+	luci.http.write_json(e)
+end
+
+function update_core()
+	cloud_ver=luci.sys.exec("curl -s 'https://github.com/nondanee/UnblockNeteaseMusic/commits/master' |tr -d '\n' |grep -Eo 'commit\/[0-9a-z]+' |sed -n 1p |sed 's#commit/##g'")
+	cloud_ver_mini=luci.sys.exec("curl -s 'https://github.com/nondanee/UnblockNeteaseMusic/commits/master' |tr -d '\n' |grep -Eo 'BtnGroup-item.>      [0-9a-z]+' |sed -n 1p |sed 's#BtnGroup-item.>      ##g'")
+	if not cloud_ver or not cloud_ver_mini then
+		return "1"
+	else
+		local_ver=luci.sys.exec("cat '/usr/share/unblockneteasemusic/local_ver'")
+		if not local_ver or (local_ver ~= cloud_ver) then
+			luci.sys.call("rm -f /usr/share/unblockneteasemusic/update_successfully")
+			luci.sys.call("/bin/bash /usr/share/unblockneteasemusic/update_core.sh luci_update")
+			if not nixio.fs.access("/usr/share/unblockneteasemusic/update_successfully") then
+				return "2"
+			else
+				luci.sys.call("rm -f /usr/share/unblockneteasemusic/update_successfully")
+				return cloud_ver_mini
+			end
+		else
+			return "0"
+		end
+	end
 end
+
+function act_update_core()
+	luci.http.prepare_content("application/json")
+	luci.http.write_json({
+		ret = update_core();
+	})
+end

+ 1 - 10
luasrc/model/cbi/unblockneteasemusic.lua

@@ -1,7 +1,7 @@
 mp = Map("unblockneteasemusic", translate("解除网易云音乐播放限制"))
 mp.description = translate("原理:采用 [QQ/虾米/百度/酷狗/酷我/咕咪/JOOX] 等音源,替换网易云音乐 无版权/收费 歌曲链接<br/>具体使用方法参见:https://github.com/project-openwrt/luci-app-unblockneteasemusic")
 
-mp:section(SimpleSection).template  = "unblockneteasemusic/unblockneteasemusic_status"
+mp:section(SimpleSection).template = "unblockneteasemusic/unblockneteasemusic_status"
 
 s = mp:section(TypedSection, "unblockneteasemusic")
 s.anonymous=true
@@ -12,15 +12,6 @@ enabled.description = translate("启用本插件以解除网易云音乐播放
 enabled.default = 0
 enabled.rmempty = false
 
-button_update_core = s:option (Button, "_button_update_core", translate("更新主程序"))
-button_update_core.description = translate("更新完毕后会自动重启插件;更新主程序需要一点时间,请不要反复点击按钮")
-local latest_ver = luci.sys.exec("/bin/bash /usr/share/unblockneteasemusic/check_update.sh")
-button_update_core.inputtitle = translate (latest_ver)
-button_update_core.inputstyle = "apply"
-function button_update_core.write (self, section, value)
-	luci.sys.call ("/bin/bash /usr/share/unblockneteasemusic/update_core.sh &")
-end
-
 account = s:option(Value, "http_port", translate("[HTTP] 监听端口"))
 account.description = translate("本插件监听的HTTP端口,不可与其他程序/HTTPS共用一个端口")
 account.placeholder = "5200"

+ 13 - 0
luasrc/model/cbi/unblockneteasemusic_upcore.lua

@@ -0,0 +1,13 @@
+local m, up_core
+
+m = SimpleForm("Version")
+m.reset = false
+m.submit = false
+
+up_core=m:field(DummyValue,"update_core",translate("更新主程序")) 
+up_core.rawhtml  = true
+up_core.template = "unblockneteasemusic/update_core"
+up_core.value = translate("未检查") 
+up_core.description = "更新完毕后会自动在后台重启插件,无需手动重启"
+
+return m

+ 36 - 0
luasrc/view/unblockneteasemusic/update_core.htm

@@ -0,0 +1,36 @@
+<%+cbi/valueheader%>
+
+<script type="text/javascript">//<![CDATA[
+	function act_update_core(btn,dataname)
+	{
+		btn.disabled = true;
+		btn.value    = '<%:正在更新主程序...%> ';
+		XHR.get('<%=luci.dispatcher.build_url("admin", "services", "unblockneteasemusic","update_core")%>',
+			status.ret,
+			function(x,status)
+			{
+				var s = document.getElementById(dataname+'-status');
+				if (s)
+				{
+					if(status.ret=="0")
+						s.innerHTML ="<font color='green'>"+"<%:当前已是最新版本%>"+"</font>";
+					else if (status.ret=="1")
+						s.innerHTML ="<font color='red'>"+"<%:无法检测最新版本%>"+"</font>";
+					else if(status.ret=="2")
+						s.innerHTML ="<font color='red'>"+"<%:更新失败,请稍后重试%>"+"</font>";
+					else
+						s.innerHTML ="<font color='green'>"+"<%:更新成功,当前版本号:%>"+status.ret+"</font>";
+				}
+				btn.disabled = false;
+				btn.value    = '<%:点此更新主程序%>';
+			}
+		);
+		return false;
+	}
+//]]></script>
+
+
+<input type="button" class="cbi-button cbi-input-reload" value="<%:点此更新主程序%> " onclick="return act_update_core(this,'<%=self.option%>')" />
+<span id="<%=self.option%>-status"><em><%=self.value%></em></span>
+
+<%+cbi/valuefooter%>

+ 0 - 21
root/usr/share/unblockneteasemusic/check_update.sh

@@ -1,21 +0,0 @@
-#!/bin/bash
-# Created By [CTCGFW]Project OpenWRT
-# https://github.com/project-openwrt
-
-commit_info="$(curl -s https://github.com/nondanee/UnblockNeteaseMusic/commits/master)"
-latest_ver="$(echo -e "${commit_info}" |tr -d '\n' | grep -Eo 'commit\/[0-9a-z]+' |sed -n 1p |sed 's#commit/##g')"
-latest_ver_mini="$(echo -e "${commit_info}" |tr -d '\n' | grep -Eo 'BtnGroup-item">      [0-9a-z]+' |sed -n 1p |sed 's#BtnGroup-item">      ##g')"
-
-[ -z "${commit_info}" ] && echo "无法检测最新版本,请稍后重试" && exit 1
-[ -z "${latest_ver}" ] && echo "无法检测最新版本,请稍后重试" && exit 1
-[ -z "${latest_ver_mini}" ] && echo "无法检测最新版本,请稍后重试" && exit 1
-
-if [ ! -e "/usr/share/unblockneteasemusic/local_ver" ]; then
-	echo "最新版本:${latest_ver_mini},点此更新"
-else
-	if [ "$(cat /usr/share/unblockneteasemusic/local_ver)" != "${latest_ver}" ]; then
-		echo "最新版本:${latest_ver_mini},点此更新"
-	else
-		echo "目前已是最新版本,无需更新"
-	fi
-fi

+ 3 - 1
root/usr/share/unblockneteasemusic/update_core.sh

@@ -50,6 +50,7 @@ function update_core(){
 		echo -e "Failed to download core." >>/tmp/unblockneteasemusic.log
 		exit 1
 	else
+		[ "${luci_update}" == "y" ] && touch "/usr/share/unblockneteasemusic/update_successfully"
 		echo -e "${latest_ver}" > /usr/share/unblockneteasemusic/local_ver
 		/etc/init.d/unblockneteasemusic restart
 	fi
@@ -64,4 +65,5 @@ function main(){
 	check_latest_version
 }
 
-main
+	[ "$1" == "luci_update" ] && luci_update="y"
+	main