1 # To check if in Standby
5 from enigma import eServiceReference, eServiceCenter
8 from ServiceReference import ServiceReference
11 from EPGRefreshTimer import epgrefreshtimer, EPGRefreshTimerEntry, checkTimespan
13 # To calculate next timer execution
17 from xml.etree.cElementTree import parse as cet_parse
18 from os import path as path
20 # We want a list of unique services
24 from Components.config import config
26 # Path to configuration
27 CONFIG = "/etc/enigma2/epgrefresh.xml"
30 """Simple Class to refresh EPGData"""
34 self.services = (Set(), Set())
35 self.previousService = None
36 self.forcedScan = False
38 self.beginOfTimespan = 0
40 # Mtime of configuration files
43 # Read in Configuration
44 self.readConfiguration()
46 def readConfiguration(self):
47 # Check if file exists
48 if not path.exists(CONFIG):
51 # Check if file did not change
52 mtime = path.getmtime(CONFIG)
53 if mtime == self.configMtime:
57 self.configMtime = mtime
60 self.services[0].clear()
61 self.services[1].clear()
64 configuration= cet_parse(CONFIG).getroot()
67 for service in configuration.findall("service"):
70 # strip all after last : (custom name)
71 pos = value.rfind(':')
75 self.services[0].add(value)
76 for bouquet in configuration.findall("bouquet"):
79 self.services[1].add(value)
81 def saveConfiguration(self):
82 # Generate List in RAM
83 list = ['<?xml version="1.0" ?>\n<epgrefresh>\n\n']
85 for service in self.services[0]:
86 ref = ServiceReference(str(service))
87 list.extend([' <!-- ', ref.getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', ''), ' -->\n'])
88 list.extend([' <service>', service, '</service>\n'])
89 for bouquet in self.services[1]:
90 ref = ServiceReference(str(bouquet))
91 list.extend([' <!-- ', ref.getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', ''), ' -->\n'])
92 list.extend([' <bouquet>', bouquet, '</bouquet>\n'])
94 list.append('\n</epgrefresh>')
97 file = open(CONFIG, 'w')
102 def forceRefresh(self, session = None):
103 print "[EPGRefresh] Forcing start of EPGRefresh"
104 if session is not None:
105 self.session = session
107 self.forcedScan = True
108 self.prepareRefresh()
110 def start(self, session = None):
111 if session is not None:
112 self.session = session
114 epgrefreshtimer.setRefreshTimer(self.createWaitTimer)
117 print "[EPGRefresh] Stopping Timer"
118 epgrefreshtimer.clear()
120 def prepareRefresh(self):
121 print "[EPGRefresh] About to start refreshing EPG"
124 self.previousService = self.session.nav.getCurrentlyPlayingServiceReference()
126 # Maybe read in configuration
128 self.readConfiguration()
130 print "[EPGRefresh] Error occured while reading in configuration:", e
132 # Save Services in a dict <transponder data> => serviceref
133 self.scanServices = []
136 # This will hold services which are not explicitely in our list
137 additionalServices = []
138 additionalBouquets = []
140 # See if we are supposed to read in autotimer services
141 if config.plugins.epgrefresh.inherit_autotimer.value:
144 from Plugins.Extensions.AutoTimer.plugin import autotimer
146 # See if instance is empty
147 removeInstance = False
148 if autotimer is None:
149 removeInstance = True
151 from Plugins.Extensions.AutoTimer.AutoTimer import AutoTimer
152 autotimer = AutoTimer()
154 # Read in configuration
158 for timer in autotimer.getEnabledTimerList():
159 additionalServices.extend(timer.getServices())
160 additionalBouquets.extend(timer.getBouquets())
162 # Remove instance if there wasn't one before
163 # we might go into the exception before we reach this line, but we don't care then...
168 print "[EPGRefresh] Could not inherit AutoTimer Services:", e
170 serviceHandler = eServiceCenter.getInstance()
171 for bouquet in self.services[1].union(additionalBouquets):
172 myref = eServiceReference(str(bouquet))
173 list = serviceHandler.list(myref)
177 # TODO: I wonder if its sane to assume we get services here (and not just new lists)
179 additionalServices.append(s.toString())
183 for serviceref in self.services[0].union(additionalServices):
184 service = eServiceReference(str(serviceref))
185 if not service.valid() \
186 or (service.flags & (eServiceReference.isMarker|eServiceReference.isDirectory)):
190 channelID = '%08x%04x%04x' % (
191 service.getUnsignedData(4), # NAMESPACE
192 service.getUnsignedData(2), # TSID
193 service.getUnsignedData(3), # ONID
196 if channelID not in channelIdList:
197 self.scanServices.append(service)
198 channelIdList.append(channelID)
201 print "[EPGRefresh] Services we're going to scan:", ', '.join([ServiceReference(x).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '') for x in self.scanServices])
206 config.plugins.epgrefresh.lastscan.value = int(time())
207 config.plugins.epgrefresh.lastscan.save()
209 # shutdown if we're supposed to go to deepstandby and not recording
210 if not self.forcedScan and config.plugins.epgrefresh.afterevent.value \
211 and not Screens.Standby.inTryQuitMainloop:
214 Screens.Standby.TryQuitMainloop,
218 self.forcedScan = False
219 epgrefreshtimer.cleanup()
222 if self.previousService is not None or Screens.Standby.inStandby:
223 self.session.nav.playService(self.previousService)
229 # Abort if a scan finished later than our begin of timespan
230 if self.beginOfTimespan < config.plugins.epgrefresh.lastscan.value:
232 if config.plugins.epgrefresh.force.value \
233 or (Screens.Standby.inStandby and \
234 not self.session.nav.RecordTimer.isRecording()):
237 # We don't follow our rules here - If the Box is still in Standby and not recording we won't reach this line
239 if not checkTimespan(
240 config.plugins.epgrefresh.begin.value,
241 config.plugins.epgrefresh.end.value):
243 print "[EPGRefresh] Gone out of timespan while refreshing, sorry!"
246 print "[EPGRefresh] Box no longer in Standby or Recording started, rescheduling"
249 epgrefreshtimer.add(EPGRefreshTimerEntry(
250 time() + config.plugins.epgrefresh.delay_standby.value*60,
255 def createWaitTimer(self):
256 self.beginOfTimespan = time()
258 # Add wait timer to epgrefreshtimer
259 epgrefreshtimer.add(EPGRefreshTimerEntry(time() + 30, self.prepareRefresh))
261 def nextService(self):
263 print "[EPGRefresh] Maybe zap to next service"
267 service = self.scanServices.pop(0)
270 self.session.nav.playService(service)
273 epgrefreshtimer.add(EPGRefreshTimerEntry(
274 time() + config.plugins.epgrefresh.interval.value*60,
280 print "[EPGRefresh] Done refreshing EPG"
285 epgrefresh = EPGRefresh()