/
AutoTimerResource.py
514 lines (457 loc) · 18.3 KB
/
AutoTimerResource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# -*- coding: UTF-8 -*-
from AutoTimer import AutoTimer
from AutoTimerConfiguration import CURRENT_CONFIG_VERSION
from RecordTimer import AFTEREVENT
from twisted.internet import reactor
from twisted.web import http, resource, server
import threading
try:
from urllib import unquote
except ImportError as ie:
from urllib.parse import unquote
from ServiceReference import ServiceReference
from Tools.XMLTools import stringToXML
from enigma import eServiceReference
from . import _, config, iteritems, plugin
from plugin import autotimer, AUTOTIMER_VERSION
from AutoTimerSettings import getAutoTimerSettingsDefinitions
API_VERSION = "1.5"
class AutoTimerBaseResource(resource.Resource):
def returnResult(self, req, state, statetext):
req.setResponseCode(http.OK)
req.setHeader('Content-type', 'application/xhtml+xml')
req.setHeader('charset', 'UTF-8')
return """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<e2simplexmlresult>
<e2state>%s</e2state>
<e2statetext>%s</e2statetext>
</e2simplexmlresult>""" % ('True' if state else 'False', statetext)
class AutoTimerDoParseResource(AutoTimerBaseResource):
def parsecallback(self, ret):
rets = self.renderBackground (self.req, ret)
self.req.write(rets)
self.req.finish()
def render(self, req):
self.req = req
# todo timeout / error handling
autotimer.parseEPG(callback = self.parsecallback)
return server.NOT_DONE_YET
def renderBackground(self, req ,ret):
output = _("Found a total of %d matching Events.\n%d Timer were added and\n%d modified,\n%d conflicts encountered,\n%d unchanged,\n%d similars added.") % (ret[0], ret[1], ret[2], len(ret[4]), len(ret[6]), len(ret[5]))
return self.returnResult(req, True, output)
class AutoTimerSimulateResource(AutoTimerBaseResource):
def parsecallback(self, timers, skipped):
ret = self.renderBackground (self.req, timers)
self.req.write(ret)
self.req.finish()
def render(self, req):
self.req = req
# todo timeout / error handling
autotimer.parseEPG(simulateOnly=True, callback = self.parsecallback)
return server.NOT_DONE_YET
def renderBackground(self, req ,timers):
returnlist = ["<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<e2autotimersimulate api_version=\"", str(API_VERSION), "\">\n"]
extend = returnlist.extend
for (name, begin, end, serviceref, autotimername) in timers:
ref = ServiceReference(str(serviceref))
extend((
'<e2simulatedtimer>\n'
' <e2servicereference>', stringToXML(serviceref), '</e2servicereference>\n',
' <e2servicename>', stringToXML(ref.getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '')), '</e2servicename>\n',
' <e2name>', stringToXML(name), '</e2name>\n',
' <e2timebegin>', str(begin), '</e2timebegin>\n',
' <e2timeend>', str(end), '</e2timeend>\n',
' <e2autotimername>', stringToXML(autotimername), '</e2autotimername>\n'
'</e2simulatedtimer>\n'
))
returnlist.append('</e2autotimersimulate>')
req.setResponseCode(http.OK)
req.setHeader('Content-type', 'application/xhtml+xml')
req.setHeader('charset', 'UTF-8')
return ''.join(returnlist)
#TODO
class AutoTimerTestResource(AutoTimerBaseResource):
def parsecallback(self, timers, skipped):
ret = self.renderBackground (self.req, timers, skipped)
self.req.write(ret)
self.req.finish()
def render(self, req):
self.req = req
# todo timeout / error handling
autotimer.parseEPG(simulateOnly=True, callback = self.parsecallback)
return server.NOT_DONE_YET
def renderBackground(self, req ,timers, skipped):
returnlist = ["<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<e2autotimertest api_version=\"", str(API_VERSION), "\">\n"]
extend = returnlist.extend
for (name, begin, end, serviceref, autotimername) in timers:
ref = ServiceReference(str(serviceref))
extend((
'<e2testtimer>\n'
' <e2servicereference>', stringToXML(serviceref), '</e2servicereference>\n',
' <e2servicename>', stringToXML(ref.getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '')), '</e2servicename>\n',
' <e2name>', stringToXML(name), '</e2name>\n',
' <e2timebegin>', str(begin), '</e2timebegin>\n',
' <e2timeend>', str(end), '</e2timeend>\n',
' <e2autotimername>', stringToXML(autotimername), '</e2autotimername>\n',
' <e2state>OK</e2state>\n'
'</e2testtimer>\n'
))
if skipped:
for (name, begin, end, serviceref, autotimername, message) in skipped:
ref = ServiceReference(str(serviceref))
extend((
'<e2testtimer>\n'
' <e2servicereference>', stringToXML(serviceref), '</e2servicereference>\n',
' <e2servicename>', stringToXML(ref.getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '')), '</e2servicename>\n',
' <e2name>', stringToXML(name), '</e2name>\n',
' <e2timebegin>', str(begin), '</e2timebegin>\n',
' <e2timeend>', str(end), '</e2timeend>\n',
' <e2autotimername>', stringToXML(autotimername), '</e2autotimername>\n',
' <e2state>Skip</e2state>\n'
' <e2message>', stringToXML(message), '</e2message>\n'
'</e2testtimer>\n'
))
returnlist.append('</e2autotimertest>')
req.setResponseCode(http.OK)
req.setHeader('Content-type', 'application/xhtml+xml')
req.setHeader('charset', 'UTF-8')
return ''.join(returnlist)
class AutoTimerListAutoTimerResource(AutoTimerBaseResource):
def render(self, req):
# We re-read the config so we won't display empty or wrong information
try:
autotimer.readXml()
except Exception as e:
return self.returnResult(req, False, _("Couldn't load config file!") + '\n' + str(e))
# show xml
req.setResponseCode(http.OK)
req.setHeader('Content-type', 'application/xhtml+xml')
req.setHeader('charset', 'UTF-8')
return ''.join(autotimer.getXml())
class AutoTimerRemoveAutoTimerResource(AutoTimerBaseResource):
def render(self, req):
id = req.args.get("id")
if id:
autotimer.remove(int(id[0]))
if config.plugins.autotimer.always_write_config.value:
autotimer.writeXml()
return self.returnResult(req, True, _("AutoTimer was removed"))
else:
return self.returnResult(req, False, _("missing parameter \"id\""))
class AutoTimerAddOrEditAutoTimerResource(AutoTimerBaseResource):
# TODO: recheck if we can modify regular config parser to work on this
# TODO: allow to edit defaults?
def render(self, req):
def get(name, default=None):
ret = req.args.get(name)
return ret[0] if ret else default
id = get("id")
timer = None
newTimer = True
if id is None:
id = autotimer.getUniqueId()
timer = autotimer.defaultTimer.clone()
timer.id = id
else:
id = int(id)
for possibleMatch in autotimer.getTimerList():
if possibleMatch.id == id:
timer = possibleMatch
newTimer = False
break
if timer is None:
return self.returnResult(req, False, _("unable to find timer with id %i" % (id,)))
if id != -1:
# Match
timer.match = unquote(get("match", timer.match))
if not timer.match:
return self.returnResult(req, False, _("autotimers need a match attribute"))
# Name
timer.name = unquote(get("name", timer.name)).strip()
if not timer.name: timer.name = timer.match
# Enabled
enabled = get("enabled")
if enabled is not None:
try: enabled = int(enabled)
except ValueError: enabled = enabled == "yes"
timer.enabled = enabled
# Timeframe
before = get("before")
after = get("after")
if before and after:
timer.timeframe = (int(after), int(before))
elif before == '' or after == '':
timer.timeframe = None
# Encoding
timer.encoding = get("encoding", timer.encoding)
# ...
timer.searchType = get("searchType", timer.searchType)
timer.searchCase = get("searchCase", timer.searchCase)
# Alternatives
timer.overrideAlternatives = int(get("overrideAlternatives", timer.overrideAlternatives))
# Justplay
justplay = get("justplay")
if justplay is not None:
try: justplay = int(justplay)
except ValueError: justplay = justplay == "zap"
timer.justplay = justplay
setEndtime = get("setEndtime")
if setEndtime is not None:
timer.setEndtime = int(setEndtime)
# Timespan
start = get("timespanFrom")
end = get("timespanTo")
if start and end:
start = [int(x) for x in start.split(':')]
end = [int(x) for x in end.split(':')]
timer.timespan = (start, end)
elif start == '' and end == '':
timer.timespan = None
# Services
servicelist = get("services")
if servicelist is not None:
servicelist = unquote(servicelist).split(',')
appendlist = []
for value in servicelist:
myref = eServiceReference(str(value))
if not (myref.flags & eServiceReference.isGroup):
# strip all after last :
pos = value.rfind(':')
if pos != -1:
if value[pos-1] == ':':
pos -= 1
value = value[:pos+1]
if myref.valid():
appendlist.append(value)
timer.services = appendlist
# Bouquets
servicelist = get("bouquets")
if servicelist is not None:
servicelist = unquote(servicelist).split(',')
while '' in servicelist: servicelist.remove('')
timer.bouquets = servicelist
# Offset
offset = get("offset")
if offset:
offset = offset.split(',')
if len(offset) == 1:
before = after = int(offset[0] or 0) * 60
else:
before = int(offset[0] or 0) * 60
after = int(offset[1] or 0) * 60
timer.offset = (before, after)
elif offset == '':
timer.offset = None
# AfterEvent
afterevent = get("afterevent")
if afterevent:
if afterevent == "default":
timer.afterevent = []
else:
try: afterevent = int(afterevent)
except ValueError:
afterevent = {
"nothing": AFTEREVENT.NONE,
"deepstandby": AFTEREVENT.DEEPSTANDBY,
"standby": AFTEREVENT.STANDBY,
"auto": AFTEREVENT.AUTO
}.get(afterevent, AFTEREVENT.AUTO)
start = get("aftereventFrom")
end = get("aftereventTo")
if start and end:
start = [int(x) for x in start.split(':')]
end = [int(x) for x in end.split(':')]
timer.afterevent = [(afterevent, (start, end))]
else:
timer.afterevent = [(afterevent, None)]
# Maxduration
maxduration = get("maxduration")
if maxduration:
timer.maxduration = int(maxduration)*60
elif maxduration == '':
timer.maxduration = None
# Includes
title = req.args.get("title")
shortdescription = req.args.get("shortdescription")
description = req.args.get("description")
dayofweek = req.args.get("dayofweek")
if title or shortdescription or description or dayofweek:
includes = timer.include
title = [unquote(x) for x in title] if title else includes[0]
shortdescription = [unquote(x) for x in shortdescription] if shortdescription else includes[1]
description = [unquote(x) for x in description] if description else includes[2]
dayofweek = [unquote(x) for x in dayofweek] if dayofweek else includes[3]
while '' in title: title.remove('')
while '' in shortdescription: shortdescription.remove('')
while '' in description: description.remove('')
while '' in dayofweek: dayofweek.remove('')
timer.include = (title, shortdescription, description, dayofweek)
# Excludes
title = req.args.get("!title")
shortdescription = req.args.get("!shortdescription")
description = req.args.get("!description")
dayofweek = req.args.get("!dayofweek")
if title or shortdescription or description or dayofweek:
excludes = timer.exclude
title = [unquote(x) for x in title] if title else excludes[0]
shortdescription = [unquote(x) for x in shortdescription] if shortdescription else excludes[1]
description = [unquote(x) for x in description] if description else excludes[2]
dayofweek = [unquote(x) for x in dayofweek] if dayofweek else excludes[3]
while '' in title: title.remove('')
while '' in shortdescription: shortdescription.remove('')
while '' in description: description.remove('')
while '' in dayofweek: dayofweek.remove('')
timer.exclude = (title, shortdescription, description, dayofweek)
tags = req.args.get("tag")
if tags:
while '' in tags: tags.remove('')
timer.tags = [unquote(x) for x in tags]
timer.matchCount = int(get("counter", timer.matchCount))
timer.matchFormatString = get("counterFormat", timer.matchFormatString)
if id != -1:
matchLeft = get("left")
timer.matchLeft = int(matchLeft) if matchLeft else (timer.matchCount if newTimer else timer.matchLeft)
timer.matchLimit = get("lastActivation", timer.matchLimit)
timer.lastBegin = int(get("lastBegin", timer.lastBegin))
timer.avoidDuplicateDescription = int(get("avoidDuplicateDescription", timer.avoidDuplicateDescription))
timer.searchForDuplicateDescription = int(get("searchForDuplicateDescription", timer.searchForDuplicateDescription))
timer.destination = get("location", timer.destination) or None
# vps
enabled = get("vps_enabled")
if enabled is not None:
try: enabled = int(enabled)
except ValueError: enabled = enabled == "yes"
timer.vps_enabled = enabled
vps_overwrite = get("vps_overwrite")
if vps_overwrite is not None:
try: vps_overwrite = int(vps_overwrite)
except ValueError: vps_overwrite = vps_overwrite == "yes"
timer.vps_overwrite = vps_overwrite
if not timer.vps_enabled and timer.vps_overwrite:
timer.vps_overwrite = False
# SeriesPlugin
series_labeling = get("series_labeling")
if series_labeling is not None:
try: series_labeling = int(series_labeling)
except ValueError: series_labeling = series_labeling == "yes"
timer.series_labeling = series_labeling
# Always zap
timer.always_zap = get("always_zap", timer.always_zap)
if newTimer:
autotimer.add(timer)
message = _("AutoTimer was added successfully")
else:
message = _("AutoTimer was changed successfully")
if config.plugins.autotimer.always_write_config.value:
autotimer.writeXml()
return self.returnResult(req, True, message)
class AutoTimerChangeSettingsResource(AutoTimerBaseResource):
def render(self, req):
for key, value in iteritems(req.args):
value = value[0]
if key == "autopoll":
config.plugins.autotimer.autopoll.value = True if value == "true" else False
elif key == "interval":
config.plugins.autotimer.interval.value = int(value)
elif key == "refresh":
config.plugins.autotimer.refresh.value = value
elif key == "try_guessing":
config.plugins.autotimer.try_guessing.value = True if value == "true" else False
elif key == "editor":
config.plugins.autotimer.editor.value = value
elif key == "disabled_on_conflict":
config.plugins.autotimer.disabled_on_conflict.value = True if value == "true" else False
elif key == "addsimilar_on_conflict":
config.plugins.autotimer.addsimilar_on_conflict.value = True if value == "true" else False
elif key == "show_in_plugins":
config.plugins.autotimer.show_in_plugins.value = True if value == "true" else False
elif key == "show_in_extensionsmenu":
config.plugins.autotimer.show_in_extensionsmenu.value = True if value == "true" else False
elif key == "fastscan":
config.plugins.autotimer.fastscan.value = True if value == "true" else False
elif key == "notifconflict":
config.plugins.autotimer.notifconflict.value = True if value == "true" else False
elif key == "notifsimilar":
config.plugins.autotimer.notifsimilar.value = True if value == "true" else False
elif key == "maxdaysinfuture":
config.plugins.autotimer.maxdaysinfuture.value = int(value)
elif key == "add_autotimer_to_tags":
config.plugins.autotimer.add_autotimer_to_tags.value = True if value == "true" else False
elif key == "add_name_to_tags":
config.plugins.autotimer.add_name_to_tags.value = True if value == "true" else False
elif key == "timeout":
config.plugins.autotimer.timeout.value = int(value)
elif key == "popup_timeout":
config.plugins.autotimer.popup_timeout.value = int(value)
elif key == "delay":
config.plugins.autotimer.delay.value = int(value)
elif key == "editdelay":
config.plugins.autotimer.editdelay.value = int(value)
elif key == "skip_during_records":
config.plugins.autotimer.skip_during_records.value = True if value == "true" else False
elif key == "skip_during_epgrefresh":
config.plugins.autotimer.skip_during_epgrefresh.value = True if value == "true" else False
elif key == "check_eit_and_remove":
config.plugins.autotimer.check_eit_and_remove.value = True if value == "true" else False
elif key == "always_write_config":
config.plugins.autotimer.always_write_config.value,
elif key == "onlyinstandby":
config.plugins.autotimer.onlyinstandby.value = True if value == "true" else False
if config.plugins.autotimer.autopoll.value:
if plugin.autopoller is None:
from AutoPoller import AutoPoller
plugin.autopoller = AutoPoller()
plugin.autopoller.start()
else:
if plugin.autopoller is not None:
plugin.autopoller.stop()
plugin.autopoller = None
return self.returnResult(req, True, _("config changed."))
class AutoTimerSettingsResource(resource.Resource):
def render(self, req):
req.setResponseCode(http.OK)
req.setHeader('Content-type', 'application/xhtml+xml')
req.setHeader('charset', 'UTF-8')
try:
from Plugins.SystemPlugins.vps import Vps
except ImportError as ie:
hasVps = False
else:
hasVps = True
try:
from Plugins.Extensions.SeriesPlugin.plugin import Plugins
except ImportError as ie:
hasSeriesPlugin = False
else:
hasSeriesPlugin = True
defs = getAutoTimerSettingsDefinitions()
resultstr = """<?xml version=\"1.0\" encoding=\"UTF-8\" ?><e2settings>"""
for (title,cfg,key,description) in defs:
resultstr += """<e2setting>
<e2settingname>config.plugins.autotimer.%s</e2settingname>
<e2settingvalue>%s</e2settingvalue>
<e2settingtitle>%s</e2settingtitle>
<e2settingdescription>%s</e2settingdescription>
</e2setting>""" % (key, cfg.value , title , description )
resultstr += """<e2setting>
<e2settingname>hasVps</e2settingname>
<e2settingvalue>%s</e2settingvalue>
</e2setting>
<e2setting>
<e2settingname>hasSeriesPlugin</e2settingname>
<e2settingvalue>%s</e2settingvalue>
</e2setting>
<e2setting>
<e2settingname>version</e2settingname>
<e2settingvalue>%s</e2settingvalue>
</e2setting>
<e2setting>
<e2settingname>api_version</e2settingname>
<e2settingvalue>%s</e2settingvalue>
</e2setting>
<e2setting>
<e2settingname>autotimer_version</e2settingname>
<e2settingvalue>%s</e2settingvalue>
</e2setting>
</e2settings>""" % (hasVps, hasSeriesPlugin, CURRENT_CONFIG_VERSION, API_VERSION, AUTOTIMER_VERSION )
return resultstr