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