1 # To check if in Standby
5 from enigma import eServiceReference, eServiceCenter
8 from EPGRefreshTimer import epgrefreshtimer, EPGRefreshTimerEntry, checkTimespan
10 # To calculate next timer execution
14 from xml.dom.minidom import parse as minidom_parse
15 from os import path as path
17 # We want a list of unique services
21 from Components.config import config
23 # Path to configuration
24 CONFIG = "/etc/enigma2/epgrefresh.xml"
27 # Support for old configuration
30 OLD_CONFIG = "/etc/enigma2/epgrefresh.conf"
31 def readOldConfig(myset):
33 file = open(OLD_CONFIG, 'r')
44 def getValue(definition, default):
48 # Iterate through nodes of last one
49 for node in definition.childNodes:
50 # Append text if we have a text node
51 if node.nodeType == node.TEXT_NODE:
54 # Return stripped output or (if empty) default
55 return ret.strip() or default
58 """Simple Class to refresh EPGData"""
62 self.services = (Set(), Set())
63 self.previousService = None
64 self.forcedScan = False
66 self.beginOfTimespan = 0
68 # Mtime of configuration files
71 # Read in Configuration
73 self.readConfiguration()
75 print "[EPGRefresh] Error occured while reading in configuration:", e
77 def readConfiguration(self):
79 # Support for old configuration
82 if path.exists(OLD_CONFIG):
83 self.services[0].clear()
84 readOldConfig(self.services[0])
87 self.saveConfiguration()
93 # Check if file exists
94 if not path.exists(CONFIG):
97 # Check if file did not change
98 mtime = path.getmtime(CONFIG)
99 if mtime == self.configMtime:
103 self.configMtime = mtime
106 self.services[0].clear()
107 self.services[1].clear()
110 dom = minidom_parse(CONFIG)
113 for configuration in dom.getElementsByTagName("epgrefresh"):
114 for service in configuration.getElementsByTagName("service"):
115 value = getValue(service, None)
117 # strip all after last : (custom name)
118 pos = value.rfind(':')
120 value = value[:pos+1]
122 self.services[0].add(value)
123 for bouquet in configuration.getElementsByTagName("bouquet"):
124 value = getValue(bouquet, None)
126 self.services[1].add(value)
128 def saveConfiguration(self):
129 # Generate List in RAM
130 list = ['<?xml version="1.0" ?>\n<epgrefresh>\n\n']
132 for service in self.services[0]:
133 ref = ServiceReference(str(serviceref))
134 list.extend([' <!-- ', ref.getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', ''), ' -->\n'])
135 list.extend([' <service>', service, '</service>\n'])
136 for bouquet in self.services[1]:
137 ref = ServiceReference(str(serviceref))
138 list.extend([' <!-- ', ref.getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', ''), ' -->\n'])
139 list.extend([' <bouquet>', bouquet, '</bouquet>\n'])
141 list.append('\n</epgrefresh>')
143 # Try Saving to Flash
146 file = open(CONFIG, 'w')
147 file.writelines(list)
151 print "[EPGRefresh] Error Saving Service List:", e
153 def forceRefresh(self, session = None):
154 print "[EPGRefresh] Forcing start of EPGRefresh"
155 if session is not None:
156 self.session = session
158 self.forcedScan = True
159 self.prepareRefresh()
161 def start(self, session = None):
162 if session is not None:
163 self.session = session
165 epgrefreshtimer.setRefreshTimer(self.createWaitTimer)
168 print "[EPGRefresh] Stopping Timer"
169 epgrefreshtimer.clear()
171 def prepareRefresh(self):
172 print "[EPGRefresh] About to start refreshing EPG"
175 self.previousService = self.session.nav.getCurrentlyPlayingServiceReference()
177 # Maybe read in configuration
179 self.readConfiguration()
181 print "[EPGRefresh] Error occured while reading in configuration:", e
183 # Save Services in a dict <transponder data> => serviceref
184 self.scanServices = []
187 # This will hold services which are not explicitely in our servicelist
188 additionalServices = []
190 serviceHandler = eServiceCenter.getInstance()
191 for bouquet in self.services[1]:
192 myref = eServiceReference(str(bouquet))
193 list = serviceHandler.list(myref)
197 # TODO: I wonder if its sane to assume we get services here (and not just new lists)
199 additionalServices.append(s.toString())
203 # See if we are supposed to read in autotimer services
204 if config.plugins.epgrefresh.inherit_autotimer.value:
207 from Plugins.Extensions.AutoTimer.plugin import autotimer
209 # See if instance is empty
210 removeInstance = False
211 if autotimer is None:
212 removeInstance = True
214 from Plugins.Extensions.AutoTimer.AutoTimer import AutoTimer
215 autotimer = AutoTimer()
217 # Read in configuration
221 for timer in autotimer.getEnabledTimerList():
222 additionalServices.extend(timer.getServices())
224 # Remove instance if there wasn't one before
225 # we might go into the exception before we reach this line, but we don't care then...
230 print "[EPGRefresh] Could not inherit AutoTimer Services:", e
232 # TODO: does this really work?
233 for serviceref in self.services[0].union(additionalServices):
234 service = eServiceReference(str(serviceref))
235 if not service.valid() \
236 or (service.flags & (eServiceReference.isMarker|eServiceReference.isDirectory)):
240 channelID = '%08x%04x%04x' % (
241 service.getUnsignedData(4), # NAMESPACE
242 service.getUnsignedData(2), # TSID
243 service.getUnsignedData(3), # ONID
246 if channelID not in channelIdList:
247 self.scanServices.append(service)
248 channelIdList.append(channelID)
251 from ServiceReference import ServiceReference
252 print "[EPGRefresh] Services we're going to scan:", ', '.join([ServiceReference(x).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '') for x in self.scanServices])
257 config.plugins.epgrefresh.lastscan.value = int(time())
258 config.plugins.epgrefresh.lastscan.save()
260 # shutdown if we're supposed to go to deepstandby and not recording
261 if not self.forcedScan and config.plugins.epgrefresh.afterevent.value \
262 and not Screens.Standby.inTryQuitMainloop:
265 Screens.Standby.TryQuitMainloop,
269 self.forcedScan = False
270 epgrefreshtimer.cleanup()
273 if self.previousService is not None or Screens.Standby.inStandby:
274 self.session.nav.playService(self.previousService)
280 # Abort if a scan finished later than our begin of timespan
281 if self.beginOfTimespan < config.plugins.epgrefresh.lastscan.value:
283 if config.plugins.epgrefresh.force.value \
284 or (Screens.Standby.inStandby and \
285 not self.session.nav.RecordTimer.isRecording()):
288 # We don't follow our rules here - If the Box is still in Standby and not recording we won't reach this line
290 if not checkTimespan(
291 config.plugins.epgrefresh.begin.value,
292 config.plugins.epgrefresh.end.value):
294 print "[EPGRefresh] Gone out of timespan while refreshing, sorry!"
297 print "[EPGRefresh] Box no longer in Standby or Recording started, rescheduling"
300 epgrefreshtimer.add(EPGRefreshTimerEntry(
301 time() + config.plugins.epgrefresh.delay_standby.value*60,
306 def createWaitTimer(self):
307 self.beginOfTimespan = time()
309 # Add wait timer to epgrefreshtimer
310 epgrefreshtimer.add(EPGRefreshTimerEntry(time() + 30, self.prepareRefresh))
312 def nextService(self):
314 print "[EPGRefresh] Maybe zap to next service"
318 service = self.scanServices.pop(0)
321 self.session.nav.playService(service)
324 epgrefreshtimer.add(EPGRefreshTimerEntry(
325 time() + config.plugins.epgrefresh.interval.value*60,
331 print "[EPGRefresh] Done refreshing EPG"
336 epgrefresh = EPGRefresh()