Building a symlink tree for MythTV recordings

    I wanted to build a directory of symlinks that pointed to my MythTV recordings, so I wrote a little python script to do it for me. I figure someone else might find this useful too...

      #!/usr/bin/python
      
      # Copyright (C) Michael Still (mikal@stillhq.com) 2007
      # Released under the terms of the GNU GPL
      
      import MySQLdb
      import os
      import re
      
      from socket import gethostname
      
      # Connect to the MythTV database based on the MythTV config
      config_values = {}
      home = os.environ.get('HOME')
      config = open(home + '/.mythtv/mysql.txt')
      for line in config.readlines():
        if not line.startswith('#') and len(line) > 5:
          (key, value) = line.rstrip('\n').split('=')
          config_values[key] = value
      
      db_connection = MySQLdb.connect(host = config_values['DBHostName'],
                                      user = config_values['DBUserName'],
                                      passwd = config_values['DBPassword'],
                                      db = config_values['DBName'])
      cursor = db_connection.cursor(MySQLdb.cursors.DictCursor)
      
      # Regexp for what is allowed in the symlink name
      unsafe = re.compile('[^a-zA-Z0-9\-\:_]+')
      
      # Find the recordings directory -- this assumes you haven't used an
      # identifier string for this machine...
      cursor.execute('select * from settings where value="RecordFilePrefix" and '
                     'hostname="%s";' % gethostname())
      row = cursor.fetchone()
      basedir = row['data']
      
      # Now find all the recordings we have at the moment
      cursor.execute('select title, subtitle, starttime, basename from recorded;')
      
      for i in range(cursor.rowcount):
        row = cursor.fetchone()
      
        title = row['title']
        subtitle = row['subtitle']
        if subtitle == '':
          subtitle = str(row['starttime'])
      
        title = title.replace(' ', '_')
        title = unsafe.sub('', title)
      
        subtitle = subtitle.replace(' ', '_')
        subtitle = unsafe.sub('', subtitle)
      
        extn = row['basename'].split('.')[1]
      
        try:
          os.mkdir(title)
        except:
          pass
      
        try:
          os.symlink('%s/%s' %(basedir, row['basename']),
                     '%s/%s.%s' %(title, subtitle, extn))
        except:
          pass
      


    This creates a tree of symlinks in the current directory that looks like this:

      $ find . -type l
      ./Masterminds/Air_America
      ./Tech_Now/Getting_Famous
      ./Babar/To_Tell_or_Not_to_Tell
      ./Babar/The_Intruder
      ./Babar/Special_Delivery
      ./Babar/Conga_the_Terrible
      ./Babar/The_Celestville_Enquirer
      ./The_Backyardigans/Viking_Voyage
      ./The_Backyardigans/Cops_and_Robots
      ./The_Backyardigans/Eureka
      ./The_Backyardigans/Movers_of_Arabia
      ./The_Backyardigans/Save_the_Day
      ./Go_Diego_Go/Rainforest_Race
      ./Go_Diego_Go/Cool_Water_for_Ana_the_Anaconda
      ./Go_Diego_Go/Great_Jaguar_to_the_Rescue
      ./Go_Diego_Go/Diego_Saves_the_River_Dolphin
      ./Go_Diego_Go/Jorge_the_Little_Hawk_Learns_to_Migrate
      ./How_Its_Made/2007-06-08_09:30:00
      


    Tags for this post: mythtv symlink script python
    Related posts: Python DNS modules; Learning Python; Rsyncing everything but the data; Example 2.1 from Dive Into Python; Calculating a SSH host key with paramiko; Python effective TLD library

posted at: 21:35 | path: /mythtv | permanent link to this entry