diff options
| author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2026-01-03 14:08:17 -0800 |
|---|---|---|
| committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2026-01-03 14:08:17 -0800 |
| commit | d58a0a7abe32f668ee80efb9bb159d31863057fa (patch) | |
| tree | ce611046437e451f4ffbe9d0b38d7624adeefafb | |
| parent | 5fad0532ede395c106d90947d1270c90b8075344 (diff) | |
| download | mpv-iptv-menu-d58a0a7abe32f668ee80efb9bb159d31863057fa.tar.gz mpv-iptv-menu-d58a0a7abe32f668ee80efb9bb159d31863057fa.tar.xz | |
add cacher module that's more invisible
| -rw-r--r-- | cacher.lua | 52 | ||||
| -rw-r--r-- | main.lua | 56 |
2 files changed, 76 insertions, 32 deletions
diff --git a/cacher.lua b/cacher.lua new file mode 100644 index 0000000..486f238 --- /dev/null +++ b/cacher.lua @@ -0,0 +1,52 @@ +-- Copyright 2025 David Vazgenovich Shakaryan + +local util = require('util') + +local mp_utils = require('mp.utils') + +local cacher = {} + +local function wrap_func(obj, opts, func, func_name) + return function(_, ...) + local fn = table.concat({opts.prefix, func_name, ...}, '.') + local path = mp_utils.join_path(opts.directory, fn) + local f = mp_utils.file_info(path) + local data + + if not f or os.time() - f.mtime > opts.time then + data = func(obj, ...) + if data then + util.write_json_file(path, data) + end + end + + if f and not data then + print('using cached ' .. fn) + data = util.read_json_file(path) + end + + return data + end +end + +function cacher.wrap(obj, opts) + local proxy = {} + + setmetatable(proxy, { + __index = function(t, k) + local v = obj[k] + if type(v) ~= 'function' or not opts.functions[k] then + return v + end + + local fn = wrap_func(obj, opts, v, k) + t[k] = fn + + return fn + end, + }) + + return proxy +end + +return cacher @@ -1,5 +1,6 @@ -- Copyright 2025 David Vazgenovich Shakaryan +local cacher = require('cacher') local util = require('util') local _catalogue = require('catalogue') local _downloader = require('downloader') @@ -37,6 +38,22 @@ local xc = _xc.new({ user = mp.get_opt('iptv_menu.xc_user'), pass = mp.get_opt('iptv_menu.xc_pass'), }) +xc = cacher.wrap(xc, { + directory = mp_utils.join_path(script_dir, 'cache'), + prefix = (xc.server:gsub('%W', '_')), + time = 24*60*60, + functions = { + get_live_categories = true, + get_live_streams = true, + get_vod_categories = true, + get_vod_streams = true, + get_vod_info = true, + get_series_categories = true, + get_series = true, + get_series_info = true, + get_epg = true, + }, +}) local osd = mp.create_osd_overlay('ass-events') local osd_width = 0 @@ -84,31 +101,6 @@ local function get_image_path(url, dl) end end -local function cached_xc_call(method, ...) - local id = xc.server:gsub('%W', '_') .. '.' .. method - if select('#', ...) ~= 0 then - id = id .. '.' .. table.concat({...}, '.') - end - - local path = mp_utils.join_path(script_dir, 'cache/' .. id) - local f = mp_utils.file_info(path) - local data - - if not f or os.time() - f.mtime > 24*60*60 then - data = xc[method](xc, ...) - if data then - util.write_json_file(path, data) - end - end - - if f and not data then - print('using cached ' .. id) - data = util.read_json_file(path) - end - - return data -end - local function load_data() local arr = { {id = 'live', name = 'Live TV', type = 'live'}, @@ -116,14 +108,14 @@ local function load_data() {id = 'series', name = 'Series', type = 'series'}, } for _, v in ipairs(arr) do - v.categories = cached_xc_call('get_' .. v.type .. '_categories') - v.elements = cached_xc_call( + v.categories = xc['get_' .. v.type .. '_categories'](xc) + v.elements = xc[ v.type == 'series' and 'get_series' or - ('get_' .. v.type .. '_streams')) + ('get_' .. v.type .. '_streams')](xc) end catalogue:load_xc_data(arr) - epg:load_xc_data(cached_xc_call('get_epg')) + epg:load_xc_data(xc:get_epg()) favourites = util.read_json_file( mp_utils.join_path(script_dir, 'favourites.json')) @@ -573,7 +565,7 @@ local function favourites_group_menu_options(group) end local function series_group_menu_options(series) - local info = cached_xc_call('get_series_info', series.series_id) + local info = xc:get_series_info(series.series_id) if not info or not info.seasons then return {} end @@ -922,13 +914,13 @@ end local function open_option_movie_info(opt) open_option_title_info( 'Movie Info: ' .. opt.name, - cached_xc_call('get_vod_info', opt.stream_id)) + xc:get_vod_info(opt.stream_id)) end local function open_option_series_info(opt) open_option_title_info( 'Series Info: ' .. opt.name, - cached_xc_call('get_series_info', opt.series_id)) + xc:get_series_info(opt.series_id)) end local function open_option_info() |
