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...

      # Copyright (C) Michael Still ( 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]
          os.symlink('%s/%s' %(basedir, row['basename']),
                     '%s/%s.%s' %(title, subtitle, extn))

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

      $ find . -type l

    Tags for this post: mythtv symlink script python
    Related posts: Python effective TLD library; Python effective TLD library update; Finding locking deadlocks in python; Python DNS modules; mbot: new hotness in Google Talk bots; mbot: new hotness in Google Talk bots

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