[EPGRefresh] removed NumVideoDecoders depend for background refresh
[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 from OrderedSet import OrderedSet
9 from ServiceReference import ServiceReference
10 from Tools.XMLTools import stringToXML
11 try:
12         from urllib import unquote
13         iteritems = lambda d: d.iteritems()
14 except ImportError as ie:
15         from urllib.parse import unquote
16         iteritems = lambda d: d.items()
17
18 API_VERSION = "1.4"
19
20 class EPGRefreshStartRefreshResource(resource.Resource):
21         def render(self, req):
22                 state = False
23
24                 if epgrefresh.forceRefresh():
25                         output = "initiated refresh"
26                         state = True
27                 else:
28                         output = "could not initiate refresh"
29
30                 req.setResponseCode(http.OK)
31                 req.setHeader('Content-type', 'application/xhtml+xml')
32                 req.setHeader('charset', 'UTF-8')
33
34                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
35 <e2simplexmlresult>
36  <e2state>%s</e2state>
37  <e2statetext>%s</e2statetext>
38 </e2simplexmlresult>""" % ('true' if state else 'false', output)
39
40 class EPGRefreshAddRemoveServiceResource(resource.Resource):
41         TYPE_ADD = 0
42         TYPE_DEL = 1
43
44         def __init__(self, type):
45                 assert(type in (self.TYPE_ADD, self.TYPE_DEL))
46                 self.type = type
47
48         def render(self, req):
49                 do_add = self.type == self.TYPE_ADD
50                 state = False
51
52                 if 'multi' in req.args:
53                         if epgrefresh.services[0]:
54                                 epgrefresh.services[0].clear()
55                                 state = True
56                         if epgrefresh.services[1]:
57                                 epgrefresh.services[1].clear()
58                                 state = True
59
60                 if 'sref' in req.args:
61                         duration = req.args.get("duration", None)
62                         try:
63                                 duration = duration and int(duration)
64                         except ValueError as ve:
65                                 output = 'invalid value for "duration": ' + str(duration)
66                         else:
67                                 for sref in req.args.get('sref'):
68                                         sref = unquote(sref)
69                                         ref = eServiceReference(sref)
70                                         if not ref.valid():
71                                                 output = 'invalid value for "sref": ' + str(sref)
72                                         elif (ref.flags & 7) == 7:
73                                                 epgservice = EPGRefreshService(sref, duration)
74                                                 # bouquet
75                                                 if epgservice in epgrefresh.services[1]:
76                                                         if do_add:
77                                                                 output = "bouquet already in list"
78                                                         else:
79                                                                 epgrefresh.services[1].remove(epgservice)
80                                                                 output = "bouquet removed from list"
81                                                                 state = True
82                                                 else:
83                                                         if do_add:
84                                                                 epgrefresh.services[1].add(epgservice)
85                                                                 output = "bouquet added to list"
86                                                                 state = True
87                                                         else:
88                                                                 output = "bouquet not in list"
89                                         else:
90                                                 if not (ref.flags & eServiceReference.isGroup):
91                                                         # strip all after last :
92                                                         pos = sref.rfind(':')
93                                                         if pos != -1:
94                                                                 if sref[pos-1] == ':':
95                                                                         pos -= 1
96                                                                 sref = sref[:pos+1]
97
98                                                 epgservice = EPGRefreshService(sref, duration)
99                                                 # assume service
100                                                 if epgservice in epgrefresh.services[0]:
101                                                         if do_add:
102                                                                 output = "service already in list"
103                                                         else:
104                                                                 epgrefresh.services[0].remove(epgservice)
105                                                                 output = "service removed from list"
106                                                                 state = True
107                                                 else:
108                                                         if do_add:
109                                                                 epgrefresh.services[0].add(epgservice)
110                                                                 output = "service added to list"
111                                                                 state = True
112                                                         else:
113                                                                 output = "service not in list"
114
115                                 # save if list changed
116                                 if state:
117                                         epgrefresh.saveConfiguration()
118                 else:
119                         output = 'missing argument "sref"'
120
121                 if 'multi' in req.args:
122                         output = 'service restriction changed'
123
124                 req.setResponseCode(http.OK)
125                 req.setHeader('Content-type', 'application/xhtml+xml')
126                 req.setHeader('charset', 'UTF-8')
127                 
128                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
129 <e2simplexmlresult>
130  <e2state>%s</e2state>
131  <e2statetext>%s</e2statetext>
132 </e2simplexmlresult> """ % ('True' if state else 'False', output)
133
134 class EPGRefreshListServicesResource(resource.Resource):
135         def render(self, req):
136                 # show xml
137                 req.setResponseCode(http.OK)
138                 req.setHeader('Content-type', 'application/xhtml+xml')
139                 req.setHeader('charset', 'UTF-8')
140                 return ''.join(epgrefresh.buildConfiguration(webif = True))
141
142 class EPGRefreshPreviewServicesResource(resource.Resource):
143         def render(self, req):
144                 req.setResponseCode(http.OK)
145                 req.setHeader('Content-type', 'application/xhtml+xml')
146                 req.setHeader('charset', 'UTF-8')
147
148                 if 'sref' in req.args:
149                         services = OrderedSet()
150                         bouquets = OrderedSet()
151                         for sref in req.args.get('sref'):
152                                 sref = unquote(sref)
153                                 ref = eServiceReference(sref)
154                                 if not ref.valid():
155                                         services = bouquets = None
156                                         break
157                                 elif (ref.flags & 7) == 7:
158                                         epgservice = EPGRefreshService(sref, None)
159                                         if epgservice not in bouquets:
160                                                 bouquets.add(epgservice)
161                                 else:
162                                         if not (ref.flags & eServiceReference.isGroup):
163                                                 # strip all after last :
164                                                 pos = sref.rfind(':')
165                                                 if pos != -1:
166                                                         if sref[pos-1] == ':':
167                                                                 pos -= 1
168                                                         sref = sref[:pos+1]
169
170                                         epgservice = EPGRefreshService(sref, None)
171                                         if epgservice not in services:
172                                                 services.add(epgservice)
173                         if services is not None and bouquets is not None:
174                                 scanServices = epgrefresh.generateServicelist(services, bouquets)
175                         else:
176                                 scanServices = []
177                 else:
178                         scanServices = epgrefresh.generateServicelist(epgrefresh.services[0], epgrefresh.services[1])
179
180                 returnlist = ["<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<e2servicelist>"]
181                 extend = returnlist.extend
182                 for serviceref in scanServices:
183                         ref = ServiceReference(str(serviceref))
184                         returnlist.extend((
185                                 ' <e2service>\n',
186                                 '  <e2servicereference>', stringToXML(str(serviceref)), '</e2servicereference>\n',
187                                 '  <e2servicename>', stringToXML(ref.getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '')), '</e2servicename>\n',
188                                 ' </e2service>\n',
189                         ))
190                 returnlist.append('\n</e2servicelist>')
191                 return ''.join(returnlist)
192
193 class EPGRefreshChangeSettingsResource(resource.Resource):
194         def render(self, req):
195                 statetext = "config changed."
196                 for key, value in iteritems(req.args):
197                         value = value[0]
198                         if key == "enabled":
199                                 config.plugins.epgrefresh.enabled.value = True if value == "true" else False
200                         elif key == "enablemessage":
201                                 config.plugins.epgrefresh.enablemessage.value = True if value == "true" else False
202                         elif key == "begin":
203                                 value = int(value)
204                                 if value:
205                                         t = localtime(value)
206                                         config.plugins.epgrefresh.begin.value = [t.tm_hour, t.tm_min]
207                         elif key == "end":
208                                 value = int(value)
209                                 if value:
210                                         t = localtime(int(value))
211                                         config.plugins.epgrefresh.end.value = [t.tm_hour, t.tm_min]
212                         elif key == "interval_seconds":
213                                 value = int(value)
214                                 if value:
215                                         config.plugins.epgrefresh.interval_seconds.value = value
216                         elif key == "delay_standby":
217                                 value = int(value)
218                                 if value:
219                                         config.plugins.epgrefresh.delay_standby.value = value
220                         elif key == "inherit_autotimer":
221                                 config.plugins.epgrefresh.inherit_autotimer.value = True if value == "true" else False
222                         elif key == "afterevent":
223                                 config.plugins.epgrefresh.afterevent.value = True if value == "true" else False
224                         elif key == "force":
225                                 config.plugins.epgrefresh.force.value = True if value == "true" else False
226                         elif key == "wakeup":
227                                 config.plugins.epgrefresh.wakeup.value = True if value == "true" else False
228                         elif key == "parse_autotimer":
229                                 if value in config.plugins.epgrefresh.parse_autotimer.choices:
230                                         config.plugins.epgrefresh.parse_autotimer.value = value
231                                 elif value == "true":
232                                         config.plugins.epgrefresh.parse_autotimer.value = "always"
233                                 else:
234                                         config.plugins.epgrefresh.parse_autotimer.value = "never"
235                         elif key == "adapter":
236                                 if value in config.plugins.epgrefresh.adapter.choices:
237                                         config.plugins.epgrefresh.adapter.value = value
238                         elif key == "skipProtectedServices":
239                                 if value in config.plugins.epgrefresh.skipProtectedServices.choices:
240                                         config.plugins.epgrefresh.skipProtectedServices.value = value
241
242                 config.plugins.epgrefresh.save()
243
244                 if config.plugins.epgrefresh.enabled.value:
245                         epgrefresh.start()
246                 else:
247                         epgrefresh.stop()
248
249                 req.setResponseCode(http.OK)
250                 req.setHeader('Content-type', 'application/xhtml+xml')
251                 req.setHeader('charset', 'UTF-8')
252
253                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
254 <e2simplexmlresult>
255  <e2state>true</e2state>
256  <e2statetext>%s</e2statetext>
257 </e2simplexmlresult>""" % (statetext,)
258
259 class EPGRefreshSettingsResource(resource.Resource):
260         def render(self, req):
261                 req.setResponseCode(http.OK)
262                 req.setHeader('Content-type', 'application/xhtml+xml')
263                 req.setHeader('charset', 'UTF-8')
264
265                 from time import time, localtime, mktime
266                 now = localtime()
267                 begin_h = config.plugins.epgrefresh.begin.value
268                 begin = mktime((
269                         now.tm_year, now.tm_mon, now.tm_mday, begin_h[0], begin_h[1],
270                         0, now.tm_wday, now.tm_yday, now.tm_isdst)
271                 )
272                 end_h = config.plugins.epgrefresh.end.value
273                 end = mktime((
274                         now.tm_year, now.tm_mon, now.tm_mday, end_h[0], end_h[1],
275                         0, now.tm_wday, now.tm_yday, now.tm_isdst)
276                 )
277
278                 canDoBackgroundRefresh = True
279                 hasAutoTimer = False
280                 try:
281                         from Plugins.Extensions.AutoTimer.AutoTimer import AutoTimer
282                         hasAutoTimer = True
283                 except ImportError as ie: pass
284
285                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
286 <e2settings>
287  <e2setting>
288   <e2settingname>config.plugins.epgrefresh.enabled</e2settingname>
289   <e2settingvalue>%s</e2settingvalue>
290  </e2setting>
291  <e2setting>
292   <e2settingname>config.plugins.epgrefresh.enablemessage</e2settingname>
293   <e2settingvalue>%s</e2settingvalue>
294  </e2setting>
295  <e2setting>
296   <e2settingname>config.plugins.epgrefresh.begin</e2settingname>
297   <e2settingvalue>%d</e2settingvalue>
298  </e2setting>
299  <e2setting>
300   <e2settingname>config.plugins.epgrefresh.end</e2settingname>
301   <e2settingvalue>%d</e2settingvalue>
302  </e2setting>
303  <e2setting>
304   <e2settingname>config.plugins.epgrefresh.interval_seconds</e2settingname>
305   <e2settingvalue>%d</e2settingvalue>
306  </e2setting>
307  <e2setting>
308   <e2settingname>config.plugins.epgrefresh.delay_standby</e2settingname>
309   <e2settingvalue>%d</e2settingvalue>
310  </e2setting>
311  <e2setting>
312   <e2settingname>config.plugins.epgrefresh.inherit_autotimer</e2settingname>
313   <e2settingvalue>%s</e2settingvalue>
314  </e2setting>
315  <e2setting>
316   <e2settingname>config.plugins.epgrefresh.afterevent</e2settingname>
317   <e2settingvalue>%s</e2settingvalue>
318  </e2setting>
319  <e2setting>
320   <e2settingname>config.plugins.epgrefresh.force</e2settingname>
321   <e2settingvalue>%s</e2settingvalue>
322  </e2setting>
323  <e2setting>
324   <e2settingname>config.plugins.epgrefresh.wakeup</e2settingname>
325   <e2settingvalue>%s</e2settingvalue>
326  </e2setting>
327  <e2setting>
328   <e2settingname>config.plugins.epgrefresh.parse_autotimer</e2settingname>
329   <e2settingvalue>%s</e2settingvalue>
330  </e2setting>
331  <e2setting>
332   <e2settingname>config.plugins.epgrefresh.lastscan</e2settingname>
333   <e2settingvalue>%d</e2settingvalue>
334  </e2setting>
335  <e2setting>
336   <e2settingname>config.plugins.epgrefresh.adapter</e2settingname>
337   <e2settingvalue>%s</e2settingvalue>
338  </e2setting>
339  <e2setting>
340   <e2settingname>config.plugins.epgrefresh.skipProtectedServices</e2settingname>
341   <e2settingvalue>%s</e2settingvalue>
342  </e2setting>
343  <e2setting>
344   <e2settingname>canDoBackgroundRefresh</e2settingname>
345   <e2settingvalue>%s</e2settingvalue>
346  </e2setting>
347  <e2setting>
348   <e2settingname>hasAutoTimer</e2settingname>
349   <e2settingvalue>%s</e2settingvalue>
350  </e2setting>
351  <e2setting>
352   <e2settingname>api_version</e2settingname>
353   <e2settingvalue>%s</e2settingvalue>
354  </e2setting>
355 </e2settings>""" % (
356                                 config.plugins.epgrefresh.enabled.value,
357                                 config.plugins.epgrefresh.enablemessage.value,
358                                 begin,
359                                 end,
360                                 config.plugins.epgrefresh.interval_seconds.value,
361                                 config.plugins.epgrefresh.delay_standby.value,
362                                 config.plugins.epgrefresh.inherit_autotimer.value,
363                                 config.plugins.epgrefresh.afterevent.value,
364                                 config.plugins.epgrefresh.force.value,
365                                 config.plugins.epgrefresh.wakeup.value,
366                                 config.plugins.epgrefresh.parse_autotimer.value,
367                                 config.plugins.epgrefresh.lastscan.value,
368                                 config.plugins.epgrefresh.adapter.value,
369                                 config.plugins.epgrefresh.skipProtectedServices.value,
370                                 canDoBackgroundRefresh,
371                                 hasAutoTimer,
372                                 API_VERSION,
373                         )
374