its mandatory to call deleteDialog for the pip screen
[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.3"
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 key == "adapter":
232                                 if value in config.plugins.epgrefresh.adapter.choices:
233                                         config.plugins.epgrefresh.adapter.value = value
234
235                 config.plugins.epgrefresh.save()
236
237                 if config.plugins.epgrefresh.enabled.value:
238                         epgrefresh.start()
239                 else:
240                         epgrefresh.stop()
241
242                 req.setResponseCode(http.OK)
243                 req.setHeader('Content-type', 'application/xhtml+xml')
244                 req.setHeader('charset', 'UTF-8')
245
246                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
247 <e2simplexmlresult>
248  <e2state>true</e2state>
249  <e2statetext>%s</e2statetext>
250 </e2simplexmlresult>""" % (statetext,)
251
252 class EPGRefreshSettingsResource(resource.Resource):
253         def render(self, req):
254                 req.setResponseCode(http.OK)
255                 req.setHeader('Content-type', 'application/xhtml+xml')
256                 req.setHeader('charset', 'UTF-8')
257
258                 from time import time, localtime, mktime
259                 now = localtime()
260                 begin_h = config.plugins.epgrefresh.begin.value
261                 begin = mktime((
262                         now.tm_year, now.tm_mon, now.tm_mday, begin_h[0], begin_h[1],
263                         0, now.tm_wday, now.tm_yday, now.tm_isdst)
264                 )
265                 end_h = config.plugins.epgrefresh.end.value
266                 end = mktime((
267                         now.tm_year, now.tm_mon, now.tm_mday, end_h[0], end_h[1],
268                         0, now.tm_wday, now.tm_yday, now.tm_isdst)
269                 )
270
271                 canDoBackgroundRefresh = SystemInfo.get("NumVideoDecoders", 1) > 1
272                 hasAutoTimer = False
273                 try:
274                         from Plugins.Extensions.AutoTimer.AutoTimer import AutoTimer
275                         hasAutoTimer = True
276                 except ImportError as ie: pass
277
278                 return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
279 <e2settings>
280  <e2setting>
281   <e2settingname>config.plugins.epgrefresh.enabled</e2settingname>
282   <e2settingvalue>%s</e2settingvalue>
283  </e2setting>
284  <e2setting>
285   <e2settingname>config.plugins.epgrefresh.enablemessage</e2settingname>
286   <e2settingvalue>%s</e2settingvalue>
287  </e2setting>
288  <e2setting>
289   <e2settingname>config.plugins.epgrefresh.begin</e2settingname>
290   <e2settingvalue>%d</e2settingvalue>
291  </e2setting>
292  <e2setting>
293   <e2settingname>config.plugins.epgrefresh.end</e2settingname>
294   <e2settingvalue>%d</e2settingvalue>
295  </e2setting>
296  <e2setting>
297   <e2settingname>config.plugins.epgrefresh.interval_seconds</e2settingname>
298   <e2settingvalue>%d</e2settingvalue>
299  </e2setting>
300  <e2setting>
301   <e2settingname>config.plugins.epgrefresh.delay_standby</e2settingname>
302   <e2settingvalue>%d</e2settingvalue>
303  </e2setting>
304  <e2setting>
305   <e2settingname>config.plugins.epgrefresh.inherit_autotimer</e2settingname>
306   <e2settingvalue>%s</e2settingvalue>
307  </e2setting>
308  <e2setting>
309   <e2settingname>config.plugins.epgrefresh.afterevent</e2settingname>
310   <e2settingvalue>%s</e2settingvalue>
311  </e2setting>
312  <e2setting>
313   <e2settingname>config.plugins.epgrefresh.force</e2settingname>
314   <e2settingvalue>%s</e2settingvalue>
315  </e2setting>
316  <e2setting>
317   <e2settingname>config.plugins.epgrefresh.wakeup</e2settingname>
318   <e2settingvalue>%s</e2settingvalue>
319  </e2setting>
320  <e2setting>
321   <e2settingname>config.plugins.epgrefresh.parse_autotimer</e2settingname>
322   <e2settingvalue>%s</e2settingvalue>
323  </e2setting>
324  <e2setting>
325   <e2settingname>config.plugins.epgrefresh.lastscan</e2settingname>
326   <e2settingvalue>%d</e2settingvalue>
327  </e2setting>
328  <e2setting>
329   <e2settingname>config.plugins.epgrefresh.adapter</e2settingname>
330   <e2settingvalue>%s</e2settingvalue>
331  </e2setting>
332  <e2setting>
333   <e2settingname>canDoBackgroundRefresh</e2settingname>
334   <e2settingvalue>%s</e2settingvalue>
335  </e2setting>
336  <e2setting>
337   <e2settingname>hasAutoTimer</e2settingname>
338   <e2settingvalue>%s</e2settingvalue>
339  </e2setting>
340  <e2setting>
341   <e2settingname>api_version</e2settingname>
342   <e2settingvalue>%s</e2settingvalue>
343  </e2setting>
344 </e2settings>""" % (
345                                 config.plugins.epgrefresh.enabled.value,
346                                 config.plugins.epgrefresh.enablemessage.value,
347                                 begin,
348                                 end,
349                                 config.plugins.epgrefresh.interval_seconds.value,
350                                 config.plugins.epgrefresh.delay_standby.value,
351                                 config.plugins.epgrefresh.inherit_autotimer.value,
352                                 config.plugins.epgrefresh.afterevent.value,
353                                 config.plugins.epgrefresh.force.value,
354                                 config.plugins.epgrefresh.wakeup.value,
355                                 config.plugins.epgrefresh.parse_autotimer.value,
356                                 config.plugins.epgrefresh.lastscan.value,
357                                 config.plugins.epgrefresh.adapter.value,
358                                 canDoBackgroundRefresh,
359                                 hasAutoTimer,
360                                 API_VERSION,
361                         )
362