]> code.delx.au - mediapc-tools/blob - mythlib.py
MythTV maintenance scripts
[mediapc-tools] / mythlib.py
1 #! python3
2
3 import os
4 import socket
5
6 import lxml.etree
7 import MySQLdb
8
9 def get_config():
10 with open(os.path.expanduser("~/.mythtv/config.xml")) as f:
11 return lxml.etree.parse(f, lxml.etree.XMLParser(encoding="utf-8"))
12
13 def get_db_connection(config_xml):
14 return MySQLdb.connect(
15 host = config_xml.xpath("/Configuration/Database/Host/text()")[0],
16 port = int(config_xml.xpath("/Configuration/Database/Port/text()")[0]),
17 user = config_xml.xpath("/Configuration/Database/UserName/text()")[0],
18 passwd = config_xml.xpath("/Configuration/Database/Password/text()")[0],
19 db = config_xml.xpath("/Configuration/Database/DatabaseName/text()")[0],
20 )
21
22 def get_recordings_dir(config_xml, db_connection):
23 try:
24 localhostname = config_xml.xpath("/Configuration/LocalHostName/text()")[0]
25 except IndexError:
26 localhostname = socket.gethostname()
27
28 with db_connection.cursor(MySQLdb.cursors.DictCursor) as cursor:
29 cursor.execute("""
30 SELECT * FROM settings
31 WHERE value='RecordFilePrefix' AND hostname='%s'
32 """ % localhostname)
33
34 return cursor.fetchone()["data"]