OSDN Git Service

luci-app-unblockneteasemusic: bump to 2.13
[immortalwrt/luci.git] / applications / luci-app-unblockneteasemusic / luasrc / controller / unblockneteasemusic.lua
1 -- SPDX-License-Identifer: GPL-3.0-only
2 -- Copyright (C) 2019-2022 Tianling Shen <cnsztl@immortalwrt.org>
3
4 module("luci.controller.unblockneteasemusic", package.seeall)
5
6 function index()
7         if not nixio.fs.access("/etc/config/unblockneteasemusic") then
8                 return
9         end
10
11         local page
12         page = entry({"admin", "services", "unblockneteasemusic"}, firstchild(), _("解除网易云音乐播放限制"), 50)
13         page.dependent = false
14         page.acl_depends = { "luci-app-unblockneteasemusic" }
15
16         entry({"admin", "services", "unblockneteasemusic", "general"}, cbi("unblockneteasemusic/unblockneteasemusic"), _("基本设定"), 1)
17         entry({"admin", "services", "unblockneteasemusic", "upgrade"}, form("unblockneteasemusic/unblockneteasemusic_upgrade"), _("更新组件"), 2).leaf = true
18         entry({"admin", "services", "unblockneteasemusic", "log"}, form("unblockneteasemusic/unblockneteasemusic_log"), _("日志"), 3)
19
20         entry({"admin", "services", "unblockneteasemusic", "status"}, call("act_status")).leaf = true
21         entry({"admin", "services", "unblockneteasemusic", "update_core"}, call("act_update_core"))
22         entry({"admin", "services", "unblockneteasemusic", "remove_core"}, call("act_remove_core"))
23 end
24
25 function act_status()
26         local stat = luci.util.ubus("service", "list", { name = "unblockneteasemusic" })
27         local running = next(stat) and stat.unblockneteasemusic.instances.unblockneteasemusic.running or false
28
29         local e = { running = running }
30         luci.http.prepare_content("application/json")
31         luci.http.write_json(e)
32 end
33
34 function update_core()
35         local core_cloud_ver = luci.sys.exec("uclient-fetch -qO- 'https://api.github.com/repos/UnblockNeteaseMusic/server/commits?sha=enhanced&path=precompiled' | jsonfilter -e '@[0].sha'")
36         local core_cloud_ver_mini = string.sub(core_cloud_ver, 1, 7)
37         local core_local_ver
38         if not core_cloud_ver or not core_cloud_ver_mini then
39                 return "1"
40         else
41                 core_local_ver = luci.sys.exec("cat '/usr/share/unblockneteasemusic/core_local_ver' 2>'/dev/null'")
42                 if not core_local_ver or (core_local_ver ~= core_cloud_ver) then
43                         luci.sys.call("rm -f /usr/share/unblockneteasemusic/update_core_successfully")
44                         luci.sys.call("/usr/share/unblockneteasemusic/update.sh update_core_from_luci")
45                         if not nixio.fs.access("/usr/share/unblockneteasemusic/update_core_successfully") then
46                                 return "2"
47                         else
48                                 luci.sys.call("rm -f /usr/share/unblockneteasemusic/update_core_successfully")
49                                 return core_cloud_ver_mini
50                         end
51                 else
52                         return "0"
53                 end
54         end
55 end
56
57 function act_update_core()
58         luci.http.prepare_content("application/json")
59         luci.http.write_json({
60                 ret = update_core();
61         })
62 end
63
64 function act_remove_core()
65         local ret = {}
66         ret.ret = luci.sys.call("cd /usr/share/unblockneteasemusic && rm -rf core/* && rm -f core_local_ver") == 0
67         luci.http.prepare_content("application/json")
68         luci.http.write_json(ret)
69 end