1 from twisted.web import http, resource
2 from AutoTimer import AutoTimer
3 from RecordTimer import AFTEREVENT
4 from urllib import unquote
7 class AutoTimerBaseResource(resource.Resource):
9 def getAutoTimerInstance(self):
10 from plugin import autotimer
16 def returnResult(self, req, state, statetext):
17 result = """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
20 <e2statetext>%s</e2statetext>
22 """ % ('true' if state else 'false', statetext)
24 req.setResponseCode(http.OK)
25 req.setHeader('Content-type', 'application; xhtml+xml')
26 req.setHeader('charset', 'UTF-8')
30 class AutoTimerDoParseResource(AutoTimerBaseResource):
31 def render(self, req):
32 autotimer = self.getAutoTimerInstance()
33 ret = autotimer.parseEPG()
34 output = _("Found a total of %d matching Events.\n%d Timer were added and %d modified.") % (ret[0], ret[1], ret[2])
39 return self.returnResult(req, True, output)
41 class AutoTimerListAutoTimerResource(AutoTimerBaseResource):
42 def render(self, req):
43 autotimer = self.getAutoTimerInstance()
46 req.setResponseCode(http.OK)
47 req.setHeader('Content-type', 'application; xhtml+xml')
48 req.setHeader('charset', 'UTF-8')
49 return ''.join(autotimer.getXml())
51 class AutoTimerRemoveAutoTimerResource(AutoTimerBaseResource):
52 def render(self, req):
53 autotimer = self.getAutoTimerInstance()
55 id = req.args.get("id")
57 autotimer.remove(int(id[0]))
60 return self.returnResult(req, True, _("AutoTimer was removed"))
62 return self.returnResult(req, False, _("missing parameter \"id\""))
64 class AutoTimerAddOrEditAutoTimerResource(AutoTimerBaseResource):
65 # TODO: recheck if we can modify regular config parser to work on this
66 # TODO: allow to edit defaults?
67 def render(self, req):
68 autotimer = self.getAutoTimerInstance()
69 def get(name, default=None):
70 ret = req.args.get(name)
71 return ret[0] if ret else default
77 timer = autotimer.defaultTimer.clone()
78 timer.id = autotimer.getUniqueId()
81 for possibleMatch in autotimer.getTimerList():
82 if possibleMatch.id == id:
87 return self.returnResult(req, False, _("unable to find timer with id %i" % (id,)))
90 timer.match = unquote(get("match", timer.match))
92 return self.returnResult(req, False, _("autotimers need a match attribute"))
95 timer.name = unquote(get("name", timer.name)).strip()
96 if not timer.name: timer.name = timer.match
99 timer.encoding = get("encoding", timer.encoding)
102 timer.searchType = get("searchType", timer.searchType)
103 timer.searchCase = get("searchCase", timer.searchCase)
106 timer.overrideAlternatives = int(get("overrideAlternatives", timer.overrideAlternatives))
109 enabled = get("enabled")
110 if enabled is not None:
111 try: enabled = int(enabled)
112 except ValueError: enabled = enabled == "yes"
113 timer.enabled = enabled
116 justplay = get("justplay")
117 if justplay is not None:
118 try: justplay = int(justplay)
119 except ValueError: justplay = justplay == "zap"
120 timer.justplay = justplay
123 start = get("timespanFrom")
124 end = get("timespanTo")
126 start = [int(x) for x in start.split(':')]
127 end = [int(x) for x in end.split(':')]
128 timer.timespan = (start, end)
131 servicelist = get("services")
132 if servicelist is not None:
133 servicelist = unquote(servicelist).split(',')
135 for value in servicelist:
136 myref = eServiceReference(str(value))
137 if not (myref.flags & eServiceReference.isGroup):
138 # strip all after last :
139 pos = value.rfind(':')
141 if value[pos-1] == ':':
143 value = value[:pos+1]
145 appendlist.append(value)
146 timer.services = appendlist
149 servicelist = get("bouquets")
150 if servicelist is not None:
151 servicelist = unquote(servicelist).split(',')
152 timer.bouquets = servicelist
155 offset = get("offset")
157 offset = offset.split(',')
159 before = after = int(offset[0] or 0) * 60
161 before = int(offset[0] or 0) * 60
162 after = int(offset[1] or 0) * 60
163 timer.offset = (before, after)
166 afterevent = get("afterevent")
168 if afterevent == "default":
169 timer.afterevent = []
171 try: afterevent = int(afterevent)
174 "nothing": AFTEREVENT.NONE,
175 "deepstandby": AFTEREVENT.DEEPSTANDBY,
176 "standby": AFTEREVENT.STANDBY,
177 "auto": AFTEREVENT.AUTO
178 }.get(afterevent, AFTEREVENT.AUTO)
179 # TODO: add afterevent timespan
180 timer.afterevent = [(afterevent, None)]
183 maxduration = get("maxduration")
185 timer.maxduration = int(maxduration)*60
187 # TODO: implement in-/excludes, counter, tags
189 timer.avoidDuplicateDescription = int(get("avoidDuplicateDescription", timer.avoidDuplicateDescription))
190 timer.destination = get("location", "") or None
192 # eventually save config
198 message = _("AutoTimer was added successfully")
200 message = _("AutoTimer was changed successfully")
201 return self.returnResult(req, True, message)