summaryrefslogtreecommitdiff
path: root/main.lua
blob: fd6f75b8249e04f36cd4716a4d00e0d201f0f3d9 (plain) (blame)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
-- Copyright 2025 David Vazgenovich Shakaryan

local utils = require('mp.utils')

-- font size is in units of osd height, which is scaled to 720
local font_size = 20
local colours = {
	bg='{\\alpha&H44&\\c&H&}',
	title='{\\c&999999&}',
	option='{\\c}',
	group='{\\c&H99DDFF&}',
	group_empty='{\\c&H446677&}',
	selected='{\\c&HFF00&}',
	selected_empty='{\\c&H337733&}',
	search_hl='{\\c&FFDD&}',
	search_hl_empty='{\\c&8877&}',
	search_path='{\\c&666666&}',
	search_path_empty='{\\c&444444&}',
	icon_playing='{\\c&HFF6633&}',
	icon_favourite='{\\c&HFF00FF&}',
}

local script_dir = mp.get_script_directory()
local xc_server = mp.get_opt('iptv_menu.xc_server')
local xc_user = mp.get_opt('iptv_menu.xc_user')
local xc_pass = mp.get_opt('iptv_menu.xc_pass')

local osd = mp.create_osd_overlay('ass-events')
local osd_lines = math.floor((720 / font_size) + 0.5) - 1
local osd_padding = math.floor((720 - (osd_lines * font_size)) / 2)
local osd_cursor_glyph = '{\\p1\\pbo' .. math.floor(font_size / 5) .. '}' ..
	'm 0 0 l ' .. math.ceil(font_size / 32) .. ' 0 ' ..
	math.ceil(font_size / 32) .. ' ' .. font_size ..
	' 0 ' .. font_size .. '{\\p0}'
local osd_bg = mp.create_osd_overlay('ass-events')
osd_bg.z = -1
osd_bg.data = '{\\pos(0,0)}' .. colours.bg ..
	'{\\p1}m 0 0 l 7680 0 7680 720 0 720{\\p0}'

local objects = {}
local favourites
local playing_id

local depth = 0
local menus = {}
local key_bindings = {}

local function copy_table(t)
	local u = {}
	for k, v in pairs(t) do
		u[k] = v
	end
	return u
end

local function read_json_file(fn)
	local f = io.open(script_dir .. '/' .. fn, 'r')
	if not f then
		return {}
	end
	local json = f:read('*all')
	f:close()
	return utils.parse_json(json)
end

local function write_json_file(fn, data)
	local f = io.open(script_dir .. '/' .. fn, 'w')
	f:write(utils.format_json(data), '\n')
	f:close()
end

local function add_object(obj)
	objects[obj.id] = obj

	if obj.type == 'group' then
		obj.children = {}
	end

	if not obj.parent_id then
		return
	end

	-- dump any objects referencing nonexistent categories into a single
	-- catchall category
	if not objects[obj.parent_id] then
		obj.parent_id = obj.section .. ':category:catchall'
		if not objects[obj.parent_id] then
			add_object({
				section=obj.section,
				type='group',
				group_type='category',
				id=obj.parent_id,
				parent_id=obj.section .. ':category:0',
				name='*CATCHALL*',
			})
		end
	end

	local parent_children = objects[obj.parent_id].children
	parent_children[#parent_children+1] = obj
end

local function load_section(section, name)
	add_object({
		section=section,
		type='group',
		group_type='category',
		id=section .. ':category:0',
		parent_id='root',
		name=name,
	})

	local tmp = read_json_file(section .. '_categories.json')
	for _, v in ipairs(tmp) do
		v.section = section
		v.type = 'group'
		v.group_type = 'category'
		v.id = section .. ':category:' .. v.category_id
		v.parent_id = section .. ':category:' .. v.parent_id
		v.name = v.category_name
		add_object(v)
	end

	tmp = read_json_file(section .. '_streams.json')
	for _, v in ipairs(tmp) do
		v.section = section
		if v.series_id then
			v.type = 'group'
			v.group_type = 'series'
			v.id = section .. ':series:' .. v.series_id
			v.lazy = true
		else
			v.type = 'stream'
			v.id = section .. ':stream:' .. v.stream_id
		end
		v.parent_id = section .. ':category:' .. v.category_id
		add_object(v)
	end
end

local function load_data()
	add_object({
		type='group',
		id='root',
	})
	add_object({
		type='group',
		id='favourites',
		parent_id='root',
		name='Favourites',
		lazy=true,
	})

	load_section('live', 'Live TV')
	load_section('movie', 'Movies')
	load_section('series', 'Series')

	favourites = read_json_file('favourites.json')
	-- json loading/dumping breaks when the table is empty, so we need a
	-- dummy value to prevent that
	if next(favourites) == nil then
		favourites = {oi=true}
	end
end

local function osd_menu_lines()
	if depth > 1 then
		-- leaves an extra line for padding between titles and options
		return osd_lines - depth
	else
		return osd_lines
	end
end

local function update_osd()
	local out = {}

	if depth > 1 then
		for i = 2, depth do
			local col = colours.title
			if menus[i].search_active then
				col = colours.selected
			end
			out[#out+1] = col .. ' » [' .. menus[i].title .. ']'
		end
		out[#out+1] = ' ' -- space character for correct line height
	end

	local menu = menus[depth]
	for i = menu.view_top, math.min(
			menu.view_top + osd_menu_lines() - 1, #menu.options) do
		local opt = menu.options[i]
		local str = opt.name
		local col = colours.option
		local icons = ''

		local is_empty = opt.type == 'group' and not opt.lazy and
			#opt.children == 0

		if i == menu.cursor and not menu.search_active then
			col = colours.selected
			icons = col .. '› ' .. icons

			if is_empty then
				col = colours.selected_empty
			end
		elseif is_empty then
			col = colours.group_empty
		elseif opt.type == 'group' then
			col = colours.group
		end

		if opt.matches then
			local buf = ''
			local n = 0
			local hl_col = colours.search_hl
			if is_empty then
				hl_col =  colours.search_hl_empty
			end

			for _, match in ipairs(opt.matches) do
				buf = buf .. col ..
					str:sub(n + 1, match.start - 1) ..
					hl_col ..
					str:sub(match.start, match.stop)
				n = match.stop
			end
			str = buf .. col .. str:sub(n + 1)
		else
			str = col .. str
		end

		if opt.type == 'group' then
			str = col .. '[' .. str .. ']'
		elseif opt.id == playing_id then
			icons = icons .. colours.icon_playing .. '\226\143\186 '
		end

		if favourites[opt.id] then
			icons = icons .. colours.icon_favourite .. '★ '
		end

		if opt.path and #opt.path > 0 then
			local path = colours.search_path
			if is_empty then
				path = colours.search_path_empty
			end
			for i = #opt.path, 1, -1 do
				local node = opt.path[i]
				path = path .. ' « [' .. node.name .. ']'
			end
			str = str .. path
		end

		out[#out+1] = icons .. str
	end

	-- \q2 disables line wrapping
	osd.data = '{\\q2\\fs' .. font_size .. '\\pos(' .. osd_padding ..
		',' .. osd_padding .. ')}' .. table.concat(out, '\\N')
	osd:update()
	osd_bg:update()
end

local function set_cursor(pos, move_view)
	local menu = menus[depth]
	local lines = osd_menu_lines()

	local pos = math.max(1, math.min(pos, #menu.options))
	local top = menu.view_top
	if move_view then
		top = top + pos - menu.cursor
	end

	-- move view to keep selected option visible
	if pos < top then
		top = pos
	elseif pos > top + lines - 1 then
		top = pos - lines + 1
	end

	top = math.max(1, math.min(top, #menu.options - lines + 1))

	menu.cursor = pos
	menu.view_top = top
	update_osd()
end

local function cursor_up()
	set_cursor(menus[depth].cursor - 1)
end

local function cursor_down()
	set_cursor(menus[depth].cursor + 1)
end

local function cursor_start()
	set_cursor(0)
end

local function cursor_end()
	set_cursor(#menus[depth].options)
end

local function cursor_page_up()
	set_cursor(menus[depth].cursor - osd_menu_lines(), true)
end

local function cursor_page_down()
	set_cursor(menus[depth].cursor + osd_menu_lines(), true)
end

local function prev_menu()
	if depth > 1 then
		depth = depth - 1
		update_osd()
	end
end

local function push_menu(t)
	local menu = {
		options={},
		cursor=1,
		view_top=1,
	}

	for k, v in pairs(t) do
		menu[k] = v
	end

	depth = depth + 1
	menus[depth] = menu
end

local function favourites_group_menu_options(group)
	local options = {}
	for id in pairs(favourites) do
		local obj = objects[id]
		if obj then
			options[#options+1] = obj
		end
	end
	return options
end

local function get_series_info(series_id)
	local cmd = 'curl -sSfL \'' .. xc_server .. '/player_api.php' ..
		'?username=' .. xc_user .. '&password=' .. xc_pass .. '&' ..
		'action=get_series_info&series_id=' .. series_id .. '\''
	print('exec: ' .. cmd)
	local fd = io.popen(cmd)
	local json = fd:read('*all')
	fd:close()
	return utils.parse_json(json)
end

local function series_group_menu_options(series)
	local info = get_series_info(series.series_id)

	if not info or not info.seasons then
		return {}
	end

	local seasons = {}
	for _, season in pairs(info.seasons) do
		local episodes = {}
		local season_num = tostring(season.season_number)
		if info.episodes and info.episodes[season_num] then
			for i, episode in pairs(info.episodes[season_num]) do
				episodes[#episodes+1] = {
					name=episode.title,
					type='stream',
					stream_type='series',
					id=series.section .. ':stream:' ..
						episode.id,
					stream_id=episode.id,
				}
			end
		end

		seasons[#seasons+1] = {
			type='group',
			group_type='season',
			id=series.section .. 'series:season:' .. season.id,
			children=episodes,
			name=season.name,
		}
	end

	return seasons
end

local function group_menu_options(group)
	if group.id == 'favourites' then
		return favourites_group_menu_options(group)
	end

	if group.group_type == 'series' then
		return series_group_menu_options(group)
	end

	return group.children
end

local function push_group_menu(group)
	push_menu({
		options=group_menu_options(group),
		title=group.name,
	})
	update_osd()
end

local function play_stream(stream)
	-- add a per-file option containing the stream id, allowing it to be
	-- retrieved when a start-file event is received
	local url
	if stream.stream_type == 'series' then
		url = xc_server .. '/series/' .. xc_user .. '/' .. xc_pass ..
			'/' .. stream.stream_id .. '.vod'
	else
		url = xc_server .. '/' .. xc_user .. '/' .. xc_pass .. '/' ..
			stream.stream_id
	end

	mp.commandv('loadfile', url, 'replace', -1,
		'script-opt=iptv_menu.playing_id=' .. stream.id)
end

local function select_option()
	local menu = menus[depth]
	local opt = menu.options[menu.cursor]
	if not opt then
		return
	end

	if opt.type == 'group' then
		push_group_menu(opt)
	else
		play_stream(opt)
	end
end

local function favourite_option()
	local menu = menus[depth]
	local opt = menu.options[menu.cursor]
	if not opt then
		return
	end

	local id = opt.id
	if favourites[id] then
		favourites[id] = nil
	else
		favourites[id] = true
	end

	write_json_file('favourites.json', favourites)
	update_osd()
end

local function split_search_text()
	local menu = menus[depth]
	return menu.search_text:sub(1, menu.search_cursor - 1),
		menu.search_text:sub(menu.search_cursor)
end

local function update_search_osd()
	local bc, ac = split_search_text()
	menus[depth].title = 'Searching: ' .. bc .. osd_cursor_glyph .. ac
	update_osd()
end

local function search_menu_options_build(options, t, path)
	local menu = menus[depth]
	local path = path or {}

	for _, v in ipairs(options) do
		if not t[v.type] then
			t[v.type] = {}
		end

		local v = copy_table(v)
		v.path = path
		t[v.type][#t[v.type]+1] = v

		-- contents of lazy-loaded groups should not be searchable
		if v.type == 'group' and not v.lazy then
			local path = copy_table(path)
			path[#path+1] = v
			search_menu_options_build(
				group_menu_options(v), t, path)
		end
	end
end

local function search_menu_options(options)
	local t = {}
	search_menu_options_build(options, t)

	-- display categories before streams
	local ret = t.group or {}
	if t.stream then
		for _, v in ipairs(t.stream) do
			ret[#ret+1] = v
		end
	end
	return ret
end

local function update_search_matches()
	local menu = menus[depth]

	if #menu.search_text == 0 then
		menu.options = menu.search_options
		update_search_osd()
		return
	end

	local options = {}
	for _, v in ipairs(menu.search_options) do
		local matches = {}

		local i, j = 0, 0
		while true do
			i, j = v.name:find(menu.search_text, j + 1, true)
			if not i then
				break
			end
			matches[#matches+1] = {start=i, stop=j}
		end

		if #matches > 0 then
			local t = copy_table(v)
			t.matches = matches
			options[#options+1] = t
		end
	end

	menu.options = options
	update_search_osd()
end

local function search_input_text(event)
	if event.event ~= 'down' and event.event ~= 'repeat' then
		return
	end

	local menu = menus[depth]
	local bc, ac = split_search_text()
	menu.search_text = bc .. event.key_text .. ac
	menu.search_cursor = menu.search_cursor + 1
	update_search_matches()
end

local function search_input_bs()
	local menu = menus[depth]
	if menu.search_cursor == 1 then
		return
	end

	local bc, ac = split_search_text()
	menu.search_text = bc:sub(1, -2) .. ac
	menu.search_cursor = menu.search_cursor - 1
	update_search_matches()
end

local function search_input_del()
	local menu = menus[depth]
	if menu.search_cursor == #menu.search_text + 1 then
		return
	end

	local bc, ac = split_search_text()
	menu.search_text = bc .. ac:sub(2)
	update_search_matches()
end

local function set_search_cursor(pos)
	local menu = menus[depth]
	local pos = math.max(1, math.min(#menu.search_text + 1, pos))
	if pos == menu.search_cursor then
		return
	end

	menu.search_cursor = pos
	update_search_osd()
end

local function search_cursor_left()
	set_search_cursor(menus[depth].search_cursor - 1)
end

local function search_cursor_right()
	set_search_cursor(menus[depth].search_cursor + 1)
end

local function search_cursor_start()
	set_search_cursor(1)
end

local function search_cursor_end()
	set_search_cursor(#menus[depth].search_text + 1)
end

local bind_search_keys
local bind_menu_keys

local function start_search()
	local menu = menus[depth]
	if menu.type == 'search' then
		-- resume search
		menu.search_active = true
		menu.search_cursor = #menu.search_text + 1
		menu.cursor = 1
		menu.view_top = 1
		update_search_osd()
	else
		push_menu({
			type='search',
			search_active=true,
			search_options=search_menu_options(menu.options),
			search_text='',
			search_cursor=1,
		})
		update_search_matches()
	end

	bind_search_keys()
end

local function end_search()
	local menu = menus[depth]
	menu.search_active = false
	menu.title = 'Search results: ' .. menu.search_text
	update_osd()
	bind_menu_keys()
end

local function cancel_search()
	menus[depth].search_active = false
	depth = depth - 1
	update_osd()
	bind_menu_keys()
end

local function bind_key(key, func, opts)
	-- unique name is needed for removal
	local i = #key_bindings+1
	local name = 'key' .. i
	key_bindings[i] = name
	mp.add_forced_key_binding(key, name, func, opts)
end

local function unbind_keys()
	for _, key in ipairs(key_bindings) do
		mp.remove_key_binding(key)
	end
	key_bindings = {}
end

function bind_search_keys()
	unbind_keys()

	bind_key('ANY_UNICODE', search_input_text, {complex=true})
	bind_key('BS', search_input_bs, {repeatable=true})
	bind_key('DEL', search_input_del, {repeatable=true})

	bind_key('ENTER', end_search)
	bind_key('ESC', cancel_search)
	bind_key('Ctrl+c', cancel_search)

	bind_key('LEFT', search_cursor_left, {repeatable=true})
	bind_key('RIGHT', search_cursor_right, {repeatable=true})
	bind_key('Ctrl+a', search_cursor_start)
	bind_key('Ctrl+e', search_cursor_end)
end

function bind_menu_keys()
	unbind_keys()

	bind_key('BS', prev_menu)
	bind_key('/', start_search)

	bind_key('ENTER', select_option)
	bind_key('Ctrl+f', favourite_option)

	bind_key('UP', cursor_up, {repeatable=true})
	bind_key('DOWN', cursor_down, {repeatable=true})
	bind_key('HOME', cursor_start)
	bind_key('END', cursor_end)
	bind_key('PGUP', cursor_page_up, {repeatable=true})
	bind_key('PGDWN', cursor_page_down, {repeatable=true})
end

local function toggle_menu()
	osd.hidden = not osd.hidden
	osd:update()
	osd_bg.hidden = osd.hidden
	osd_bg:update()

	if osd.hidden then
		unbind_keys()
	elseif menus[depth].search_active then
		bind_search_keys()
	else
		bind_menu_keys()
	end
end

mp.register_event('start-file', function()
	playing_id = tonumber(mp.get_opt('iptv_menu.playing_id'))
	update_osd()
end)

mp.register_event('end-file', function()
	playing_id = nil
	update_osd()
end)

mp.add_forced_key_binding('TAB', 'toggle-menu', toggle_menu)
bind_menu_keys()
load_data()
push_group_menu(objects['root'])