autotimer: basic autotimer management through webif
[enigma2-plugins.git] / autotimer / src / AutoTimerResource.py
1 from twisted.web import http, resource
2 from AutoTimer import AutoTimer
3 from RecordTimer import AFTEREVENT
4 from urllib import unquote
5 from . import _
6
7 class AutoTimerBaseResource(resource.Resource):
8         _remove = False
9         def getAutoTimerInstance(self):
10                 from plugin import autotimer
11                 if autotimer is None:
12                         self._remove = True
13                         return AutoTimer()
14                 self._remove = False
15                 return autotimer
16         def returnResult(self, req, state, statetext):
17                 result = """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
18                         <e2simplexmlresult>
19                                 <e2state>%s</e2state>
20                                 <e2statetext>%s</e2statetext>
21                         </e2simplexmlresult>
22                         """ % ('true' if state else 'false', statetext)
23
24                 req.setResponseCode(http.OK)
25                 req.setHeader('Content-type', 'application; xhtml+xml')
26                 req.setHeader('charset', 'UTF-8')
27                 return result
28
29
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])
35
36                 if self._remove:
37                         autotimer.writeXml()
38
39                 return self.returnResult(req, True, output)
40
41 class AutoTimerListAutoTimerResource(AutoTimerBaseResource):
42         def render(self, req):
43                 autotimer = self.getAutoTimerInstance()
44
45                 # show xml
46                 req.setResponseCode(http.OK)
47                 req.setHeader('Content-type', 'application; xhtml+xml')
48                 req.setHeader('charset', 'UTF-8')
49                 return ''.join(autotimer.getXml())
50
51 class AutoTimerRemoveAutoTimerResource(AutoTimerBaseResource):
52         def render(self, req):
53                 autotimer = self.getAutoTimerInstance()
54
55                 id = req.args.get("id")
56                 if id:
57                         autotimer.remove(int(id[0]))
58                         if self._remove:
59                                 autotimer.writeXml()
60                         return self.returnResult(req, True, _("AutoTimer was removed"))
61                 else:
62                         return self.returnResult(req, False, _("missing parameter \"id\""))
63
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
72
73                 id = get("id")
74                 timer = None
75                 newTimer = True
76                 if id is None:
77                         timer = autotimer.defaultTimer.clone()
78                         timer.id = autotimer.getUniqueId()
79                 else:
80                         id = int(id)
81                         for possibleMatch in autotimer.getTimerList():
82                                 if possibleMatch.id == id:
83                                         timer = possibleMatch
84                                         newTimer = False
85                                         break
86                         if timer is None:
87                                 return self.returnResult(req, False, _("unable to find timer with id %i" % (id,)))
88
89                 # Match
90                 timer.match = unquote(get("match", timer.match))
91                 if not timer.match:
92                         return self.returnResult(req, False, _("autotimers need a match attribute"))
93
94                 # Name
95                 timer.name = unquote(get("name", timer.name)).strip()
96                 if not timer.name: timer.name = timer.match
97
98                 # Encoding
99                 timer.encoding = get("encoding", timer.encoding)
100
101                 # ...
102                 timer.searchType = get("searchType", timer.searchType)
103                 timer.searchCase = get("searchCase", timer.searchCase)
104
105                 # Alternatives
106                 timer.overrideAlternatives = int(get("overrideAlternatives", timer.overrideAlternatives))
107
108                 # Enabled
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
114
115                 # Justplay
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
121
122                 # Timespan
123                 start = get("timespanFrom")
124                 end = get("timespanTo")
125                 if start and end:
126                         start = [int(x) for x in start.split(':')]
127                         end = [int(x) for x in end.split(':')]
128                         timer.timespan = (start, end)
129
130                 # Services
131                 servicelist = get("services")
132                 if servicelist is not None:
133                         servicelist = unquote(servicelist).split(',')
134                         appendlist = []
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(':')
140                                         if pos != -1:
141                                                 if value[pos-1] == ':':
142                                                         pos -= 1
143                                                 value = value[:pos+1]
144
145                                 appendlist.append(value)
146                         timer.services = appendlist
147
148                 # Bouquets
149                 servicelist = get("bouquets")
150                 if servicelist is not None:
151                         servicelist = unquote(servicelist).split(',')
152                         timer.bouquets = servicelist
153
154                 # Offset
155                 offset = get("offset")
156                 if offset:
157                         offset = offset.split(',')
158                         if len(offset) == 1:
159                                 before = after = int(offset[0] or 0) * 60
160                         else:
161                                 before = int(offset[0] or 0) * 60
162                                 after = int(offset[1] or 0) * 60
163                         timer.offset = (before, after)
164
165                 # AfterEvent
166                 afterevent = get("afterevent")
167                 if afterevent:
168                         if afterevent == "default":
169                                 timer.afterevent = []
170                         else:
171                                 try: afterevent = int(afterevent)
172                                 except ValueError:
173                                         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)]
181
182                 # Maxduration
183                 maxduration = get("maxduration")
184                 if maxduration:
185                         timer.maxduration = int(maxduration)*60
186
187                 # TODO: implement in-/excludes, counter, tags
188
189                 timer.avoidDuplicateDescription = int(get("avoidDuplicateDescription", timer.avoidDuplicateDescription))
190                 timer.destination = get("location", "") or None
191
192                 # eventually save config
193                 if self._remove:
194                         autotimer.writeXml()
195
196                 if newTimer:
197                         autotimer.add(timer)
198                         message = _("AutoTimer was added successfully")
199                 else:
200                         message = _("AutoTimer was changed successfully")
201                 return self.returnResult(req, True, message)