Don't record something we are already recording today.
[recordtv.git] / src / rtv_schedule.py
blobb3eaeeb2b0dcdb81b2b29b8863cc11b740faaad7
1 #!/usr/bin/python
3 import time, datetime, os, re
4 import rtv_favourite, rtv_utils, rtv_programmeinfo
5 import rtv_propertiesfile, rtv_selection
7 # TODO: delete old files:
8 # - scheduled recordings
9 # - logs
10 # - selections
12 MESSAGE_TIME_FORMAT = "%H:%M on %a"
14 at_output_re = re.compile( "job (\d+) at .*\n" )
16 def priority_time_compare( pi1, pi2 ):
17 pi1Pri = pi1.get_priority()
18 pi2Pri = pi2.get_priority()
20 if pi1Pri > pi2Pri:
21 return -1
22 elif pi1Pri < pi2Pri:
23 return 1
24 elif pi1.startTime < pi2.startTime:
25 return -1
26 elif pi1.startTime > pi2.startTime:
27 return 1
29 return 0
31 class Schedule:
33 def __init__( self, config ):
34 self.config = config
36 def sub_title_matters( self, proginfo ):
37 return ( proginfo.sub_title is not None
38 and proginfo.unique_subtitles == True )
40 def add_to_old_progs_map( self, dr, fn, ret, delete_unused ):
41 if not fn.endswith( ".rtvinfo" ):
42 return
44 full_path = os.path.join( dr, fn )
45 proginfo = rtv_programmeinfo.ProgrammeInfo()
46 proginfo.load( full_path )
48 if not self.sub_title_matters( proginfo ):
49 if delete_unused:
50 os.remove( full_path )
51 return
53 if proginfo.title not in ret:
54 ret[proginfo.title] = {}
56 if proginfo.sub_title not in ret[proginfo.title]:
57 ret[proginfo.title][proginfo.sub_title] = 1
59 def find_old_programmes_map( self, old_dir, converted_dir ):
60 ret = {}
62 dr_list = os.listdir( old_dir )
63 for fn in dr_list:
64 self.add_to_old_progs_map( old_dir, fn, ret, True )
66 for (dirpath, dirnames, filenames) in os.walk( converted_dir ):
67 for fn in filenames:
68 self.add_to_old_progs_map( dirpath, fn, ret, False )
70 return ret
72 def get_at_job( self, at_output ):
73 for ln in at_output:
74 m = at_output_re.match( ln )
75 if m:
76 return m.group( 1 )
77 print ( "Unable to understand at command output '%s' - "
78 + "can't create a scheduled_events entry" ) % at_output
79 return None
81 def print_already_recorded( self, prog ):
82 print ( "Not recording '%s : %s' - we have recorded it in the past."
83 % ( prog.title, prog.sub_title ) )
85 def print_recording_today( self, prog ):
86 print ( "Not recording '%s : %s' - we are recording it today already."
87 % ( prog.title, prog.sub_title ) )
89 def remove_already_recorded( self, scheduler ):
90 old_dir = os.path.join( self.config.recorded_progs_dir, "old" )
91 converted_dir = self.config.converted_progs_dir
92 old_progs_map = scheduler.find_old_programmes_map( old_dir,
93 converted_dir )
95 new_queue = []
96 new_queue_sub_titles_map = {}
98 for prog in self.record_queue:
99 if ( prog.title in old_progs_map and
100 prog.sub_title in old_progs_map[prog.title] ):
101 self.print_already_recorded( prog )
102 elif ( prog.title in new_queue_sub_titles_map and
103 prog.sub_title in new_queue_sub_titles_map[prog.title] ):
104 self.print_recording_today( prog )
105 else:
106 new_queue.append( prog )
107 if self.sub_title_matters( prog ):
108 new_queue_sub_titles_map[prog.title] = prog.sub_title
110 self.record_queue = new_queue
113 def print_clash_priority_error( self, losePi, keepPi ):
114 print ( ("Not recording '%s' at %s - it clashes with '%s',"
115 + " which is higher priority (%d > %d).")
116 % ( losePi.title,
117 losePi.startTime.strftime(
118 MESSAGE_TIME_FORMAT ),
119 keepPi.title, keepPi.get_priority(),
120 losePi.get_priority() ) )
122 def print_clash_time_error( self, losePi, keepPi ):
123 print ( ("Not recording '%s' at %s - it clashes with '%s',"
124 + " which has the same priority (%d), but starts earlier"
125 + " (%s).")
126 % ( losePi.title,
127 losePi.startTime.strftime(
128 MESSAGE_TIME_FORMAT ),
129 keepPi.title, keepPi.get_priority(),
130 keepPi.startTime.strftime(
131 MESSAGE_TIME_FORMAT ) ) )
133 def print_clash_same_time( self, losePi, keepPi ):
134 print ( ("Not recording '%s' at %s - it clashes with '%s',"
135 + " which has the same priority (%d). They start at"
136 + " the same time, so the one to record was chosen"
137 + " randomly.")
138 % ( losePi.title,
139 losePi.startTime.strftime(
140 MESSAGE_TIME_FORMAT ),
141 keepPi.title, keepPi.get_priority() ) )
144 def remove_clashes( self ):
145 self.record_queue.sort( priority_time_compare )
146 new_queue = []
148 for pi1Num in range( len( self.record_queue ) ):
149 pi1 = self.record_queue[pi1Num]
151 clashed_with = None
153 for pi2 in new_queue:
155 if pi1.clashes_with( pi2 ):
156 clashed_with = pi2
157 break
159 if not clashed_with:
160 new_queue.append( pi1 )
161 else:
162 if pi1.get_priority() < clashed_with.get_priority():
163 self.print_clash_priority_error( pi1, clashed_with )
164 elif pi1.startTime > clashed_with.startTime:
165 self.print_clash_time_error( pi1, clashed_with )
166 else:
167 self.print_clash_same_time( pi1, clashed_with )
169 self.record_queue = new_queue
171 def schedule_recordings( self, queue ):
173 if len( queue ) == 0:
174 print "No programmes found to record."
175 return
177 rtv_utils.ensure_dir_exists( self.config.recorded_progs_dir )
178 rtv_utils.ensure_dir_exists( self.config.scheduled_events_dir )
179 rtv_utils.ensure_dir_exists( self.config.recording_log_dir )
181 for programmeInfo in queue:
183 print "Recording '%s' at '%s'" % ( programmeInfo.title,
184 programmeInfo.startTime.strftime( MESSAGE_TIME_FORMAT ) )
186 filename = rtv_utils.prepare_filename( programmeInfo.title )
187 filename += programmeInfo.startTime.strftime( "-%Y-%m-%d_%H_%M" )
189 length_timedelta = programmeInfo.endTime - programmeInfo.startTime
190 length_in_seconds = ( ( length_timedelta.days
191 * rtv_utils.SECS_IN_DAY ) + length_timedelta.seconds )
192 length_in_seconds += 60 * self.config.extra_recording_time_mins
194 sched_filename = os.path.join( self.config.scheduled_events_dir,
195 filename + ".rtvinfo" )
197 outfilename = os.path.join( self.config.recorded_progs_dir,
198 filename )
199 cmds_array = ( "at",
200 programmeInfo.startTime.strftime( "%H:%M %d.%m.%Y" ) )
201 at_output = rtv_utils.run_command_feed_input( cmds_array,
202 self.config.record_start_command % (
203 self.channel_xmltv2tzap.get_value( programmeInfo.channel ),
204 outfilename, length_in_seconds, sched_filename,
205 os.path.join( self.config.recording_log_dir,
206 filename + ".log" ) ) )
207 at_job_start = self.get_at_job( at_output )
209 if at_job_start != None:
210 programmeInfo.atJob = at_job_start
211 programmeInfo.save( sched_filename )
213 def sax_callback( self, pi ):
214 for fav in self.favs_and_sels:
215 if fav.matches( pi ):
216 dtnow = datetime.datetime.today()
217 if( pi.startTime > dtnow
218 and (pi.startTime - dtnow).days < 1 ):
220 if fav.deleteAfterDays:
221 pi.deleteTime = pi.endTime + datetime.timedelta(
222 float( fav.deleteAfterDays ), 0 )
224 pi.channel_pretty = self.channel_xmltv2tzap.get_value(
225 pi.channel )
226 if not pi.channel_pretty:
227 print (
228 "Pretty channel name not found for channel %s"
229 % pi.channel )
231 pi.priority = fav.priority
232 pi.destination = fav.destination
233 pi.unique_subtitles = fav.unique_subtitles
235 if fav.real_title:
236 pi.title = fav.real_title
238 self.record_queue.append( pi )
239 break
241 def remove_scheduled_events( self ):
243 rtv_utils.ensure_dir_exists( self.config.scheduled_events_dir )
245 for fn in os.listdir( self.config.scheduled_events_dir ):
246 full_fn = os.path.join( self.config.scheduled_events_dir, fn )
248 pi = rtv_programmeinfo.ProgrammeInfo()
249 pi.load( full_fn )
251 done_atrm = False
252 try:
253 at_job_start = int( pi.atJob )
254 rtv_utils.run_command( ( "atrm", str( at_job_start ) ) )
255 done_atrm = True
256 except ValueError:
257 pass
259 if done_atrm:
260 os.unlink( full_fn )
262 def delete_pending_recordings( self ):
263 rtv_utils.ensure_dir_exists( self.config.recorded_progs_dir )
265 dttoday = datetime.datetime.today()
267 for fn in os.listdir( self.config.recorded_progs_dir ):
268 if fn[-4:] == ".flv":
270 infofn = os.path.join( self.config.recorded_progs_dir,
271 fn[:-4] + ".rtvinfo" )
273 if os.path.isfile( infofn ):
275 pi = rtv_programmeinfo.ProgrammeInfo()
276 pi.load( infofn )
278 if pi.deleteTime and pi.deleteTime < dttoday:
279 full_fn = os.path.join(
280 self.config.recorded_progs_dir, fn )
282 print "Deleting file '%s'" % full_fn
284 os.unlink( full_fn )
285 os.unlink( infofn )
289 def schedule( self, xmltv_parser = rtv_utils, fav_reader = rtv_selection,
290 scheduler = None ):
292 if scheduler == None:
293 scheduler = self
295 # TODO: if we are generating a TV guide as well as scheduling,
296 # we should share the same XML parser.
298 self.delete_pending_recordings()
300 self.favs_and_sels = fav_reader.read_favs_and_selections(
301 self.config, record_only = True )
303 self.channel_xmltv2tzap = rtv_propertiesfile.PropertiesFile()
304 self.channel_xmltv2tzap.load( self.config.channel_xmltv2tzap_file )
306 scheduler.remove_scheduled_events()
308 self.record_queue = []
309 xmltv_parser.parse_xmltv_files( self.config, self.sax_callback )
310 self.remove_already_recorded( scheduler )
311 self.remove_clashes()
312 scheduler.schedule_recordings( self.record_queue )
313 self.record_queue = []
316 def schedule( config ):
317 sch = Schedule( config )
318 sch.schedule()
324 # === Test code ===
326 def format_title_subtitle( pi ):
327 ret = pi.title
328 if pi.sub_title:
329 ret += " : "
330 ret += pi.sub_title
331 return ret
333 def format_pi_list( qu ):
334 ret = "[ "
335 for pi in qu[:-1]:
336 ret += format_title_subtitle( pi )
337 ret += ", "
338 if len( qu ) > 0:
339 ret += format_title_subtitle( qu[-1] )
340 ret += " ]"
342 return ret
344 class FakeScheduler( object ):
346 def __init__( self, schedule ):
348 self.schedule = schedule
349 self.remove_scheduled_events_called = False
351 dtnow = datetime.datetime.today()
353 favHeroes = rtv_favourite.Favourite()
354 favHeroes.title_re = "Heroes"
356 favNewsnight = rtv_favourite.Favourite()
357 favNewsnight.title_re = "Newsnight.*"
358 favNewsnight.priority = -50
360 favLost = rtv_favourite.Favourite()
361 favLost.title_re = "Lost"
362 favLost.priority = -50
364 favPocoyo = rtv_favourite.Favourite()
365 favPocoyo.title_re = "Pocoyo"
367 self.test_favs = [ favHeroes, favNewsnight, favLost, favPocoyo ]
369 self.piHeroes = rtv_programmeinfo.ProgrammeInfo()
370 self.piHeroes.title = "Heroes"
371 self.piHeroes.startTime = dtnow + datetime.timedelta( hours = 1 )
372 self.piHeroes.endTime = dtnow + datetime.timedelta( hours = 2 )
373 self.piHeroes.channel = "south-east.bbc2.bbc.co.uk"
375 self.piNewsnight = rtv_programmeinfo.ProgrammeInfo()
376 self.piNewsnight.title = "Newsnight"
377 self.piNewsnight.startTime = dtnow + datetime.timedelta( hours = 1,
378 minutes = 30 )
379 self.piNewsnight.endTime = dtnow + datetime.timedelta( hours = 2 )
380 self.piNewsnight.channel = "south-east.bbc1.bbc.co.uk"
382 self.piNR = rtv_programmeinfo.ProgrammeInfo()
383 self.piNR.title = "Newsnight Review"
384 self.piNR.startTime = dtnow + datetime.timedelta( hours = 2 )
385 self.piNR.endTime = dtnow + datetime.timedelta( hours = 2,
386 minutes = 30 )
387 self.piNR.channel = "south-east.bbc2.bbc.co.uk"
389 self.piLost = rtv_programmeinfo.ProgrammeInfo()
390 self.piLost.title = "Lost"
391 self.piLost.startTime = dtnow + datetime.timedelta( hours = 1,
392 minutes = 25 )
393 self.piLost.endTime = dtnow + datetime.timedelta( hours = 2,
394 minutes = 30 )
395 self.piLost.channel = "channel4.com"
397 self.piTurnip = rtv_programmeinfo.ProgrammeInfo()
398 self.piTurnip.title = "Newsnight Turnip"
399 self.piTurnip.startTime = dtnow + datetime.timedelta( hours = 1,
400 minutes = 30 )
401 self.piTurnip.endTime = dtnow + datetime.timedelta( hours = 2,
402 minutes = 30 )
403 self.piTurnip.channel = "channel5.co.uk"
405 self.piPocoyo1 = rtv_programmeinfo.ProgrammeInfo()
406 self.piPocoyo1.title = "Pocoyo"
407 self.piPocoyo1.sub_title = "Subtitle already seen"
408 self.piPocoyo1.startTime = dtnow + datetime.timedelta( hours = 1 )
409 self.piPocoyo1.endTime = dtnow + datetime.timedelta( hours = 2 )
410 self.piPocoyo1.channel = "south-east.bbc2.bbc.co.uk"
412 self.piPocoyo2 = rtv_programmeinfo.ProgrammeInfo()
413 self.piPocoyo2.title = "Pocoyo"
414 self.piPocoyo2.sub_title = "Subtitle not seen"
415 self.piPocoyo2.startTime = dtnow + datetime.timedelta( hours = 2 )
416 self.piPocoyo2.endTime = dtnow + datetime.timedelta( hours = 3 )
417 self.piPocoyo2.channel = "south-east.bbc2.bbc.co.uk"
419 self.which_test = None
421 def find_old_programmes_map( self, old_dir, converted_dir ):
422 return { self.piPocoyo1.title : { self.piPocoyo1.sub_title : 1 } }
424 def parse_xmltv_files( self, config, callback ):
425 if self.which_test == "no_clash1":
426 callback( self.piNewsnight )
427 callback( self.piNR )
428 elif self.which_test == "priority_clash1":
429 callback( self.piHeroes )
430 callback( self.piNewsnight )
431 elif self.which_test == "time_clash1":
432 callback( self.piNewsnight )
433 callback( self.piLost )
434 elif self.which_test == "same_clash1":
435 callback( self.piNewsnight )
436 callback( self.piTurnip )
437 elif self.which_test == "norerecord":
438 callback( self.piPocoyo1 )
439 callback( self.piPocoyo2 )
441 def remove_scheduled_events( self ):
442 if self.remove_scheduled_events_called:
443 raise Exception( "remove_scheduled_events called twice." )
444 self.remove_scheduled_events_called = True
446 def read_favs_and_selections( self, config, record_only ):
447 return self.test_favs
449 def schedule_recordings( self, queue ):
450 self.queue = queue
452 def test( self ):
453 self.which_test = "priority_clash1"
454 self.schedule.schedule( self, self, self )
455 if not self.remove_scheduled_events_called:
456 raise Exception( "remove_scheduled_events never called" )
457 if self.queue != [self.piHeroes]:
458 raise Exception( "queue should look like %s, but it looks like %s"
459 % ( [self.piHeroes], self.queue ) )
462 self.which_test = "no_clash1"
463 self.remove_scheduled_events_called = False
464 self.schedule.schedule( self, self, self )
465 if not self.remove_scheduled_events_called:
466 raise Exception( "remove_scheduled_events never called" )
467 if self.queue != [self.piNewsnight, self.piNR]:
468 raise Exception( "queue should look like %s, but it looks like %s"
469 % ( [self.piNewsnight, self.piNR], self.queue ) )
471 self.which_test = "time_clash1"
472 self.remove_scheduled_events_called = False
473 self.schedule.schedule( self, self, self )
474 if not self.remove_scheduled_events_called:
475 raise Exception( "remove_scheduled_events never called" )
476 if self.queue != [self.piLost]:
477 raise Exception( "queue should look like %s, but it looks like %s"
478 % ( [self.piLost], self.queue ) )
480 self.which_test = "same_clash1"
481 self.remove_scheduled_events_called = False
482 self.schedule.schedule( self, self, self )
483 if not self.remove_scheduled_events_called:
484 raise Exception( "remove_scheduled_events never called" )
485 if self.queue != [self.piNewsnight] and self.queue != [self.piTurnip]:
486 raise Exception( ("queue should look like %s or %s, but it"
487 + " looks like %s" )
488 % ( format_pi_list( [self.piNewsnight] ),
489 format_pi_list( [self.piTurnip] ),
490 format_pi_list( self.queue ) ) )
492 def test_norerecord( self ):
493 self.which_test = "norerecord"
494 self.remove_scheduled_events_called = False
495 self.schedule.schedule( self, self, self )
497 if not self.remove_scheduled_events_called:
498 raise Exception( "remove_scheduled_events never called" )
499 if self.queue != [self.piPocoyo2]:
500 raise Exception( ("queue should look like %s, but it"
501 + " looks like %s" )
502 % ( format_pi_list( [self.piPocoyo2] ),
503 format_pi_list( self.queue ) ) )
507 class FakeScheduler_SameTitleSameDay( FakeScheduler ):
509 def __init__( self, schedule ):
510 FakeScheduler.__init__( self, schedule )
512 def find_old_programmes_map( self, old_dir, converted_dir ):
513 return {}
515 def parse_xmltv_files( self, config, callback ):
516 dtnow = datetime.datetime.today()
518 # 2 with no subtitle
519 self.piPocoyo1 = rtv_programmeinfo.ProgrammeInfo()
520 self.piPocoyo1.title = "Pocoyo"
521 self.piPocoyo1.startTime = dtnow + datetime.timedelta( hours = 1 )
522 self.piPocoyo1.endTime = dtnow + datetime.timedelta( hours = 2 )
523 self.piPocoyo1.channel = "south-east.bbc2.bbc.co.uk"
525 self.piPocoyo2 = rtv_programmeinfo.ProgrammeInfo()
526 self.piPocoyo2.title = "Pocoyo"
527 self.piPocoyo2.startTime = dtnow + datetime.timedelta( hours = 2 )
528 self.piPocoyo2.endTime = dtnow + datetime.timedelta( hours = 3 )
529 self.piPocoyo2.channel = "south-east.bbc2.bbc.co.uk"
531 # 2 with identical subtitle
532 self.piPocoyo3 = rtv_programmeinfo.ProgrammeInfo()
533 self.piPocoyo3.title = "Pocoyo"
534 self.piPocoyo3.sub_title = "Subtitle we will see twice"
535 self.piPocoyo3.startTime = dtnow + datetime.timedelta( hours = 3 )
536 self.piPocoyo3.endTime = dtnow + datetime.timedelta( hours = 4 )
537 self.piPocoyo3.channel = "south-east.bbc2.bbc.co.uk"
539 self.piPocoyo4 = rtv_programmeinfo.ProgrammeInfo()
540 self.piPocoyo4.title = "Pocoyo"
541 self.piPocoyo4.sub_title = "Subtitle we will see twice"
542 self.piPocoyo4.startTime = dtnow + datetime.timedelta( hours = 4 )
543 self.piPocoyo4.endTime = dtnow + datetime.timedelta( hours = 5 )
544 self.piPocoyo4.channel = "south-east.bbc2.bbc.co.uk"
546 callback( self.piPocoyo1 )
547 callback( self.piPocoyo2 )
548 callback( self.piPocoyo3 )
549 callback( self.piPocoyo4 )
551 def read_favs_and_selections( self, config, record_only ):
552 favPocoyo = rtv_favourite.Favourite()
553 favPocoyo.title_re = "Pocoyo"
554 return [ favPocoyo ]
556 def test( self ):
557 self.schedule.schedule( self, self, self )
559 if not self.remove_scheduled_events_called:
560 raise Exception( "remove_scheduled_events never called" )
562 expected_queue = [ self.piPocoyo1, self.piPocoyo2, self.piPocoyo3 ]
564 if self.queue != expected_queue:
565 raise Exception( ("queue should look like %s, but it"
566 + " looks like %s" )
567 % ( format_pi_list( [self.piPocoyo3] ),
568 format_pi_list( self.queue ) ) )
571 def test( config ):
572 p = FakeScheduler( Schedule( config ) )
574 p.test()
575 p.test_norerecord()
577 FakeScheduler_SameTitleSameDay( Schedule( config ) ).test()