summaryrefslogtreecommitdiff
path: root/xc.lua
blob: b96d2a15b8a0c7eb406ec4b38335a64191075cff (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
-- Copyright 2025 David Vazgenovich Shakaryan

local mp_utils = require('mp.utils')

local xc = {}
local mt = {}
mt.__index = mt

function xc.new(t)
	assert(t.server)
	assert(t.user)
	assert(t.pass)

	return setmetatable(t, mt)
end

function mt:get(path, params)
	local url =
		self.server .. path ..
		'?username=' .. self.user ..
		'&password=' .. self.pass
	for k, v in pairs(params or {}) do
		url = url .. '&' .. k .. '=' .. v
	end
	local cmd = {'curl', '-sSfL', url}
	print('querying ' .. url)

	local res = mp.command_native({
		name = 'subprocess',
		args = cmd,
		capture_stdout = true,
		playback_only = false,
	})
	if res.status == 0 then
		return res.stdout
	end
end

function mt:api_get(params)
	return mp_utils.parse_json(self:get('/player_api.php', params))
end

function mt:get_live_categories()
	return self:api_get({action = 'get_live_categories'})
end

function mt:get_live_streams()
	return self:api_get({action = 'get_live_streams'})
end

function mt:get_vod_categories()
	return self:api_get({action = 'get_vod_categories'})
end

function mt:get_vod_streams()
	return self:api_get({action = 'get_vod_streams'})
end

function mt:get_vod_info(id)
	return self:api_get({action = 'get_vod_info', vod_id = id})
end

function mt:get_series_categories()
	return self:api_get({action = 'get_series_categories'})
end

function mt:get_series()
	return self:api_get({action = 'get_series'})
end

function mt:get_series_info(id)
	return self:api_get({action = 'get_series_info', series_id = id})
end

function mt:get_epg()
	local xml = self:get('/xmltv.php')

	-- this is a bit retarded, but using python to convert xml to json
	-- avoids having to write an xml parser or pull in external
	-- dependencies
	local cmd = {'python', '-c', [[
import json
import sys
import xml.etree.ElementTree as ET

json.dump(
	[
		{**{x.tag: x.text for x in p}, **p.attrib}
		for p in ET.parse(sys.stdin).iter('programme')],
	sys.stdout)
]]}

	local res = mp.command_native({
		name = 'subprocess',
		args = cmd,
		stdin_data = xml,
		capture_stdout = true,
		playback_only = false,
	})
	if res.status == 0 then
		return mp_utils.parse_json(res.stdout)
	end
end

function mt:stream_url(stream_type, stream_id)
	if stream_type == 'series' then
		return self.server .. '/series/' ..
			self.user .. '/' ..
			self.pass .. '/' ..
			stream_id .. '.vod'
	else
		return self.server .. '/' ..
			self.user .. '/' ..
			self.pass .. '/' ..
			stream_id
	end
end

return xc