autotimer/epgrefresh: follow webif change
[enigma2-plugins.git] / epgrefresh / src / EPGRefreshResource.py
1 from twisted.web import http, resource
2 from EPGRefresh import epgrefresh
3 from EPGRefreshService import EPGRefreshService
4 from enigma import eServiceReference
5 from Components.config import config
6 from Components.SystemInfo import SystemInfo
7 from time import localtime
8 try:
9         from urllib import unquote
10         iteritems = lambda d: d.iteritems()
11 except ImportError as ie:
12         from urllib.parse import unquote
13         iteritems = lambda d: d.items()
14
15 API_VERSION = "1.0"
16
17 class EPGRefreshStartRefreshResource(resource.Resource):
18         def render(self, req):
19                 state = False
20
21                 if epgrefresh.forceRefresh():
22                         output = "initiated refresh"
23                         state = True
24                 else:
25                         output = "could not initiate refresh"
26
27                 req.setResponseCode(http.OK)
28                 req.setHeader('Content-type', 'application; xhtml+xml')
29                 req.setHeader('charset', 'UTF-8')
30
31                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
32 <e2simplexmlresult>
33  <e2state>%s</e2state>
34  <e2statetext>%s</e2statetext>
35 </e2simplexmlresult>""" % ('true' if state else 'false', output)
36
37 class EPGRefreshAddRemoveServiceResource(resource.Resource):
38         TYPE_ADD = 0
39         TYPE_DEL = 1
40
41         def __init__(self, type):
42                 assert(type in (self.TYPE_ADD, self.TYPE_DEL))
43                 self.type = type
44
45         def render(self, req):
46                 do_add = self.type == self.TYPE_ADD
47                 state = False
48
49                 if 'multi' in req.args:
50                         if epgrefresh.services[0]:
51                                 epgrefresh.services[0].clear()
52                                 state = True
53                         if epgrefresh.services[1]:
54                                 epgrefresh.services[1].clear()
55                                 state = True
56
57                 if 'sref' in req.args:
58                         duration = req.args.get("duration", None)
59                         try:
60                                 duration = duration and int(duration)
61                         except ValueError as ve:
62                                 output = 'invalid value for "duration": ' + str(duration)
63                         else:
64                                 for sref in req.args.get('sref'):
65                                         sref = unquote(sref)
66                                         ref = eServiceReference(sref)
67                                         if not ref.valid():
68                                                 output = 'invalid value for "sref": ' + str(sref)
69                                         elif (ref.flags & 7) == 7:
70                                                 epgservice = EPGRefreshService(sref, duration)
71                                                 # bouquet
72                                                 if epgservice in epgrefresh.services[1]:
73                                                         if do_add:
74                                                                 output = "bouquet already in list"
75                                                         else:
76                                                                 epgrefresh.services[1].remove(epgservice)
77                                                                 output = "bouquet removed from list"
78                                                                 state = True
79                                                 else:
80                                                         if do_add:
81                                                                 epgrefresh.services[1].add(epgservice)
82                                                                 output = "bouquet added to list"
83                                                                 state = True
84                                                         else:
85                                                                 output = "bouquet not in list"
86                                         else:
87                                                 if not (ref.flags & eServiceReference.isGroup):
88                                                         # strip all after last :
89                                                         pos = sref.rfind(':')
90                                                         if pos != -1:
91                                                                 if sref[pos-1] == ':':
92                                                                         pos -= 1
93                                                                 sref = sref[:pos+1]
94
95                                                 epgservice = EPGRefreshService(sref, duration)
96                                                 # assume service
97                                                 if epgservice in epgrefresh.services[0]:
98                                                         if do_add:
99                                                                 output = "service already in list"
100                                                         else:
101                                                                 epgrefresh.services[0].remove(epgservice)
102                                                                 output = "service removed from list"
103                                                                 state = True
104                                                 else:
105                                                         if do_add:
106                                                                 epgrefresh.services[0].add(epgservice)
107                                                                 output = "service added to list"
108                                                                 state = True
109                                                         else:
110                                                                 output = "service not in list"
111
112                                 # save if list changed
113                                 if state:
114                                         epgrefresh.saveConfiguration()
115                 else:
116                         output = 'missing argument "sref"'
117
118                 if 'multi' in req.args:
119                         output = 'service restriction changed'
120
121                 req.setResponseCode(http.OK)
122                 req.setHeader('Content-type', 'application; xhtml+xml')
123                 req.setHeader('charset', 'UTF-8')
124                 
125                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
126 <e2simplexmlresult>
127  <e2state>%s</e2state>
128  <e2statetext>%s</e2statetext>
129 </e2simplexmlresult> """ % ('True' if state else 'False', output)
130
131 class EPGRefreshListServicesResource(resource.Resource):
132         def render(self, req):
133                 # show xml
134                 req.setResponseCode(http.OK)
135                 req.setHeader('Content-type', 'application; xhtml+xml')
136                 req.setHeader('charset', 'UTF-8')
137                 return ''.join(epgrefresh.buildConfiguration(webif = True))
138
139 class EPGRefreshChangeSettingsResource(resource.Resource):
140         def render(self, req):
141                 statetext = "config changed."
142                 for key, value in iteritems(req.args):
143                         value = value[0]
144                         if key == "enabled":
145                                 config.plugins.epgrefresh.enabled.value = True if value == "true" else False
146                         elif key == "enablemessage":
147                                 config.plugins.epgrefresh.enablemessage.value = True if value == "true" else False
148                         elif key == "begin":
149                                 value = int(value)
150                                 if value:
151                                         t = localtime(value)
152                                         config.plugins.epgrefresh.begin.value = [t.tm_hour, t.tm_min]
153                         elif key == "end":
154                                 value = int(value)
155                                 if value:
156                                         t = localtime(int(value))
157                                         config.plugins.epgrefresh.end.value = [t.tm_hour, t.tm_min]
158                         elif key == "interval":
159                                 value = int(value)
160                                 if value:
161                                         config.plugins.epgrefresh.interval.value = value
162                         elif key == "delay_standby":
163                                 value = int(value)
164                                 if value:
165                                         config.plugins.epgrefresh.delay_standby.value = value
166                         elif key == "inherit_autotimer":
167                                 config.plugins.epgrefresh.inherit_autotimer.value = True if value == "true" else False
168                         elif key == "afterevent":
169                                 config.plugins.epgrefresh.afterevent.value = True if value == "true" else False
170                         elif key == "force":
171                                 config.plugins.epgrefresh.force.value = True if value == "true" else False
172                         elif key == "wakeup":
173                                 config.plugins.epgrefresh.wakeup.value = True if value == "true" else False
174                         elif key == "parse_autotimer":
175                                 config.plugins.epgrefresh.parse_autotimer.value = True if value == "true" else False
176                         elif key == "background":
177                                 statetext += " parameter \"background\" is deprecated. please use new \"adapter\" parameter instead."
178                                 config.plugins.epgrefresh.adapter.value = "pip_hidden" if value == "true" else "main"
179                         elif key == "adapter":
180                                 if value in config.plugins.epgrefresh.adapter.choices:
181                                         config.plugins.epgrefresh.adapter.value = value
182
183                 config.plugins.epgrefresh.save()
184
185                 if config.plugins.epgrefresh.enabled.value:
186                         epgrefresh.start()
187                 else:
188                         epgrefresh.stop()
189
190                 req.setResponseCode(http.OK)
191                 req.setHeader('Content-type', 'application; xhtml+xml')
192                 req.setHeader('charset', 'UTF-8')
193
194                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
195 <e2simplexmlresult>
196  <e2state>true</e2state>
197  <e2statetext>%s</e2statetext>
198 </e2simplexmlresult>""" % (statetext,)
199
200 class EPGRefreshSettingsResource(resource.Resource):
201         def render(self, req):
202                 req.setResponseCode(http.OK)
203                 req.setHeader('Content-type', 'application; xhtml+xml')
204                 req.setHeader('charset', 'UTF-8')
205
206                 from time import time, localtime, mktime
207                 now = localtime()
208                 begin_h = config.plugins.epgrefresh.begin.value
209                 begin = mktime((
210                         now.tm_year, now.tm_mon, now.tm_mday, begin_h[0], begin_h[1],
211                         0, now.tm_wday, now.tm_yday, now.tm_isdst)
212                 )
213                 end_h = config.plugins.epgrefresh.end.value
214                 end = mktime((
215                         now.tm_year, now.tm_mon, now.tm_mday, end_h[0], end_h[1],
216                         0, now.tm_wday, now.tm_yday, now.tm_isdst)
217                 )
218
219                 canDoBackgroundRefresh = SystemInfo.get("NumVideoDecoders", 1) > 1
220                 hasAutoTimer = False
221                 try:
222                         from Plugins.Extensions.AutoTimer.AutoTimer import AutoTimer
223                         hasAutoTimer = True
224                 except ImportError as ie: pass
225
226                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
227 <e2settings>
228  <e2setting>
229   <e2settingname>config.plugins.epgrefresh.enabled</e2settingname>
230   <e2settingvalue>%s</e2settingvalue>
231  </e2setting>
232  <e2setting>
233   <e2settingname>config.plugins.epgrefresh.enablemessage</e2settingname>
234   <e2settingvalue>%s</e2settingvalue>
235  </e2setting>
236  <e2setting>
237   <e2settingname>config.plugins.epgrefresh.begin</e2settingname>
238   <e2settingvalue>%d</e2settingvalue>
239  </e2setting>
240  <e2setting>
241   <e2settingname>config.plugins.epgrefresh.end</e2settingname>
242   <e2settingvalue>%d</e2settingvalue>
243  </e2setting>
244  <e2setting>
245   <e2settingname>config.plugins.epgrefresh.interval</e2settingname>
246   <e2settingvalue>%d</e2settingvalue>
247  </e2setting>
248  <e2setting>
249   <e2settingname>config.plugins.epgrefresh.delay_standby</e2settingname>
250   <e2settingvalue>%d</e2settingvalue>
251  </e2setting>
252  <e2setting>
253   <e2settingname>config.plugins.epgrefresh.inherit_autotimer</e2settingname>
254   <e2settingvalue>%s</e2settingvalue>
255  </e2setting>
256  <e2setting>
257   <e2settingname>config.plugins.epgrefresh.afterevent</e2settingname>
258   <e2settingvalue>%s</e2settingvalue>
259  </e2setting>
260  <e2setting>
261   <e2settingname>config.plugins.epgrefresh.force</e2settingname>
262   <e2settingvalue>%s</e2settingvalue>
263  </e2setting>
264  <e2setting>
265   <e2settingname>config.plugins.epgrefresh.wakeup</e2settingname>
266   <e2settingvalue>%s</e2settingvalue>
267  </e2setting>
268  <e2setting>
269   <e2settingname>config.plugins.epgrefresh.parse_autotimer</e2settingname>
270   <e2settingvalue>%s</e2settingvalue>
271  </e2setting>
272  <e2setting>
273   <!-- deprecated, pending removal -->
274   <e2settingname>config.plugins.epgrefresh.background</e2settingname>
275   <e2settingvalue>%s</e2settingvalue>
276  </e2setting>
277  <e2setting>
278   <e2settingname>config.plugins.epgrefresh.adapter</e2settingname>
279   <e2settingvalue>%s</e2settingvalue>
280  </e2setting>
281  <e2setting>
282   <e2settingname>canDoBackgroundRefresh</e2settingname>
283   <e2settingvalue>%s</e2settingvalue>
284  </e2setting>
285  <e2setting>
286   <e2settingname>hasAutoTimer</e2settingname>
287   <e2settingvalue>%s</e2settingvalue>
288  </e2setting>
289 </e2settings>""" % (
290                                 config.plugins.epgrefresh.enabled.value,
291                                 config.plugins.epgrefresh.enablemessage.value,
292                                 begin,
293                                 end,
294                                 config.plugins.epgrefresh.interval.value,
295                                 config.plugins.epgrefresh.delay_standby.value,
296                                 config.plugins.epgrefresh.inherit_autotimer.value,
297                                 config.plugins.epgrefresh.afterevent.value,
298                                 config.plugins.epgrefresh.force.value,
299                                 config.plugins.epgrefresh.wakeup.value,
300                                 config.plugins.epgrefresh.parse_autotimer.value,
301                                 config.plugins.epgrefresh.adapter.value in ("pip", "pip_hidden"),
302                                 config.plugins.epgrefresh.adapter.value,
303                                 canDoBackgroundRefresh,
304                                 hasAutoTimer,
305                         )
306