3 from enigma import eServiceReference, eEPGCache
4 from Components.Sources.Source import Source
5 from Components.config import config
6 from ServiceReference import ServiceReference
7 from RecordTimer import RecordTimerEntry, RecordTimer, AFTEREVENT, parseEvent
8 from Components.config import config
9 from xml.sax.saxutils import unescape
10 from time import time, strftime, localtime, mktime
11 from string import split
23 def __init__(self, session, func = LIST):
26 self.session = session
27 self.recordtimer = session.nav.RecordTimer
28 self.epgcache = eEPGCache.getInstance()
29 self.result = False,"unknown command"
32 def handleCommand(self, cmd):
33 if self.func is self.ADDBYID:
34 self.result = self.addTimerByEventID(cmd)
36 elif self.func is self.ADD:
37 self.result = self.editTimer(cmd)
39 elif self.func is self.TVBROWSER:
40 self.result = self.tvBrowser(cmd)
42 elif self.func is self.DEL:
43 self.result = self.delTimer(cmd)
45 elif self.func is self.CHANGE:
46 self.result = self.editTimer(cmd)
48 elif self.func is self.WRITE:
49 self.result = self.writeTimerList(force=True)
50 elif self.func is self.RECNOW:
52 self.result = self.recordNow(cmd)
54 self.result = False, "Unknown function: '%s'" %(self.func)
58 def delTimer(self, param):
59 print "[WebComponents.Timer] delTimer"
61 if param.has_key('sRef'):
62 service_ref = ServiceReference(param['sRef'])
64 return False, "Missing Parameter: sRef"
66 if param.has_key('begin'):
67 begin = int(param['begin'])
69 return False, "Missing Parameter: begin"
71 if param.has_key('end'):
72 end = int(param['end'])
74 return False, "Missing Parameter: end"
77 for timer in self.recordtimer.timer_list + self.recordtimer.processed_timers:
78 if str(timer.service_ref) == str(service_ref) and int(timer.begin) == begin and int(timer.end) == end:
79 self.recordtimer.removeEntry(timer)
80 return True, "The timer '%s' has been deleted successfully" %(timer.name)
82 return False, "The timer has NOT been deleted"
84 return False, "No matching Timer found"
87 def tvBrowser(self, param):
88 print "[WebComponents.Timer] tvbrowser"
90 """ The URL's for the tvBrowser-Capture-Driver are:
92 http://dreambox/web/tvbrowser? +
95 &command=add&&year={year}&month={month}&day={day}&shour={start_hour}&smin={start_minute}&ehour={end_hour}&emin={end_minute}&sRef={urlencode(channel_name_external, "utf8")}&name={urlencode(title, "utf8")}&description={urlencode(descr, "utf8")}&dirname={dirname}&tags={urlencode("tag1 tag2...", "utf8")}&afterevent=0&eit=&disabled=0&justplay=0&repeated=0
98 &command=add&&year={year}&month={month}&day={day}&shour={start_hour}&smin={start_minute}&ehour={end_hour}&emin={end_minute}&sRef={urlencode(channel_name_external, "utf8")}&name={urlencode(title, "utf8")}&description={urlencode(descr, "utf8")}&dirname={dirname}&tags={urlencode("tag1 tag2...", "utf8")}&afterevent=0&eit=&disabled=0&justplay=1&repeated=0
101 &command=del&&year={year}&month={month}&day={day}&shour={start_hour}&smin={start_minute}&ehour={end_hour}&emin={end_minute}&sRef={urlencode(channel_name_external, "utf8")}
104 listDate = ['year','month','day','shour','smin','ehour','emin']
105 for element in listDate:
106 if param[element] is None:
107 return False,"%s missing"%element
109 param[element] = int(param[element])
110 param['begin'] = int(mktime( (param['year'], param['month'], param['day'], param['shour'], param['smin'], 0, 0, 0, -1) ) )
111 param['end'] = int(mktime( (param['year'], param['month'], param['day'], param['ehour'], param['emin'], 0, 0, 0, -1) ) )
112 if param['end'] < param['begin']:
113 param['end'] += 86400
114 for element in listDate:
117 if param['sRef'] is None:
118 return False, "Missing Parameter: sRef"
120 takeApart = split(param['sRef'], '|')
121 if len(takeApart) > 1:
122 param['sRef'] = takeApart[1]
125 if param.has_key('repeated'):
126 repeated = int(param['repeated'])
128 list = ["mo","tu","we","th","fr","sa","su","ms","mf"]
130 if param.has_key(element):
131 number = param[element] or 0
133 repeated = repeated + int(number)
136 param['repeated'] = repeated
138 if param['command'] == "add":
140 return self.editTimer(param)
141 elif param['command'] == "del":
143 return self.delTimer(param)
144 elif param['command'] == "change":
146 return self.editTimer(param)
148 return False, "Unknown command: '%s'" %param['command']
150 def recordNow(self,param):
151 print "recordNow ",param
154 if param == "undefinitely":
157 serviceref = self.session.nav.getCurrentlyPlayingServiceReference()
159 #assert isinstance(serviceref, ServiceReference)
160 serviceref = ServiceReference(serviceref.toString())
163 service = self.session.nav.getCurrentService()
164 event = self.epgcache.lookupEventTime(serviceref, -1, 0)
166 info = service.info()
167 ev = info.getEvent(0)
173 end = begin + 3600 * 10
174 name = "instant record"
178 if event is not None:
179 curEvent = parseEvent(event)
181 description = curEvent[3]
182 eventid = curEvent[4]
187 return False, "No event found, started infinite recording"
189 location = config.movielist.last_videodir.value
190 timer = RecordTimerEntry(serviceref, begin, end, name, description, eventid, False, False, 0, dirname = location)
191 timer.dontSave = True
192 self.recordtimer.record(timer)
194 return True, "Instant recording started"
197 #===============================================================================
198 # This Function can add a new or edit an exisiting Timer.
199 # When the Parameter "deleteOldOnSave" is not set, a new Timer will be added.
200 # Otherwise, and if the parameters channelOld, beginOld and endOld are set,
201 # an existing timer with corresponding values will be changed.
202 #===============================================================================
203 def editTimer(self, param):
204 print "[WebComponents.Timer] editTimer"
206 #OK first we need to parse all of your Parameters
207 #For some of them (like afterEvent or justplay) we can use default values
208 #for others (the serviceReference or the Begin/End time of the timer
209 #we have to quit if they are not set/have illegal values
211 if param.has_key('sRef'):
212 service_ref = ServiceReference(param['sRef'])
214 return False, "Missing Parameter: sRef"
217 if param.has_key('repeated'):
218 repeated = int(param['repeated'])
221 if param.has_key('begin'):
222 begin = int(param['begin'])
225 elif time() > int(begin) and repeated == 0:
228 return False, "Illegal Parameter value for Parameter begin : '%s'" %begin
230 return False, "Missing Parameter: begin"
232 if param.has_key('end'):
233 end = int(param['end'])
235 return False, "Missing Parameter: end"
237 if param.has_key('name'):
240 return False, "Missing Parameter: name"
242 if param.has_key('description'):
243 description = param['description'].replace("\n", " ")
245 return False, "Missing Parameter: description"
247 disabled = False #Default to: Enabled
248 if param.has_key('disabled'):
249 if param['disabled'] == "1":
252 #TODO - maybe we can give the user some useful hint here
255 justplay = False #Default to: Record
256 if param.has_key('justplay'):
257 if param['justplay'] == "1":
260 afterEvent = 3 #Default to Afterevent: Auto
261 if param.has_key('afterevent'):
262 if ( param['afterevent'] == "0") or (param['afterevent'] == "1") or (param['afterevent'] == "2"):
263 afterEvent = int(param['afterevent'])
265 dirname = config.movielist.last_timer_videodir.value
266 if param.has_key('dirname'):
267 dirname = param['dirname']
270 if param.has_key('tags'):
271 tags = unescape(param['tags']).split(' ')
274 if param.has_key('deleteOldOnSave'):
275 delold = int(param['deleteOldOnSave'])
277 #Try to edit an existing Timer
279 if param.has_key('channelOld') and param['channelOld'] != '':
280 channelOld = ServiceReference(param['channelOld'])
282 return False, "Missing Parameter: channelOld"
283 # We do need all of the following Parameters, too, for being able of finding the Timer.
284 # Therefore so we can neither use default values in this part nor can we
285 # continue if a parameter is missing
286 if param.has_key('beginOld'):
287 beginOld = int(param['beginOld'])
289 return False, "Missing Parameter: beginOld"
291 if param.has_key('endOld'):
292 endOld = int(param['endOld'])
294 return False, "Missing Parameter: endOld"
296 #let's try to find the timer
298 for timer in self.recordtimer.timer_list + self.recordtimer.processed_timers:
299 if str(timer.service_ref) == str(channelOld):
300 if int(timer.begin) == beginOld:
301 if int(timer.end) == endOld:
302 #we've found the timer we've been searching for
303 #Let's apply the new values
304 timer.service_ref = service_ref
305 timer.begin = int(begin)
308 timer.description = description
309 timer.disabled = disabled
310 timer.justplay = justplay
311 timer.afterEvent = afterEvent
312 timer.repeated = repeated
313 timer.dirname = dirname
316 #send the changed timer back to enigma2 and hope it's good
317 self.session.nav.RecordTimer.timeChanged(timer)
318 print "[WebComponents.Timer] editTimer: Timer changed!"
319 return True, "Timer %s has been changed!" %(timer.name)
321 #obviously some value was not good, return an error
322 return False, "Changing the timer for '%s' failed!" %name
324 return False, "Could not find timer '%s' with given start and end time!" %name
326 #Try adding a new Timer
329 #Create a new instance of recordtimerentry
330 timer = RecordTimerEntry(service_ref, begin, end, name, description, 0, disabled, justplay, afterEvent, dirname = dirname, tags = tags)
331 timer.repeated = repeated
333 self.recordtimer.record(timer)
334 return True, "Timer added successfully!"
336 #something went wrong, most possibly one of the given paramater-values was wrong
337 return False, "Could not add timer '%s'!" %name
339 return False, "Unexpected Error"
342 def addTimerByEventID(self, param):
343 print "[WebComponents.Timer] addTimerByEventID", param
344 if param['sRef'] is None:
345 return False, "Missing Parameter: sRef"
346 if param['eventid'] is None:
347 return False, "Missing Parameter: eventid"
350 if param['justplay'] is not None:
351 if param['justplay'] == "1":
354 location = config.movielist.last_timer_videodir.value
355 if param['dirname'] is not None and param['dirname'] != "":
356 location = param['dirname']
358 if param['tags'] is not None and param['tags'] != "":
361 epgcache = eEPGCache.getInstance()
362 event = epgcache.lookupEventId(eServiceReference(param['sRef']),int(param['eventid']))
364 return False, "EventId not found"
366 (begin, end, name, description, eit) = parseEvent(event)
368 timer = RecordTimerEntry(ServiceReference(param['sRef']), begin , end, name, description, eit, False, justplay, AFTEREVENT.NONE, dirname=location, tags=tags)
369 self.recordtimer.record(timer)
370 return True, "Timer '%s' added" %(timer.name)
373 def writeTimerList(self, force=False):
374 # is there an easier and better way? :\
375 if config.plugins.Webinterface.autowritetimer.value or force:
376 print "Timer.py writing timer to flash"
377 self.session.nav.RecordTimer.saveTimer()
378 return True, "TimerList was saved "
380 return False, "TimerList was not saved "
384 print "[WebComponents.Timer] result: ", self.result
385 (result, text) = self.result
386 xml = "<e2simplexmlresult>\n"
388 xml += "<e2state>True</e2state>\n"
390 xml += "<e2state>False</e2state>\n"
391 xml += "<e2statetext>%s</e2statetext>\n" % text
392 xml += "</e2simplexmlresult>\n"
395 text = property(getText)
397 ## part for listfiller requests
401 for item in self.recordtimer.timer_list + self.recordtimer.processed_timers:
403 timer.append(item.service_ref)
404 timer.append(item.service_ref.getServiceName())
405 timer.append(item.eit)
406 timer.append(item.name)
407 timer.append(item.description)
409 if item.disabled is True:
414 timer.append(item.begin)
415 timer.append(item.end)
416 timer.append(item.end - item.begin)
417 timer.append(item.start_prepare)
419 if item.justplay is True:
424 timer.append(item.afterEvent)
426 timer.append(item.dirname)
427 timer.append(" ".join(item.tags))
429 timer.append(item.log_entries)
432 timer.append(item.Filename)
436 timer.append(item.backoff)
439 timer.append(item.next_activation)
443 timer.append(item.first_try_prepare)
444 timer.append(item.state)
445 timer.append(item.repeated)
447 if item.dontSave is True:
452 timer.append(item.cancelled)
454 if item.eit is not None:
455 event = self.epgcache.lookupEvent(['EX',("%s" % item.service_ref ,2,item.eit)])
456 if event[0][0] is not None:
457 timer.append(event[0][0])
464 if item.disabled is True:
471 timerlist.append(timer)
475 list = property(command)
477 "ServiceReference":0,
495 "firstTryPrepare":18,
500 "DescriptionExtended":23,
502 "toggleDisabledIMG":25,