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