LinHES Forums
http://forums.linhes.org/

Python class for connecting to Myth DB
http://forums.linhes.org/viewtopic.php?f=6&t=19900
Page 1 of 1

Author:  nickca [ Wed May 27, 2009 12:28 am ]
Post subject:  Python class for connecting to Myth DB

Here's a simple Python class I wrote and use in some custom scripts I've written. It gets the connection information from $HOME/.mythtv/config.xml and uses it to connect to the DB, then returns a MySQLdb.connect object which you can then open a cursor from, etc.

MythTVDB.py
Code:
#!/usr/bin/python
# MythTVDB.py: Class to connect to MythTV database

# Imports
import os, sys
from xml.dom import minidom
import MySQLdb

class MythTVDB:
    def getText(self, nodelist):
        rc = ""
        for node in nodelist:
            if node.nodeType == node.TEXT_NODE:
                rc = rc + node.data
        return rc

    def OpenConnection(self):
        """OpenConnection -- parse the MythTV config file and open a connection to the MySQL database"""
        # Load the text configuration file
        self.config_values = {}
        home = os.environ.get('HOME')
        try:
            filen = open(home + '/.mythtv/config.xml')
            xmlobj = minidom.parse(filen).documentElement
        except:
            print "Could not read MythTV configuration %s" % filen
            sys.exit(1)

        configtags = xmlobj.getElementsByTagName('DefaultBackend')
        for node in configtags:
            hostname = node.getElementsByTagName('DBHostName')
            for hn in hostname:
                self.config_values['DBHostName'] = self.getText(hn.childNodes)
            username = node.getElementsByTagName('DBUserName')
            for un in username:
                self.config_values['DBUserName'] = self.getText(un.childNodes)
            password = node.getElementsByTagName('DBPassword')
            for pw in password:
                self.config_values['DBPassword'] = self.getText(pw.childNodes)
            dbname = node.getElementsByTagName('DBName')
            for db in dbname:
                self.config_values['DBName'] = self.getText(db.childNodes)
       
        # Open the DB connection
        try:
            self.db_connection = MySQLdb.connect(
            host = self.config_values['DBHostName'],
            user = self.config_values['DBUserName'],
            passwd = self.config_values['DBPassword'],
            db = self.config_values['DBName'])
        except:
            print "Could not connect to database"
            sys.exit(1)
        return self.db_connection


Save this as MythTVDB.py and put it somewhere in your Python path or in the same directory as any scripts that use it. Then use it like this (in this example it reads the default MythVideo player from the database):

Code:
from MythTVDB import *

db = MythTVDB()
db_connection = db.OpenConnection()
the_query = "select data from settings where value='VideoDefaultPlayer';"
cursor = db_connection.cursor()
cursor.execute(the_query)
row = cursor.fetchone()
player = row[0]
cursor.close()


Hope this is of some use to some of you. BTW, this will only work on R6, R5.5 and earlier store the MythTV database info in a different format (a plain text file instead of an XML file).

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/