1 | -- Plugin for converting channels lists from coolstream receivers |
---|
2 | -- Author focus.cst@gmail.com |
---|
3 | -- License GPL v2 |
---|
4 | -- Copyright (C) 2013 CoolStream International Ltd |
---|
5 | |
---|
6 | -- flag to test as plain script, without xupnpd - cfg not defined in this case |
---|
7 | local cst_test = false |
---|
8 | |
---|
9 | if not cfg then |
---|
10 | cfg={} |
---|
11 | cfg.tmp_path='/tmp/' |
---|
12 | cfg.feeds_path='/tmp/' |
---|
13 | cfg.debug=1 |
---|
14 | cst_test = true |
---|
15 | end |
---|
16 | |
---|
17 | function cst_debug(level, msg) |
---|
18 | if cfg.debug>level then |
---|
19 | print(msg) |
---|
20 | end |
---|
21 | end |
---|
22 | |
---|
23 | function cst_get_bouquets(file) |
---|
24 | local btable={} |
---|
25 | repeat |
---|
26 | local string=file:read() |
---|
27 | if string then |
---|
28 | cst_debug(1, "########## bouquet="..string) |
---|
29 | local num = string.match(string, "%d+"); |
---|
30 | if num then |
---|
31 | local len = string.len(num); |
---|
32 | local name = string.sub(string, len+1); |
---|
33 | btable[num] = name |
---|
34 | cst_debug(1, "num="..num.." name="..btable[num]); |
---|
35 | end |
---|
36 | --break; -- one bouquet |
---|
37 | end |
---|
38 | until not string |
---|
39 | return btable |
---|
40 | end |
---|
41 | |
---|
42 | function cst_get_channels(file) |
---|
43 | local ctable={} |
---|
44 | repeat |
---|
45 | local string=file:read() |
---|
46 | idx = 1; |
---|
47 | if string then |
---|
48 | cst_debug(1, "########## channel="..string) |
---|
49 | local num = string.match(string, "%d+"); |
---|
50 | if num then |
---|
51 | local len = string.len(num); |
---|
52 | local rest = string.sub(string, len+1); |
---|
53 | local id = string.match(rest, "%x+ "); |
---|
54 | len = string.len(id); |
---|
55 | local name = string.sub(rest, len+2); |
---|
56 | cst_debug(1, "num="..num.." id="..id.." name="..name) |
---|
57 | if id and name then |
---|
58 | table.insert(ctable, {id, name}); |
---|
59 | idx = idx + 1; |
---|
60 | end |
---|
61 | end |
---|
62 | end |
---|
63 | until not string |
---|
64 | return ctable |
---|
65 | end |
---|
66 | |
---|
67 | -- all bouquets |
---|
68 | -- local burl = "getbouquets" |
---|
69 | -- only favorites |
---|
70 | local burl = "getbouquets?fav=true" |
---|
71 | |
---|
72 | -- without epg |
---|
73 | -- local curl = "getbouquet?bouquet=" |
---|
74 | -- with epg |
---|
75 | local curl = "getbouquet?epg=true&bouquet=" |
---|
76 | |
---|
77 | function cst_updatefeed(feed,friendly_name) |
---|
78 | local rc=false |
---|
79 | local feedspath = cfg.feeds_path |
---|
80 | if not friendly_name then |
---|
81 | friendly_name = feed |
---|
82 | end |
---|
83 | local wget = "wget -q -O- " |
---|
84 | local cst_url = 'http://'..feed..'/control/' |
---|
85 | |
---|
86 | cst_debug(0, wget..cst_url..burl) |
---|
87 | local bouquetsfile = io.popen(wget..cst_url..burl) |
---|
88 | local bouquets = cst_get_bouquets(bouquetsfile) |
---|
89 | bouquetsfile:close() |
---|
90 | |
---|
91 | if not bouquets then |
---|
92 | return rc |
---|
93 | end |
---|
94 | local bindex |
---|
95 | local bouquett = {} |
---|
96 | for bindex,bouquett in pairs(bouquets) do |
---|
97 | local cindex |
---|
98 | local channelt = {} |
---|
99 | cst_debug(0,wget.."\""..cst_url..curl..bindex.."\"") |
---|
100 | local xmlbouquetfile = io.popen(wget.."\""..cst_url..curl..bindex.."\"") |
---|
101 | local bouquet = cst_get_channels(xmlbouquetfile) |
---|
102 | xmlbouquetfile:close() |
---|
103 | if bouquet then |
---|
104 | local m3ufilename = cfg.tmp_path.."cst_"..friendly_name.."_bouquet_"..bindex..".m3u" |
---|
105 | cst_debug(0, m3ufilename) |
---|
106 | local m3ufile = io.open(m3ufilename,"w") |
---|
107 | m3ufile:write("#EXTM3U name=\""..bouquett.." ("..friendly_name..")\" plugin=coolstream type=ts\n") |
---|
108 | for cindex,channelt in pairs(bouquet) do |
---|
109 | local id = channelt[1]; |
---|
110 | local name = channelt[2]; |
---|
111 | m3ufile:write("#EXTINF:0,"..name.."\n") |
---|
112 | -- m3ufile:write(cst_url.."zapto?"..id.."\n") |
---|
113 | m3ufile:write("http://"..feed..":31339/id="..id.."\n") |
---|
114 | end |
---|
115 | m3ufile:close() |
---|
116 | os.execute(string.format('mv %s %s',m3ufilename,feedspath)) |
---|
117 | rc=true |
---|
118 | end |
---|
119 | end |
---|
120 | return rc |
---|
121 | end |
---|
122 | |
---|
123 | function cst_read_url(url) |
---|
124 | local wget = "wget -q -O- " |
---|
125 | local turl = wget..url |
---|
126 | cst_debug(0, turl) |
---|
127 | local file = io.popen(turl) |
---|
128 | local string = file:read() |
---|
129 | file:close() |
---|
130 | return string |
---|
131 | end |
---|
132 | |
---|
133 | function cst_zapto(urlbase,id) |
---|
134 | local zap = urlbase.."/control/zapto?"..id; |
---|
135 | cst_read_url(zap) |
---|
136 | end |
---|
137 | |
---|
138 | function cst_sendurl(cst_url,range) |
---|
139 | local i,j,baseurl = string.find(cst_url,"(.+):.+") |
---|
140 | cst_debug(0, "cst_sendurl: url="..cst_url.." baseurl="..baseurl) |
---|
141 | |
---|
142 | i,j,id = string.find(cst_url,".*id=(.+)") |
---|
143 | local surl = baseurl.."/control/standby" |
---|
144 | local standby = cst_read_url(surl) |
---|
145 | |
---|
146 | if standby then |
---|
147 | cst_debug(0, "standby="..standby) |
---|
148 | |
---|
149 | -- wakeup from standby |
---|
150 | if string.find(standby,"on") then |
---|
151 | cst_read_url(surl.."?off") |
---|
152 | end |
---|
153 | end |
---|
154 | -- zap to channel |
---|
155 | cst_zapto(baseurl,id) |
---|
156 | |
---|
157 | if not cst_test then |
---|
158 | plugin_sendurl(cst_url,cst_url,range) |
---|
159 | end |
---|
160 | end |
---|
161 | |
---|
162 | if cst_test then |
---|
163 | cst_updatefeed("172.16.1.20","tank") |
---|
164 | -- cst_updatefeed("172.16.1.10","tank") |
---|
165 | -- cst_sendurl("http://172.16.1.20:31339/id=c1f000010070277a", 0) |
---|
166 | end |
---|
167 | |
---|
168 | if not cst_test then |
---|
169 | plugins['coolstream']={} |
---|
170 | plugins.coolstream.name="CoolStream" |
---|
171 | plugins.coolstream.desc="IP address (example: <i>192.168.0.1</i>)" |
---|
172 | plugins.coolstream.updatefeed=cst_updatefeed |
---|
173 | plugins.coolstream.sendurl=cst_sendurl |
---|
174 | end |
---|