-import lxml.etree
-import MySQLdb
-
-os.chdir(sys.argv[1])
-
-# Remove any symlinks and empty dirs in the tree
-for dirpath, dirnames, filenames in os.walk(".", topdown=False):
- for filename in filenames:
- filename = os.path.join(dirpath, filename)
- if os.path.islink(filename):
- os.unlink(filename)
- for dirname in dirnames:
- dirname = os.path.join(dirpath, dirname)
- os.rmdir(dirname)
-
-# Connect to the MythTV database based on the MythTV config
-with open(os.path.expanduser("~/.mythtv/config.xml")) as f:
- config_xml = lxml.etree.parse(f, lxml.etree.XMLParser(encoding="utf-8"))
-
-db_connection = MySQLdb.connect(
- host = config_xml.xpath("/Configuration/Database/Host/text()")[0],
- port = int(config_xml.xpath("/Configuration/Database/Port/text()")[0]),
- user = config_xml.xpath("/Configuration/Database/UserName/text()")[0],
- passwd = config_xml.xpath("/Configuration/Database/Password/text()")[0],
- db = config_xml.xpath("/Configuration/Database/DatabaseName/text()")[0],
-)
-cursor = db_connection.cursor(MySQLdb.cursors.DictCursor)
-
-# Regexp for what is allowed in the symlink name
-unsafe = re.compile("[^a-zA-Z0-9\-_ ,\\.]+")
-
-try:
- localhostname = config_xml.xpath("/Configuration/LocalHostName/text()")[0]
-except IndexError:
- localhostname = socket.gethostname()
-
-# Find the recordings directory
-cursor.execute("""
- SELECT * FROM settings
- WHERE value='RecordFilePrefix' AND hostname='%s'
-""" % localhostname)
-recordingsdir = cursor.fetchone()["data"]
-
-# Now find all the recordings we have at the moment
-cursor.execute("""
- SELECT title, subtitle, starttime, basename, watched FROM recorded
-""")
-for row in cursor:
+import mythlib
+
+def main():
+ config_xml = mythlib.get_config()
+ db_connection = mythlib.get_db_connection(config_xml)
+ recordings_dir = mythlib.get_recordings_dir(config_xml, db_connection)
+ symlinks_dir = recordings_dir + "-symlinks"
+
+ os.chdir(symlinks_dir)
+ os.umask(0o22)
+
+ clean_dir()
+
+ for_each_recording(db_connection, handle_recording, recordings_dir)
+
+def clean_dir():
+ for dirpath, dirnames, filenames in os.walk(".", topdown=False):
+ for filename in filenames:
+ filename = os.path.join(dirpath, filename)
+ if os.path.islink(filename):
+ os.unlink(filename)
+ for dirname in dirnames:
+ dirname = os.path.join(dirpath, dirname)
+ os.rmdir(dirname)
+
+def for_each_recording(db_connection, fn, *args):
+ with db_connection.cursor(MySQLdb.cursors.DictCursor) as cursor:
+ cursor.execute("""
+ SELECT
+ title,
+ subtitle,
+ CONVERT_TZ(starttime, 'UTC', '%s') as starttime,
+ basename,
+ watched
+ FROM recorded
+ """ % "Australia/Sydney")
+ for row in cursor:
+ fn(row, *args)
+
+def handle_recording(row, recordings_dir):