diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2025-05-05 21:06:40 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2025-05-05 21:06:40 -0700 |
commit | 15fcafef23564e8e55523c2ac5a45b036aaeb0da (patch) | |
tree | 4d14032a274114635f8c6f01a72f4c33f75725e0 | |
parent | 163b6577501a3474d32b4f4c49415a459caef143 (diff) | |
download | mpv-iptv-menu-15fcafef23564e8e55523c2ac5a45b036aaeb0da.tar.gz mpv-iptv-menu-15fcafef23564e8e55523c2ac5a45b036aaeb0da.tar.xz |
add support for nested categories
-rw-r--r-- | main.lua | 88 |
1 files changed, 63 insertions, 25 deletions
@@ -3,6 +3,18 @@ local utils = require('mp.utils') local script_dir = mp.get_script_directory() local stream_prefix = mp.get_opt('iptv_menu.stream_prefix') +local osd = mp.create_osd_overlay('ass-events') +local osd_lines = 23 +local key_bindings = {} + +local categories = {} +local streams = {} + +local depth = 0 +local menus = {} +local menu_top = {} +local menu_pos = {} + local function load_json_file(path) local f = io.open(script_dir .. '/' .. path, 'r') local data = f:read('*all') @@ -10,35 +22,55 @@ local function load_json_file(path) return utils.parse_json(data) end -local osd = mp.create_osd_overlay('ass-events') -local osd_lines = 23 - -local categories = load_json_file('categories.json') -local streams = load_json_file('streams.json') +local function load_data() + categories = load_json_file('categories.json') + for _, v in ipairs(categories) do + v.type = 'category' + end -local depth = 1 -local menus = {categories} -local menu_top = {1} -local menu_pos = {1} + streams = load_json_file('streams.json') + for _, v in ipairs(streams) do + v.type = 'stream' + end +end local function osd_menu_lines() - return osd_lines - depth + 1 + if depth > 1 then + return osd_lines - depth + else + return osd_lines + end end local function update_osd() local out = {} - for i = 1, depth - 1 do - out[#out+1] = '{\\c&H999999&}[' .. - menus[i][menu_pos[i]]['category_name'] .. ']{\\c}' + + if depth > 1 then + for i = 1, depth - 1 do + out[#out+1] = '{\\c&H999999&} ยป [' .. + menus[i][menu_pos[i]].category_name .. ']{\\c}' + end + out[#out+1] = ' ' -- space character for correct line height end + for i = menu_top[depth], math.min( menu_top[depth] + osd_menu_lines() - 1, #menus[depth]) do - local key = (depth == 1 and 'category_name' or 'name') - local str = menus[depth][i][key] + local opt = menus[depth][i] + local str + + if opt.type == 'category' then + str = '[' .. opt.category_name .. ']' + else + str = opt.name + end + if i == menu_pos[depth] then str = '{\\c&HFF00&}* ' .. str .. '{\\c}' + elseif opt.type == 'category' then + str = '{\\c&H99DDFF&}' .. str .. '{\\c}' end + out[#out+1] = str end @@ -91,11 +123,15 @@ local function last_option() advance_cursor(math.huge) end -local function select_category() - local cat = categories[menu_pos[depth]] +local function add_category_menu(category_id) local menu = {} + for _, v in ipairs(categories) do + if tostring(v.parent_id) == category_id then + menu[#menu+1] = v + end + end for _, v in ipairs(streams) do - if v['category_id'] == cat['category_id'] then + if v.category_id == category_id then menu[#menu+1] = v end end @@ -107,16 +143,18 @@ local function select_category() update_osd() end -local function select_stream() - local url = stream_prefix .. menus[depth][menu_pos[depth]]['stream_id'] +local function play_stream(stream_id) + local url = stream_prefix .. stream_id mp.commandv('loadfile', url) end local function select_option() - if depth == 1 then - select_category() + local opt = menus[depth][menu_pos[depth]] + + if opt.type == 'category' then + add_category_menu(opt.category_id) else - select_stream() + play_stream(opt.stream_id) end end @@ -127,8 +165,6 @@ local function prev_menu() end end -local key_bindings = {} - local function bind_key(key, name, func, opts) key_bindings[#key_bindings+1] = name mp.add_forced_key_binding(key, name, func, opts) @@ -166,4 +202,6 @@ end mp.add_forced_key_binding('TAB', 'toggle-menu', toggle_menu) bind_keys() +load_data() +add_category_menu('0') update_osd() |