Bugfix:
[enigma2-plugins.git] / 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, buildConfig
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, strftime, time, mktime
16 from datetime import timedelta, date
17
18 # EPGCache & Event
19 from enigma import eEPGCache, eServiceReference, eServiceCenter, iServiceInformation
20
21 # Enigma2 Config
22 from Components.config import config
23
24 # AutoTimer Component
25 from AutoTimerComponent import preferredAutoTimerComponent
26
27 from itertools import chain
28 from collections import defaultdict
29
30 XML_CONFIG = "/etc/enigma2/autotimer.xml"
31
32 def getTimeDiff(timer, begin, end):
33         if begin <= timer.begin <= end:
34                 return end - timer.begin
35         elif timer.begin <= begin <= timer.end:
36                 return timer.end - begin
37         return 0
38
39 typeMap = {
40         "exact": eEPGCache.EXAKT_TITLE_SEARCH,
41         "partial": eEPGCache.PARTIAL_TITLE_SEARCH
42 }
43
44 caseMap = {
45         "sensitive": eEPGCache.CASE_CHECK,
46         "insensitive": eEPGCache.NO_CASE_CHECK
47 }
48
49 class AutoTimer:
50         """Read and save xml configuration, query EPGCache"""
51
52         def __init__(self):
53                 # Initialize
54                 self.timers = []
55                 self.configMtime = -1
56                 self.uniqueTimerId = 0
57                 self.defaultTimer = preferredAutoTimerComponent(
58                         0,              # Id
59                         "",             # Name
60                         "",             # Match
61                         True    # Enabled
62                 )
63
64 # Configuration
65
66         def readXml(self):
67                 # Abort if no config found
68                 if not os_path.exists(XML_CONFIG):
69                         print "[AutoTimer] No configuration file present"
70                         return
71
72                 # Parse if mtime differs from whats saved
73                 mtime = os_path.getmtime(XML_CONFIG)
74                 if mtime == self.configMtime:
75                         print "[AutoTimer] No changes in configuration, won't parse"
76                         return
77
78                 # Save current mtime
79                 self.configMtime = mtime
80
81                 # Parse Config
82                 configuration = cet_parse(XML_CONFIG).getroot()
83
84                 # Empty out timers and reset Ids
85                 del self.timers[:]
86                 self.defaultTimer.clear(-1, True)
87
88                 parseConfig(
89                         configuration,
90                         self.timers,
91                         configuration.get("version"),
92                         0,
93                         self.defaultTimer
94                 )
95                 self.uniqueTimerId = len(self.timers)
96
97         def getXml(self):
98                 return buildConfig(self.defaultTimer, self.timers, webif = True)
99
100         def writeXml(self):
101                 file = open(XML_CONFIG, 'w')
102                 file.writelines(buildConfig(self.defaultTimer, self.timers))
103                 file.close()
104
105 # Manage List
106
107         def add(self, timer):
108                 self.timers.append(timer)
109
110         def getEnabledTimerList(self):
111                 return (x for x in self.timers if x.enabled)
112
113         def getTimerList(self):
114                 return self.timers
115
116         def getTupleTimerList(self):
117                 lst = self.timers
118                 return [(x,) for x in lst]
119
120         def getSortedTupleTimerList(self):
121                 lst = self.timers[:]
122                 lst.sort()
123                 return [(x,) for x in lst]
124
125         def getUniqueId(self):
126                 self.uniqueTimerId += 1
127                 return self.uniqueTimerId
128
129         def remove(self, uniqueId):
130                 idx = 0
131                 for timer in self.timers:
132                         if timer.id == uniqueId:
133                                 self.timers.pop(idx)
134                                 return
135                         idx += 1
136
137         def set(self, timer):
138                 idx = 0
139                 for stimer in self.timers:
140                         if stimer == timer:
141                                 self.timers[idx] = timer
142                                 return
143                         idx += 1
144                 self.timers.append(timer)
145
146 # Main function
147
148         def parseEPG(self, simulateOnly = False):
149                 if NavigationInstance.instance is None:
150                         print "[AutoTimer] Navigation is not available, can't parse EPG"
151                         return (0, 0, 0, [], [], [])
152
153                 total = 0
154                 new = 0
155                 modified = 0
156                 timers = []
157                 conflicting = []
158                 similar = {}                    # Contains the the marked similar servicerefs and the conflicting string
159                 similars = []                   # Contains the added similar timers 
160
161                 # NOTE: the config option specifies "the next X days" which means today (== 1) + X
162                 delta = timedelta(days = config.plugins.autotimer.maxdaysinfuture.value + 1)
163                 evtLimit = mktime((date.today() + delta).timetuple())
164                 checkEvtLimit = delta.days > 1
165                 del delta
166
167                 # Read AutoTimer configuration
168                 self.readXml()
169
170                 # Get E2 instances
171                 epgcache = eEPGCache.getInstance()
172                 serviceHandler = eServiceCenter.getInstance()
173                 recordHandler = NavigationInstance.instance.RecordTimer
174
175                 # Create dict of all movies in all folders used by an autotimer to compare with recordings
176                 # The moviedict will be filled only if one AutoTimer is configured to avoid duplicate description for any recordings
177                 moviedict = defaultdict(list)
178
179                 # Save Recordings in a dict to speed things up a little
180                 # We include processed timers as we might search for duplicate descriptions
181                 # The recordict is always filled
182                 recorddict = defaultdict(list)
183                 for timer in ( recordHandler.timer_list + recordHandler.processed_timers ):
184                         if timer and timer.service_ref:
185                                 event = epgcache.lookupEventId(timer.service_ref.ref, timer.eit)
186                                 extdesc = event and event.getExtendedDescription()
187                                 timer.extdesc = extdesc
188                                 recorddict[str(timer.service_ref)].append(timer)
189
190                 # Iterate Timer
191                 for timer in self.getEnabledTimerList():
192                         # Precompute timer destination dir
193                         dest = timer.destination or config.usage.default_path.value
194
195                         # Workaround to allow search for umlauts if we know the encoding
196                         match = timer.match
197                         if timer.encoding != 'UTF-8':
198                                 try:
199                                         match = match.decode('UTF-8').encode(timer.encoding)
200                                 except UnicodeDecodeError:
201                                         pass
202
203                         # Search EPG, default to empty list
204                         epgmatches = epgcache.search(('RITBDSE', 500, typeMap[timer.searchType], match, caseMap[timer.searchCase])) or []
205                         # Sort list of tuples by begin time 'B'
206                         epgmatches.sort(key = lambda x: x[3]) 
207
208                         # Reset the the marked similar servicerefs
209                         similar.clear()
210
211                         # Loop over all EPG matches
212                         for idx, ( serviceref, eit, name, begin, duration, shortdesc, extdesc ) in enumerate( epgmatches ):
213
214                                 # Reset similarMatch and conflictString
215                                 similarMatch = False
216                                 conflictString = ""
217                                 
218                                 print "TEST AT name " +str(name)
219                                 print "TEST AT serv " +str(serviceref)
220                                 print "TEST AT name " +str(eit)
221                                 
222                                 # Check if serviceref is in similar matches list
223                                 if eit in similar:
224                                         similarMatch = True
225                                         conflictString = similar[eit]
226
227                                 eserviceref = eServiceReference(serviceref)
228                                 evt = epgcache.lookupEventId(eserviceref, eit)
229                                 if not evt:
230                                         print "[AutoTimer] Could not create Event!"
231                                         continue
232
233                                 # Try to determine real service (we always choose the last one)
234                                 n = evt.getNumOfLinkageServices()
235                                 if n > 0:
236                                         i = evt.getLinkageService(eserviceref, n-1)
237                                         serviceref = i.toString()
238
239                                 evtBegin = begin
240                                 evtEnd = end = begin + duration
241                                 
242                                 # If event starts in less than 60 seconds skip it
243                                 if begin < time() + 60:
244                                         print "[AutoTimer] Skipping an event because it starts in less than 60 seconds"
245                                         continue
246
247                                 # If maximum days in future is set then check time
248                                 if checkEvtLimit:
249                                         if begin > evtLimit:
250                                                 continue
251
252                                 # Convert begin time
253                                 timestamp = localtime(begin)
254                                 # Update timer
255                                 timer.update(begin, timestamp)
256
257                                 # Check Duration, Timespan, Timeframe and Excludes
258                                 # Remove similarMatch if You want perform the check also on similar timers
259                                 if timer.checkServices(serviceref) \
260                                         or timer.checkDuration(duration) \
261                                         or ( not similarMatch and timer.checkTimespan(timestamp) ) \
262                                         or ( not similarMatch and timer.checkTimeframe(begin) ) \
263                                         or timer.checkFilter(name, shortdesc, extdesc, ( not similarMatch and str(timestamp.tm_wday) ) ):
264                                         continue
265
266                                 if timer.hasOffset():
267                                         # Apply custom Offset
268                                         begin, end = timer.applyOffset(begin, end)
269                                 else:
270                                         # Apply E2 Offset
271                                         begin -= config.recording.margin_before.value * 60
272                                         end += config.recording.margin_after.value * 60
273
274                                 # Eventually change service to alternative
275                                 if timer.overrideAlternatives:
276                                         serviceref = timer.getAlternative(serviceref)
277
278                                 total += 1
279
280                                 # Append to timerlist and abort if simulating
281                                 #if not similarMatch: # Do we want the similar timers in the timers list?
282                                 timers.append((name, begin, end, serviceref, timer.name))
283                                 if simulateOnly:
284                                         continue
285
286                                 # Check for existing recordings in directory
287                                 if timer.avoidDuplicateDescription == 3:
288                                         # Reset movie Exists
289                                         movieExists = False
290
291                                         # Eventually create cache
292                                         if dest and dest not in moviedict:
293                                                 movielist = serviceHandler.list(eServiceReference("2:0:1:0:0:0:0:0:0:0:" + dest))
294                                                 if movielist is None:
295                                                         print "[AutoTimer] listing of movies in " + dest + " failed"
296                                                 else:
297                                                         append = moviedict[dest].append
298                                                         while 1:
299                                                                 movieref = movielist.getNext()
300                                                                 if not movieref.valid():
301                                                                         break
302                                                                 if movieref.flags & eServiceReference.mustDescent:
303                                                                         continue
304                                                                 info = serviceHandler.info(movieref)
305                                                                 if info is None:
306                                                                         continue
307                                                                 event = info.getEvent(movieref)
308                                                                 if event is None:
309                                                                         continue
310                                                                 append({
311                                                                         "name": info.getName(movieref),
312                                                                         "shortdesc": info.getInfoString(movieref, iServiceInformation.sDescription),
313                                                                         "extdesc": event.getExtendedDescription()
314                                                                 })
315                                                         del append
316
317                                         for movieinfo in moviedict.get(dest, []):
318                                                 if movieinfo.get("name") == name \
319                                                         and movieinfo.get("shortdesc") == shortdesc \
320                                                         and movieinfo.get("extdesc") == extdesc:
321                                                         print "[AutoTimer] We found a matching recorded movie, skipping event:", name
322                                                         movieExists = True
323                                                         break
324
325                                         if movieExists:
326                                                 continue
327
328                                 # Initialize
329                                 newEntry = None
330                                 oldExists = False
331                                 similarExists = False           # Indicates if we found a matching similar timer
332
333                                 # Check for double Timers
334                                 # We first check eit and if user wants us to guess event based on time
335                                 # we try this as backup. The allowed diff should be configurable though.
336                                 for rtimer in recorddict.get(serviceref, []):
337                                         if rtimer.eit == eit or config.plugins.autotimer.try_guessing.value and getTimeDiff(rtimer, evtBegin, evtEnd) > ((duration/10)*8):
338                                                 oldExists = True
339
340                                                 # Abort if we don't want to modify timers or timer is repeated
341                                                 if config.plugins.autotimer.refresh.value == "none" or rtimer.repeated:
342                                                         print "[AutoTimer] Won't modify existing timer because either no modification allowed or repeated timer"
343                                                         break
344
345                                                 if hasattr(rtimer, "isAutoTimer"):
346                                                         rtimer.log(501, "[AutoTimer] AutoTimer %s modified this automatically generated timer." % (timer.name,))
347                                                 else:
348                                                         if config.plugins.autotimer.refresh.value != "all":
349                                                                 print "[AutoTimer] Won't modify existing timer because it's no timer set by us"
350                                                                 break
351
352                                                         rtimer.log(501, "[AutoTimer] Warning, AutoTimer %s messed with a timer which might not belong to it." % (timer.name,))
353
354                                                 newEntry = rtimer
355                                                 modified += 1
356
357                                                 # Modify values saved in timer
358                                                 newEntry.name = name
359                                                 newEntry.description = shortdesc
360                                                 newEntry.begin = int(begin)
361                                                 newEntry.end = int(end)
362                                                 newEntry.service_ref = ServiceReference(serviceref)
363
364                                                 # Don't store the extended description within the timer
365                                                 if hasattr(newEntry, "extdesc"):
366                                                         del newEntry.extdesc
367
368                                                 break
369                                         elif timer.avoidDuplicateDescription >= 1 \
370                                                 and not rtimer.disabled \
371                                                 and rtimer.name == name \
372                                                 and rtimer.description == shortdesc \
373                                                 and hasattr(rtimer, "extdesc") and rtimer.extdesc == extdesc:
374                                                 oldExists = True
375                                                 print "[AutoTimer] We found a timer (similar service) with same description, skipping event"
376                                                 break
377
378                                 # We found no timer we want to edit
379                                 if newEntry is None:
380                                         # But there is a match
381                                         if oldExists:
382                                                 continue
383
384                                         # We want to search for possible doubles
385                                         if timer.avoidDuplicateDescription >= 2:
386                                                 for rtimer in chain.from_iterable( recorddict.values() ):
387                                                         if not rtimer.disabled \
388                                                                 and rtimer.name == name \
389                                                                 and rtimer.description == shortdesc \
390                                                                 and hasattr(rtimer, "extdesc") and rtimer.extdesc == extdesc:
391                                                                 oldExists = True
392                                                                 print "[AutoTimer] We found a timer (any service) with same description, skipping event"
393                                                                 break
394                                                 if oldExists:
395                                                         continue
396
397                                         if timer.checkCounter(timestamp):
398                                                 print "[AutoTimer] Not adding new timer because counter is depleted."
399                                                 continue
400
401                                         newEntry = RecordTimerEntry(ServiceReference(serviceref), begin, end, name, shortdesc, eit)
402                                         newEntry.log(500, "[AutoTimer] Adding new timer based on AutoTimer %s." % (timer.name,))
403
404                                         # Mark this entry as AutoTimer (only AutoTimers will have this Attribute set)
405                                         newEntry.isAutoTimer = True
406
407                                 # Apply afterEvent
408                                 if timer.hasAfterEvent():
409                                         afterEvent = timer.getAfterEventTimespan(localtime(end))
410                                         if afterEvent is None:
411                                                 afterEvent = timer.getAfterEvent()
412                                         if afterEvent is not None:
413                                                 newEntry.afterEvent = afterEvent
414
415                                 newEntry.dirname = timer.destination
416                                 newEntry.justplay = timer.justplay
417                                 newEntry.tags = timer.tags
418                                 newEntry.vpsplugin_enabled = timer.vps_enabled
419                                 newEntry.vpsplugin_overwrite = timer.vps_overwrite
420
421                                 if oldExists:
422                                         # XXX: this won't perform a sanity check, but do we actually want to do so?
423                                         recordHandler.timeChanged(newEntry)
424                                 else:
425                                         if similarMatch:
426                                                 newEntry.log(504, "[AutoTimer] Similar Timer is added because of conflicts with %s." % (conflictString))
427                                         conflicts = recordHandler.record(newEntry)
428
429                                         if conflicts:
430                                                 conflictString += ' / '.join(["%s (%s)" % (x.name, strftime("%Y%m%d %H%M", localtime(x.begin))) for x in conflicts])
431
432                                         if conflicts and config.plugins.autotimer.addsimilar_on_conflict.value:
433                                                 # We start our search right after our actual index
434                                                 for servicerefS, eitS, nameS, beginS, durationS, shortdescS, extdescS in epgmatches[idx+1:]:
435                                                         if evtBegin < beginS:
436                                                                 # Match must be after our conflicting event
437                                                                 if extdesc == extdescS and shortdesc == shortdescS:
438                                                                         # Match only if the descriptions are equal
439                                                                         print "[AutoTimer] Found similar Timer: " + name
440                                                                         
441                                                                         # Indicate that there exists a similar timer
442                                                                         similarExists = True
443
444                                                                         # Store the similar serviceref and conflictString, so it can be handled later
445                                                                         similar[eit] = conflictString
446                                                                         
447                                                                         # If this one also will conflict and there are more matches, they will be marked the next time 
448                                                                         break
449
450                                         if conflicts and config.plugins.autotimer.disabled_on_conflict.value and not similarExists:                     # Don't add disabled timer if a similar timer exists
451                                         #if conflicts and config.plugins.autotimer.disabled_on_conflict.value:                                                                                                  # Add disabled timer even if a similar timer exists
452                                                 newEntry.log(503, "[AutoTimer] Timer disabled because of conflicts with %s." % (conflictString,))
453                                                 newEntry.disabled = True
454                                                 # We might want to do the sanity check locally so we don't run it twice - but I consider this workaround a hack anyway
455                                                 conflicts = recordHandler.record(newEntry)
456                                                 conflicting.append((name, begin, end, serviceref, timer.name))
457
458                                         if conflicts is None:                                                                                   # Similar timers will be added to the new timer list
459                                         #if conflicts is None and not similarMatch: # Similar timers won't be added to the new timer list
460                                                 timer.decrementCounter()
461                                                 new += 1
462                                                 timer.extdesc = extdesc
463                                                 recorddict[serviceref].append(newEntry)
464
465                                         elif conflicts:                                                                                                                 # Conflicting timers will be added also if a similar timer exists
466                                         #elif conflicts and not similarExists:                  # Conflicting timers won't be added if a similar timer exists
467                                                 conflicting.append((name, begin, end, serviceref, timer.name))
468
469                                         if similarMatch and conflicts is None:                  # Similar timers can be in new timers list and additionally in similar timers list
470                                         #elif similarMatch and conflicts is None:               # Similar timers will be only in new timers list or similar timers list
471                                                 similars.append((name, begin, end, serviceref, timer.name))
472
473                 return (total, new, modified, timers, conflicting, similars)