Content-Length: 10425 | pFad | https://github.com/Hill-98/mpv-config/raw/refs/heads/main/scripts/check-update.js

h: 10413 '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(); }








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/Hill-98/mpv-config/raw/refs/heads/main/scripts/check-update.js

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy