View unanswered posts    View active topics

All times are UTC - 6 hours





Post new topic Reply to topic  [ 1 post ] 
Print view Previous topic   Next topic  
Author Message
Search for:
PostPosted: Wed May 27, 2009 12:28 am 
Offline
Joined: Tue Oct 17, 2006 10:03 pm
Posts: 49
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).


Top
 Profile  
 

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 


All times are UTC - 6 hours




Who is online

Users browsing this forum: No registered users and 58 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group

Theme Created By ceyhansuyu