eventlist: take care of startTimeQuery return value (value != 0 is a error)
[enigma2-plugins.git] / eventlist / src / EventList.py
1 # -*- coding: utf-8 -*-
2 #
3 # EventList - Converter
4 #
5 # Coded by Dr.Best (c) 2013
6 # Support: www.dreambox-tools.info
7 # E-Mail: dr.best@dreambox-tools.info
8 #
9 # This plugin is open source but it is NOT free software.
10 #
11 # This plugin may only be distributed to and executed on hardware which
12 # is licensed by Dream Multimedia GmbH.
13 # In other words:
14 # It's NOT allowed to distribute any parts of this plugin or its source code in ANY way
15 # to hardware which is NOT licensed by Dream Multimedia GmbH.
16 # It's NOT allowed to execute this plugin and its source code or even parts of it in ANY way
17 # on hardware which is NOT licensed by Dream Multimedia GmbH.
18 #
19 # If you want to use or modify the code or parts of it,
20 # you have to keep MY license and inform me about the modifications by mail.
21 #
22
23
24 from Components.Converter.Converter import Converter
25 from Components.Element import cached
26
27 from enigma import eEPGCache, eServiceReference
28 from time import localtime, strftime, mktime, time
29 from datetime import datetime, timedelta
30
31 class EventList(Converter, object):
32         def __init__(self, type):
33                 Converter.__init__(self, type)
34                 self.epgcache = eEPGCache.getInstance()
35                 self.primetime = 0
36                 self.eventcount = 0
37                 if (len(type)):
38                         args = type.split(',')
39                         i = 0
40                         while i <= len(args)-1:
41                                 type_c, value = args[i].split('=')
42                                 if type_c == "eventcount":
43                                         self.eventcount = int(value)                    
44                                 elif type_c == "primetime":
45                                         if value == "yes":
46                                                 self.primetime = 1
47                                 i +=1                   
48         @cached
49         def getContent(self):
50                 contentList = []
51                 ref = self.source.service
52                 info = ref and self.source.info
53                 if info is None:
54                         return []
55                 curEvent = self.source.getCurrentEvent()
56                 if curEvent:
57                         if not self.epgcache.startTimeQuery(eServiceReference(ref.toString()), curEvent.getBeginTime() + curEvent.getDuration()):
58                                 i = 1
59                                 while i <= (self.eventcount):
60                                         event = self.epgcache.getNextTimeEntry()
61                                         if event is not None:
62                                                 contentList.append(self.getEventTuple(event),)
63                                         i +=1
64                                 if self.primetime == 1:
65                                         now = localtime(time())
66                                         dt = datetime(now.tm_year, now.tm_mon, now.tm_mday, 20, 15)
67                                         if time() > mktime(dt.timetuple()):
68                                                 dt += timedelta(days=1) # skip to next day...
69                                         primeTime = int(mktime(dt.timetuple()))
70                                         if not self.epgcache.startTimeQuery(eServiceReference(ref.toString()), primeTime):
71                                                 event = self.epgcache.getNextTimeEntry()
72                                                 if event and (event.getBeginTime() <= int(mktime(dt.timetuple()))):
73                                                         contentList.append(self.getEventTuple(event),)
74                 return contentList
75
76         def getEventTuple(self,event):
77                 time = "%s - %s" % (strftime("%H:%M", localtime(event.getBeginTime())), strftime("%H:%M", localtime(event.getBeginTime() + event.getDuration())))
78                 title = event.getEventName()
79                 duration = "%d min" % (event.getDuration() / 60)
80                 return (time,title,duration)
81
82         def changed(self, what):
83                 if what[0] != self.CHANGED_SPECIFIC:
84                         Converter.changed(self, what)