cleanups, some more micro-optimizations
[vuplus_dvbapp-plugin] / autotimer / src / AutoTimer.py
1 # Plugins Config
2 from xml.etree.cElementTree import parse as cet_parse
3 from os import path as os_path
4 from AutoTimerConfiguration import parseConfig, writeConfig
5
6 # Navigation (RecordTimer)
7 import NavigationInstance
8
9 # Timer
10 from ServiceReference import ServiceReference
11 from RecordTimer import RecordTimerEntry
12 from Components.TimerSanityCheck import TimerSanityCheck
13
14 # Timespan
15 from time import localtime, time
16
17 # EPGCache & Event
18 from enigma import eEPGCache, eServiceReference
19
20 # Enigma2 Config
21 from Components.config import config
22
23 # AutoTimer Component
24 from AutoTimerComponent import AutoTimerComponent
25
26 XML_CONFIG = "/etc/enigma2/autotimer.xml"
27
28 def getTimeDiff(timer, begin, end):
29         if begin <= timer.begin <= end:
30                 return end - timer.begin
31         elif timer.begin <= begin <= timer.end:
32                 return timer.end - begin
33         return 0
34
35 typeMap = {
36         "exact": eEPGCache.EXAKT_TITLE_SEARCH,
37         "partial": eEPGCache.PARTIAL_TITLE_SEARCH
38 }
39
40 caseMap = {
41         "sensitive": eEPGCache.CASE_CHECK,
42         "insensitive": eEPGCache.NO_CASE_CHECK
43 }
44
45 class AutoTimerIgnoreTimerException(Exception):
46         def __init__(self, cause):
47                 self.cause = cause
48
49         def __str__(self):
50                 return "[AutoTimer] " + str(self.cause)
51
52         def __repr__(self):
53                 return str(type(self))
54
55 class AutoTimer:
56         """Read and save xml configuration, query EPGCache"""
57
58         def __init__(self):
59                 # Initialize
60                 self.timers = []
61                 self.configMtime = -1
62                 self.uniqueTimerId = 0
63                 self.defaultTimer = AutoTimerComponent(
64                         0,              # Id
65                         "",             # Name
66                         "",             # Match
67                         True    # Enabled
68                 )
69
70 # Configuration
71
72         def readXml(self):
73                 # Abort if no config found
74                 if not os_path.exists(XML_CONFIG):
75                         print "[AutoTimer] No configuration file present"
76                         return
77
78                 # Parse if mtime differs from whats saved
79                 mtime = os_path.getmtime(XML_CONFIG)
80                 if mtime == self.configMtime:
81                         print "[AutoTimer] No changes in configuration, won't parse"
82                         return
83
84                 # Save current mtime
85                 self.configMtime = mtime
86
87                 # Parse Config
88                 configuration = cet_parse(XML_CONFIG).getroot()
89
90                 # Empty out timers and reset Ids
91                 del self.timers[:]
92                 self.defaultTimer.clear(-1, True)
93
94                 parseConfig(
95                         configuration,
96                         self.timers,
97                         configuration.get("version"),
98                         0,
99                         self.defaultTimer
100                 )
101                 self.uniqueTimerId = len(self.timers)
102
103         def writeXml(self):
104                 writeConfig(XML_CONFIG, self.defaultTimer, self.timers)
105
106 # Manage List
107
108         def add(self, timer):
109                 self.timers.append(timer)
110
111         def getEnabledTimerList(self):
112                 return [x for x in self.timers if x.enabled]
113
114         def getTimerList(self):
115                 return self.timers
116
117         def getTupleTimerList(self):
118                 list = self.timers
119                 return [(x,) for x in list]
120
121         def getSortedTupleTimerList(self):
122                 list = self.timers[:]
123                 list.sort()
124                 return [(x,) for x in list]
125
126         def getUniqueId(self):
127                 self.uniqueTimerId += 1
128                 return self.uniqueTimerId
129
130         def remove(self, uniqueId):
131                 idx = 0
132                 for timer in self.timers:
133                         if timer.id == uniqueId:
134                                 self.timers.pop(idx)
135                                 return
136                         idx += 1
137
138         def set(self, timer):
139                 idx = 0
140                 for stimer in self.timers:
141                         if stimer == timer:
142                                 self.timers[idx] = timer
143                                 return
144                         idx += 1
145                 self.timers.append(timer)
146
147 # Main function
148
149         def parseEPG(self, simulateOnly = False):
150                 if NavigationInstance.instance is None:
151                         print "[AutoTimer] Navigation is not available, can't parse EPG"
152                         return (0, 0, 0, [])
153
154                 total = 0
155                 new = 0
156                 modified = 0
157                 timers = []
158
159                 self.readXml()
160
161                 # Save Recordings in a dict to speed things up a little
162                 # We include processed timers as we might search for duplicate descriptions
163                 recorddict = {}
164                 for timer in NavigationInstance.instance.RecordTimer.timer_list + NavigationInstance.instance.RecordTimer.processed_timers:
165                         if not recorddict.has_key(str(timer.service_ref)):
166                                 recorddict[str(timer.service_ref)] = [timer]
167                         else:
168                                 recorddict[str(timer.service_ref)].append(timer)
169
170                 # Iterate Timer
171                 for timer in self.getEnabledTimerList():
172                         # Workaround to allow search for umlauts if we know the encoding
173                         match = timer.match
174                         if timer.encoding != 'UTF-8':
175                                 try:
176                                         match = match.decode('UTF-8').encode(timer.encoding)
177                                 except UnicodeDecodeError:
178                                         pass
179
180                         # Search EPG, default to empty list
181                         epgcache = eEPGCache.getInstance()
182                         ret = epgcache.search(('RI', 100, typeMap[timer.searchType], match, caseMap[timer.searchCase])) or []
183
184                         for serviceref, eit in ret:
185                                 eserviceref = eServiceReference(serviceref)
186
187                                 evt = epgcache.lookupEventId(eserviceref, eit)
188                                 if not evt:
189                                         print "[AutoTimer] Could not create Event!"
190                                         continue
191
192                                 # Try to determine real service (we always choose the last one)
193                                 n = evt.getNumOfLinkageServices()
194                                 if n > 0:
195                                         i = evt.getLinkageService(eserviceref, n-1)
196                                         serviceref = i.toString()
197
198                                 # Gather Information
199                                 name = evt.getEventName()
200                                 description = evt.getShortDescription()
201                                 begin = evt.getBeginTime()
202                                 duration = evt.getDuration()
203                                 end = begin + duration
204
205                                 # If event starts in less than 60 seconds skip it
206                                 if begin < time() + 60:
207                                         continue
208
209                                 # Convert begin time
210                                 timestamp = localtime(begin)
211
212                                 # Update timer
213                                 timer.update(begin, timestamp)
214
215                                 # Check Duration, Timespan and Excludes
216                                 if timer.checkServices(serviceref) \
217                                         or timer.checkDuration(duration) \
218                                         or timer.checkTimespan(timestamp) \
219                                         or timer.checkFilter(name, description,
220                                                 evt.getExtendedDescription(), str(timestamp.tm_wday)):
221                                         continue
222
223                                 if timer.hasOffset():
224                                         # Apply custom Offset
225                                         begin, end = timer.applyOffset(begin, end)
226                                 else:
227                                         # Apply E2 Offset
228                                         begin -= config.recording.margin_before.value * 60
229                                         end += config.recording.margin_after.value * 60
230
231
232                                 total += 1
233
234                                 # Append to timerlist and abort if simulating
235                                 timers.append((name, begin, end, serviceref, timer.name))
236                                 if simulateOnly:
237                                         continue
238
239                                 # Initialize
240                                 newEntry = None
241                                 oldExists = False
242
243                                 # Check for double Timers
244                                 # We first check eit and if user wants us to guess event based on time
245                                 # we try this as backup. The allowed diff should be configurable though.
246                                 for rtimer in recorddict.get(serviceref, []):
247                                         if rtimer.eit == eit or config.plugins.autotimer.try_guessing.value and getTimeDiff(rtimer, begin, end) > ((duration/10)*8):
248                                                 oldExists = True
249
250                                                 # Abort if we don't want to modify timers or timer is repeated
251                                                 if config.plugins.autotimer.refresh.value == "none" or rtimer.repeated:
252                                                         print "[AutoTimer] Won't modify existing timer because either no modification allowed or repeated timer"
253                                                         break
254
255                                                 if hasattr(rtimer, "isAutoTimer"):
256                                                                 print "[AutoTimer] Modifying existing AutoTimer!"
257                                                 else:
258                                                         if config.plugins.autotimer.refresh.value != "all":
259                                                                 print "[AutoTimer] Won't modify existing timer because it's no timer set by us"
260                                                                 break
261
262                                                         print "[AutoTimer] Warning, we're messing with a timer which might not have been set by us"
263
264                                                 newEntry = rtimer
265                                                 modified += 1
266
267                                                 # Modify values saved in timer
268                                                 newEntry.name = name
269                                                 newEntry.description = description
270                                                 newEntry.begin = int(begin)
271                                                 newEntry.end = int(end)
272                                                 newEntry.service_ref = ServiceReference(serviceref)
273
274                                                 break
275                                         elif timer.getAvoidDuplicateDescription() == 1 and rtimer.description == description:
276                                                 oldExists = True
277                                                 print "[AutoTimer] We found a timer with same description, skipping event"
278                                                 break
279
280                                 # We found no timer we want to edit
281                                 if newEntry is None:
282                                         # But there is a match
283                                         if oldExists:
284                                                 continue
285
286                                         # We want to search for possible doubles
287                                         if timer.getAvoidDuplicateDescription() == 2:
288                                                 # I thinks thats the fastest way to do this, though it's a little ugly
289                                                 try:
290                                                         for list in recorddict.values():
291                                                                 for rtimer in list:
292                                                                         if rtimer.description == description:
293                                                                                 raise AutoTimerIgnoreTimerException("We found a timer with same description, skipping event")
294                                                 except AutoTimerIgnoreTimerException, etite:
295                                                         print etite
296                                                         continue
297
298                                         if timer.checkCounter(timestamp):
299                                                 continue
300
301                                         print "[AutoTimer] Adding an event."
302                                         newEntry = RecordTimerEntry(ServiceReference(serviceref), begin, end, name, description, eit)
303
304                                         # Mark this entry as AutoTimer (only AutoTimers will have this Attribute set)
305                                         newEntry.isAutoTimer = True
306
307                                 # Apply afterEvent
308                                 if timer.hasAfterEvent():
309                                         afterEvent = timer.getAfterEventTimespan(localtime(end))
310                                         if afterEvent is None:
311                                                 afterEvent = timer.getAfterEvent()
312                                         if afterEvent is not None:
313                                                 newEntry.afterEvent = afterEvent
314
315                                 newEntry.dirname = timer.destination
316                                 newEntry.justplay = timer.justplay
317                                 newEntry.tags = timer.tags
318
319                                 if oldExists:
320                                         # XXX: this won't perform a sanity check, but do we actually want to do so?
321                                         NavigationInstance.instance.RecordTimer.timeChanged(newEntry)
322                                 else:
323                                         conflicts = NavigationInstance.instance.RecordTimer.record(newEntry)
324                                         if conflicts and config.plugins.autotimer.disabled_on_conflict.value:
325                                                 newEntry.disabled = True
326                                                 # We might want to do the sanity check locally so we don't run it twice - but I consider this workaround a hack anyway
327                                                 conflicts = NavigationInstance.instance.RecordTimer.record(newEntry)
328                                         if conflicts is None:
329                                                 timer.decrementCounter()
330                                                 new += 1
331                                                 if recorddict.has_key(serviceref):
332                                                         recorddict[serviceref].append(newEntry)
333                                                 else:
334                                                         recorddict[serviceref] = [newEntry]
335
336                 return (total, new, modified, timers)
337