ServiceList improvements for WebControl and WebTV (Remaining time and total is now...
[vuplus_dvbapp-plugin] / webinterface / src / WebComponents / Sources / EPG.py
1 from Components.Sources.Source import Source
2 from enigma import eServiceCenter, eServiceReference, eEPGCache
3
4 class EPG(Source):
5     BOUQUETNOW = 0
6     BOUQUETNEXT = 1
7     SERVICENOW = 2
8     SERVICENEXT = 3
9     SERVICE = 4
10     SEARCH = 5
11     BOUQUET = 6
12     SEARCHSIMILAR = 7
13     def __init__(self, navcore, func=BOUQUETNOW, endtm=False):
14         self.func = func
15         Source.__init__(self)
16         self.navcore = navcore
17         self.epgcache = eEPGCache.getInstance()
18         self.command = None
19         self.endtime = endtm
20
21     def handleCommand(self, cmd):
22         self.command = cmd
23
24     def do_func(self):
25         if not self.command is None:
26             if self.func is self.SEARCHSIMILAR:
27                 func = self.searchSimilarEvent
28             elif self.func is self.SEARCH:
29                 func = self.searchEvent
30             elif self.func is self.SERVICE:
31                 func = self.getEPGofService
32             elif self.func is self.BOUQUETNOW:
33                 func = self.getBouquetEPGNow
34             elif self.func is self.BOUQUETNEXT:
35                 func = self.getBouquetEPGNext
36             elif self.func is self.BOUQUET:
37                 func = self.getEPGofBouquet
38             elif self.func is self.SERVICENOW:
39                 func = self.getServiceEPGNow
40             elif self.func is self.SERVICENEXT:
41                 func = self.getServiceEPGNext
42
43             return func(self.command)
44         return ()
45
46     def getBouquetEPGNow(self, ref):
47         return self.getEPGNowNext(ref, 0)
48
49     def getBouquetEPGNext(self, ref):
50         return self.getEPGNowNext(ref, 1)
51
52     def getServiceEPGNow(self, ref):
53         return self.getEPGNowNext(ref, 0, True)
54
55     def getServiceEPGNext(self, ref):
56         return self.getEPGNowNext(ref, 1, True)
57
58     def getEPGNowNext(self, ref, type, service=False):
59         print "[WebComponents.EPG] getting EPG NOW/NEXT", ref
60
61         if service:
62             events = self.epgcache.lookupEvent(['IBDCTSERNX', (ref, type, -1)])
63         else:
64             serviceHandler = eServiceCenter.getInstance()
65             list = serviceHandler.list(eServiceReference(ref))
66             services = list and list.getContent('S')
67             search = ['IBDCTSERNX']
68
69             if services: # It's a Bouquet
70                 search.extend([(service, type, -1) for service in services])
71
72             events = self.epgcache.lookupEvent(search)
73
74         if events:
75             return events
76         return ()
77
78     def getEPGofService(self, ref, options='IBDCTSERN'):
79         print "getting EPG of Service", ref
80         events = self.epgcache.lookupEvent([options , (ref, 0, -1, -1)]);
81         if events:
82             if self.endtime:
83                 list = self.insertEndTime(events)
84                 return list
85
86             return events
87         return ()
88
89     def insertEndTime(self, events):
90         list = []
91         for event in events:
92             i = 0
93             evt = []
94             end = event[1] + event[2]
95             for item in event:
96                 if i == 3:
97                     evt.append(end)
98                     i += 1
99
100                 evt.append(item)
101                 i += 1
102
103             list.append(evt)
104
105         return list
106
107     def getEPGofBouquet(self, param):
108         print "[WebComponents.EPG] getting EPG for Bouquet", param
109
110         if 'bRef' not in param:
111             return ()
112
113         time = -1
114         if 'time' in param:
115             if param['time'] is not None:
116                 time = int(float(param['time']))
117                 if time < 0:
118                     time = -1
119
120         bRef = param['bRef']
121
122         serviceHandler = eServiceCenter.getInstance()
123         sl = serviceHandler.list(eServiceReference(bRef))
124         services = sl and sl.getContent('S')
125
126         search = ['IBDCTSERN']
127         search.extend([(service, 0, time) for service in services])
128
129         events = self.epgcache.lookupEvent(search)
130
131         if events:
132             return events
133         return ()
134
135     def searchEvent(self, needle):
136         print "[WebComponents.EPG] searching EPG: ", needle
137
138         events = self.epgcache.search(('IBDCTSERN', 256, eEPGCache.PARTIAL_TITLE_SEARCH, needle, 1));
139         if events:
140             return events
141         return ()
142
143     def searchSimilarEvent(self, needle):
144         print "[WebComponents.EPG] searching similar eventid: ",needle
145
146         events = self.epgcache.search(('IBDCTSERN', 256, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, needle['sRef'], int(needle['eventid'])));
147         if events:
148             return events
149         return ()
150
151     def getLut(self):
152         if self.endtime:
153             lut = {
154                     "EventID": 0,
155                     "TimeStart": 1,
156                     "Duration": 2,
157                     "TimeEnd": 3,
158                     "CurrentTime": 4,
159                     "Title": 5,
160                     "Description": 6,
161                     "DescriptionExtended": 7,
162                     "ServiceReference": 8,
163                     "ServiceName": 9
164                 }
165             return lut
166         else:
167             lut = {
168                 "EventID": 0,
169                 "TimeStart": 1,
170                 "Duration": 2,
171                 "CurrentTime": 3,
172                 "Title": 4,
173                 "Description": 5,
174                 "DescriptionExtended": 6,
175                 "ServiceReference": 7,
176                 "ServiceName": 8
177             }
178
179             return lut
180
181     list = property(do_func)
182
183     lut = property(getLut)