-- Copyright 2025 David Vazgenovich Shakaryan local cacher = require('cacher') local config = require('config') local input = require('input') local rt = require('rt') local util = require('util') local _catalogue = require('catalogue') local _downloader = require('downloader') local _epg = require('epg') local _osd = require('osd') local _state = require('state') local _xc = require('xc') local mp_utils = require('mp.utils') local script_name = mp.get_script_name() local state = _state.new() local click_state = {} local downloader = _downloader.new({limit = 5}) local xc = _xc.new({ server = mp.get_opt('iptv_menu.xc_server'), user = mp.get_opt('iptv_menu.xc_user'), pass = mp.get_opt('iptv_menu.xc_pass'), }) xc = cacher.wrap(xc, { directory = config.cache_dir, 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 catalogue = _catalogue.new() local epg = _epg.new() local osd local function dl_img(url, path, cb) downloader:schedule(url, path, function(success, _, path) if success then cb(path) else osd:flash_error('Image download failed') end end) end osd = _osd.new({ img_path_func = function(url, cb) local path = mp_utils.join_path( config.img_dir, url:gsub('%W', '_')) if not mp_utils.file_info(path) and cb then dl_img(url, path, cb) end return path end, img_fail_cb = function() osd:flash_error('Image load failed') end }) local ctx = { catalogue = catalogue, epg = epg, xc = xc, } rt.init(state, osd, input, ctx) mp.observe_property('mouse-pos', 'native', function(_, mpos) input.update_mpos(mpos) end) mp.observe_property('user-data/osc/visibility', 'native', function(_, val) if val and (osd:is_hidden() or val ~= 'never') then state.saved_osc_visibility = val end end) mp.observe_property('osd-dimensions', 'native', function(_, val) osd:resize(val.w, val.h) osd:redraw(state) input.on_resize() end) mp.register_event('start-file', function() state.playing_id = mp.get_opt('iptv_menu.playing_id') osd:redraw(state) end) mp.register_event('end-file', function() state.playing_id = nil osd:redraw(state) end) mp.register_event('shutdown', function() osd:destroy() end) state:push_menu({title = 'mpv-iptv-menu'}) state.saved_osc_visibility = mp.get_property_native( 'user-data/osc/visibility', 'auto') rt.set_osc_visibility() mp.add_forced_key_binding('TAB', 'toggle-menu', rt.toggle_menu) input.init(state, osd) input.set_key_mapping('MENU') input.set_key_bindings() mp.add_timeout(0, function() rt.load_data() state.depth = 0 rt.push_group_menu(catalogue:get('root')) osd:redraw(state) end)