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