Tidied up the recording script to use named variables, and added an optional file...
[recordtv.git] / src / rtv_selection.py
blobd5fd62e0c6e9ddb941a0d0e8fb2051e0097ce230
1 #!/usr/bin/python
3 import re, os
4 import rtv_programmeinfo, rtv_utils, rtv_favourite
6 fn_re = re.compile(
7 "^(\\S*)-(\\d{4}-\\d{2}-\\d{2}_\\d{2}_\\d{2})-(\S*)$" )
9 FILENAME_DATETIME_FORMAT = "%Y-%m-%d_%H_%M"
11 class Selection( object ):
13 def __init__( self ):
14 self.title = None
15 self.channel = None
16 self.startTime = None
17 self.deleteAfterDays = None
18 self.priority = 100
20 def load( self, filename ):
21 m = fn_re.match( filename )
22 if not m:
23 print( "Selection not recognised - filename '%s'" % filename
24 + "is not of the recognised format." )
25 else:
26 self.title = m.group( 1 )
27 self.channel = m.group( 3 )
28 self.startTime = rtv_utils.parse_datetime( m.group( 2 ),
29 FILENAME_DATETIME_FORMAT )
31 def matches( self, pi ):
33 return ( self.title == rtv_utils.prepare_filename( pi.title ) and
34 self.channel == pi.channel and
35 self.startTime == pi.startTime )
39 def read_favs_and_selections( config, record_only = False ):
40 ans = []
42 rtv_utils.ensure_dir_exists( config.favourites_dir )
44 for fn in os.listdir( config.favourites_dir ):
46 if fn[-7:] == ".rtvfav":
47 full_fn = os.path.join( config.favourites_dir, fn )
49 fav = rtv_favourite.Favourite()
50 fav.load( full_fn )
52 if ( not record_only ) or ( fav.record == "yes" ):
53 ans.append( fav )
55 rtv_utils.ensure_dir_exists( config.selections_dir )
57 for fn in os.listdir( config.selections_dir ):
59 if fn[-7:] == ".rtvsel":
60 # Note: no need for full path, as the filename itself
61 # contains all the information we need, so we don't need
62 # to read the file at all.
64 sel = Selection()
65 sel.load( fn[:-7] )
67 ans.append( sel )
69 return ans