-
Notifications
You must be signed in to change notification settings - Fork 7
/
check-update.js
334 lines (303 loc) · 10.2 KB
/
check-update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
'use strict';
var msg = mp.msg;
var commands = require('../script-modules/commands');
var HttpClient = require('../script-modules/HttpClient');
var io = require('../script-modules/io');
var p = require('../script-modules/path');
var u = require('../script-modules/utils');
var options = {
check_config_interval: 7,
check_mpv_update: false,
check_mpv_repo: 'shinchiro/mpv-winbuild-cmake',
check_mpv_interval: 1,
mpv_local_version_regex: '-g([a-z0-9-]{7})',
mpv_remote_name_regex: 'mpv-x86_64-([\\w]+-git-[a-z0-9]{7})',
mpv_remote_version_regex: '-git-([a-z0-9-]{7})',
http_proxy: '',
};
var checking_state = {};
/** @type {null|HttpClient} */
var http = null;
var tools = {
git: u.which('git'),
};
function init_http() {
return new HttpClient({
timeout: mp.get_property_native('network-timeout'),
proxy: options.http_proxy || mp.get_property_native('http-proxy'),
});
}
/**
* @param {string} name
* @returns {string}
*/
function cache_path(name) {
return u.string_format(p.get_state_path() + '/check-update_%s.json', name);
}
/**
* @param {number} interval
* @returns {number}
*/
function parse_interval(interval) {
return interval * 86400 * 1000;
}
/**
* @param {string} name
* @returns {object}
*/
function read_cache(name) {
var path = cache_path(name);
var result = {};
if (io.file_exist(path)) {
try {
result = JSON.parse(io.read_file(path));
} catch (ex) {
}
}
return result;
}
/**
* @param {string} name
* @param {object} data
* @returns {boolean}
*/
function write_cache(name, data) {
var path = cache_path(name);
try {
io.write_file(path, JSON.stringify(data));
} catch (ex) {
msg.verbose(ex.message);
msg.warn(u.string_format('缓存文件写入失败 (%s)', name));
return false;
}
return true;
}
/**
* @returns {number|null}
*/
function get_config_local_version() {
var local_version_file = commands.expand_path('~~/.commit_time');
var result = null;
if (io.file_exist(local_version_file)) {
result = parseInt(io.read_file(local_version_file));
} else if (tools.git) {
var process = commands.subprocess([tools.git, '-C', commands.expand_path('~~/'), 'log', '-1', '--format=%ct']);
if (process.status === 0) {
result = parseInt(process.stdout) * 1000;
}
}
return isNaN(result) ? null : result;
}
/**
* @returns {string|null}
*/
function get_mpv_local_version() {
var mpv_version = mp.get_property_native('mpv-version').trim();
var matches = mpv_version.match(new RegExp(options.mpv_local_version_regex));
msg.verbose('mpv_local_version_matches: %s: %s ', mpv_version, JSON.stringify(matches));
return matches === null ? null : matches[1];
}
/**
* @param {Function} cb
*/
function get_config_remote_version(cb) {
http.get('https://api.github.com/repos/Hill-98/mpv-config/commits/main', {
headers: {
'Accept': 'application/vnd.github+json',
},
}, function (err, response) {
if (err || response.status_code !== 200) {
msg.verbose(err || response.status_text);
cb('未获取到最新版本');
return;
}
if (typeof response.data !== 'object') {
cb('获取到的数据格式无效');
return;
}
cb(null, Date.parse(response.data.commit.committer.date));
});
}
/**
* @param {string} remote_repo
* @param {Function} cb
*/
function get_mpv_remote_version(remote_repo, cb) {
http.get(u.string_format('https://api.github.com/repos/%s/releases/latest', remote_repo), {
headers: {
'Accept': 'application/vnd.github+json',
},
}, function (err, response) {
if (err || response.status_code !== 200) {
msg.verbose(err || response.status_text);
cb('未获取到最新版本');
return;
}
if (typeof response.data !== 'object') {
cb('获取到的数据格式无效');
return;
}
var json = response.data;
for (var i = 0; i < json.assets.length; i++) {
/** @type {string} */
var name = json.assets[i].name;
var name_matches = name.match(new RegExp(options.mpv_remote_name_regex));
msg.verbose(u.string_format('name_matches: %s: %s', name, JSON.stringify(name_matches)));
if (name_matches === null) {
continue;
}
var version_matches = name.match(new RegExp(options.mpv_remote_version_regex));
msg.verbose(u.string_format('version_matches: %s: %s', name, JSON.stringify(version_matches)));
if (version_matches !== null) {
cb(null, {
name: name_matches[1],
version: version_matches[1],
});
return;
}
}
cb('未找到指定的远程版本');
});
}
function check_config_update(force) {
var idx = 'config';
if (checking_state[idx] === true) {
mp.osd_message('正在检查配置文件是否有新版本...');
return;
}
checking_state[idx] = true;
var cache = read_cache(idx);
var cache_valid = typeof cache.next_check_update_time === 'number' && typeof cache.remote_commit_time === 'number';
var check_update_interval = parse_interval(options.check_config_interval);
var local_commit_time = get_config_local_version();
if (local_commit_time === null) {
msg.error('检查配置文件更新失败: 未获取到本地版本');
checking_state[idx] = false;
return;
}
var compare_version = function compare_version(a, b) {
if (a >= b) {
return false;
}
var text = '检查到配置文件新版本: ' + new Date(b).toLocaleString();
var osd = mp.create_osd_overlay('ass-events');
osd.data = text;
osd.update();
msg.info(text);
setTimeout(function () {
osd.remove();
}, 3000);
return true;
};
if (force || !cache_valid || cache.next_check_update_time <= Date.now()) {
get_config_remote_version(function (err, remote_commit_time) {
if (err) {
cache.next_check_update_time = Date.now() + 3600000; // 1h
write_cache(idx, cache);
msg.error('检查配置文件更新失败: ' + err);
checking_state[idx] = false;
return;
}
write_cache(idx, {
next_check_update_time: Date.now() + check_update_interval,
remote_commit_time: remote_commit_time,
});
if (!compare_version(local_commit_time, remote_commit_time) && force) {
msg.info('本地配置文件版本已经是最新的了');
}
checking_state[idx] = false;
});
} else {
compare_version(local_commit_time, cache.remote_commit_time);
checking_state[idx] = false;
}
}
function check_mpv_update(force) {
var idx = 'mpv';
if (checking_state[idx] === true) {
mp.osd_message('正在检查 mpv 是否有新版本...');
return;
}
checking_state[idx] = true;
var cache = read_cache(idx);
var cache_valid = typeof cache.next_check_update_time === 'number' && typeof cache.local_version === 'string' && typeof cache.remote_version === 'string';
/** @type {string} */
var check_update_interval = parse_interval(options.check_mpv_interval);
var local_version = get_mpv_local_version();
var remote_repo = options.check_mpv_repo;
if (local_version === null) {
msg.error('检查 mpv 更新失败: 未获取到本地版本');
checking_state[idx] = false;
return;
}
var compare_version = function compare_version(a, b, s) {
if (a === b) {
return false;
}
var text = '检查到 mpv 新版本: ' + (s || b);
var osd = mp.create_osd_overlay('ass-events');
osd.data = text;
osd.update();
msg.info(text);
setTimeout(function () {
osd.remove();
}, 3000);
return true;
};
if (force || !cache_valid || cache.next_check_update_time <= Date.now() || cache.local_version !== local_version || cache.remote_repo !== remote_repo) {
get_mpv_remote_version(remote_repo, function (err, remote) {
if (err) {
cache.next_check_update_time = Date.now() + 3600000; // 1h
write_cache(idx, cache);
msg.error('检查 mpv 更新失败:' + err);
checking_state[idx] = false;
return;
}
write_cache(idx, {
next_check_update_time: Date.now() + check_update_interval,
local_version: local_version,
remote_repo: remote_repo,
remote_version: remote.version,
remote_version_name: remote.name,
});
if (!compare_version(local_version, remote.version, remote.name) && force) {
msg.info('本地 mpv 版本已经是最新的了');
}
checking_state[idx] = false;
});
} else {
compare_version(local_version, cache.remote_version, cache.remote_version_name);
checking_state[idx] = false;
}
}
function check_update() {
check_config_update();
if (options.check_mpv_update) {
check_mpv_update();
}
}
if (HttpClient.available) {
mp.options.read_options(options, 'check_update', function (list) {
if (list.http_proxy) {
http = init_http();
}
check_update();
});
mp.observe_property('http-proxy', 'native', function () {
http = init_http();
});
mp.observe_property('network-timeout', 'native', function () {
http = init_http();
});
mp.register_script_message('check-update/config', function () {
check_config_update(true);
});
mp.register_script_message('check-update/mpv', function () {
check_mpv_update(true);
});
http = init_http();
check_update();
} else {
msg.error('检查更新不可用: 未找到 curl');
exit();
}