v1.5 fixes for gemini 3.1
[enigma2-plugins.git] / epgrefresh / src / EPGRefresh.py
1 # To check if in Standby
2 import Screens.Standby
3
4 # eServiceReference
5 from enigma import eServiceReference
6
7 # Timer
8 from EPGRefreshTimer import epgrefreshtimer, EPGRefreshTimerEntry, checkTimespan
9
10 # To calculate next timer execution
11 from time import time
12
13 # Path (check if file exists, getmtime)
14 from os import path
15
16 # We want a list of unique services
17 from sets import Set
18
19 # Configuration
20 from Components.config import config
21
22 # Path to configuration
23 CONFIG = "/etc/enigma2/epgrefresh.conf"
24
25 class EPGRefresh:
26         """WIP - Simple Class to refresh EPGData - WIP"""
27
28         def __init__(self):
29                 # Initialize 
30                 self.services = Set()
31                 self.previousService = None
32                 self.forcedScan = False
33                 self.session = None
34                 self.beginOfTimespan = 0
35
36                 # Mtime of configuration files
37                 self.configMtime = -1
38
39                 # Read in Configuration
40                 self.readConfiguration()
41
42         def readConfiguration(self):
43                 # Check if file exists
44                 if not path.exists(CONFIG):
45                         return
46
47                 # Check if file did not change
48                 mtime = path.getmtime(CONFIG)
49                 if mtime == self.configMtime:
50                         return
51
52                 # Keep mtime
53                 self.configMtime = mtime
54
55                 # Empty out list
56                 self.services.clear()
57
58                 # Open file
59                 file = open(CONFIG, 'r')
60
61                 # Add References
62                 for line in file:
63                         line = line.strip()
64                         if line:
65                                 self.services.add(line)
66
67                 # Close file
68                 file.close()
69
70         def saveConfiguration(self):
71                 # Open file
72                 file = open(CONFIG, 'w')
73
74                 # Write references
75                 for serviceref in self.services:
76                         file.write(serviceref)
77                         file.write('\n')
78
79                 # Close file
80                 file.close()
81
82         def forceRefresh(self, session = None):
83                 print "[EPGRefresh] Forcing start of EPGRefresh"
84                 if session is not None:
85                         self.session = session
86
87                 self.forcedScan = True
88                 self.prepareRefresh()
89
90         def start(self, session = None):
91                 if session is not None:
92                         self.session = session
93
94                 epgrefreshtimer.setRefreshTimer(self.createWaitTimer)
95
96         def stop(self):
97                 print "[EPGRefresh] Stopping Timer"
98                 epgrefreshtimer.clear()
99
100         def prepareRefresh(self):
101                 print "[EPGRefresh] About to start refreshing EPG"
102
103                 # Keep service
104                 self.previousService =  self.session.nav.getCurrentlyPlayingServiceReference()
105
106                 # Maybe read in configuration
107                 try:
108                         self.readConfiguration()
109                 except Exception, e:
110                         print "[EPGRefresh] Error occured while reading in configuration:", e
111
112                 # Save Services in a dict <transponder data> => serviceref
113                 self.scanServices = []
114                 channelIdList = []
115
116                 # TODO: does this really work?
117                 for serviceref in self.services:
118                         service = eServiceReference(serviceref)
119                         channelID = '%08x%04x%04x' % (
120                                 service.getUnsignedData(4), # NAMESPACE
121                                 service.getUnsignedData(2), # TSID
122                                 service.getUnsignedData(3), # ONID
123                         )
124                         if channelID not in channelIdList:
125                                 self.scanServices.append(service)
126
127                 # See if we are supposed to read in autotimer services
128                 if config.plugins.epgrefresh.inherit_autotimer.value:
129                         try:
130                                 # Import Instance
131                                 from Plugins.Extensions.AutoTimer.plugin import autotimer
132
133                                 # See if instance is empty
134                                 if autotimer is None:
135                                         # Create an instance
136                                         from Plugins.Extensions.AutoTimer.AutoTimer import AutoTimer
137                                         autotimer = AutoTimer()
138
139                                 # Read in configuration
140                                 autotimer.readXml()
141
142                                 # Fetch services
143                                 for timer in autotimer.getEnabledTimerList():
144                                         for serviceref in timer.getServices():
145                                                 service = eServiceReference(str(serviceref))
146                                                 channelID = '%08x%04x%04x' % (
147                                                         service.getUnsignedData(4), # NAMESPACE
148                                                         service.getUnsignedData(2), # TSID
149                                                         service.getUnsignedData(3), # ONID
150                                                 )
151                                                 if channelID not in channelIdList:
152                                                         self.scanServices.append(service)
153                         except Exception, e:
154                                 print "[EPGRefresh] Could not inherit AutoTimer Services:", e
155
156                 # Debug
157                 from ServiceReference import ServiceReference
158                 print "[EPGRefresh] Services we're going to scan:", ', '.join([ServiceReference(x).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '') for x in self.scanServices])
159
160                 self.refresh()
161
162         def cleanUp(self):
163                 config.plugins.epgrefresh.lastscan.value = int(time())
164                 config.plugins.epgrefresh.lastscan.save()
165
166                 # shutdown if we're supposed to go to deepstandby and not recording
167                 if not self.forcedScan and config.plugins.epgrefresh.afterevent.value and not Screens.Standby.inTryQuitMainloop:
168                         self.session.open(
169                                 Screens.Standby.TryQuitMainloop,
170                                 1
171                         )
172
173                 self.forcedScan = False
174                 epgrefreshtimer.cleanup()
175
176                 # Zap back
177                 if self.previousService is not None or Screens.Standby.inStandby:
178                         self.session.nav.playService(self.previousService)
179
180         def refresh(self):
181                 if self.forcedScan:
182                         self.nextService()
183                 else:
184                         # Abort if a scan finished later than our begin of timespan
185                         if self.beginOfTimespan < config.plugins.epgrefresh.lastscan.value:
186                                 return
187                         if config.plugins.epgrefresh.force.value or (Screens.Standby.inStandby and not self.session.nav.RecordTimer.isRecording()):
188                                 self.nextService()
189                         # We don't follow our rules here - If the Box is still in Standby and not recording we won't reach this line 
190                         else:
191                                 if not checkTimespan(config.plugins.epgrefresh.begin.value, config.plugins.epgrefresh.end.value):
192                                         print "[EPGRefresh] Gone out of timespan while refreshing, sorry!"
193                                         self.cleanUp()
194                                 else:
195                                         print "[EPGRefresh] Box no longer in Standby or Recording started, rescheduling"
196
197                                         # Recheck later
198                                         epgrefreshtimer.add(EPGRefreshTimerEntry(time() + config.plugins.epgrefresh.delay_standby.value*60, self.refresh, nocheck = True))
199
200         def createWaitTimer(self):
201                 self.beginOfTimespan = time()
202
203                 # Add wait timer to epgrefreshtimer
204                 epgrefreshtimer.add(EPGRefreshTimerEntry(time() + 30, self.prepareRefresh))
205
206         def nextService(self):
207                 # DEBUG
208                 print "[EPGRefresh] Maybe zap to next service"
209
210                 try:
211                         # Get next reference
212                         service = self.scanServices.pop(0)
213
214                         # Play next service
215                         self.session.nav.playService(service)
216
217                         # Start Timer
218                         epgrefreshtimer.add(EPGRefreshTimerEntry(time() + config.plugins.epgrefresh.interval.value*60, self.refresh, nocheck = True))
219                 except IndexError:
220                         # Debug
221                         print "[EPGRefresh] Done refreshing EPG"
222
223                         # Clean up
224                         self.cleanUp()
225
226 epgrefresh = EPGRefresh()