]>
code.delx.au - mediapc-tools/blob - mythsymlink
a8391ac8a2b76b3cb6f10c248e09f4065d5e854f
13 # Remove any symlinks and empty dirs in the tree
14 for dirpath
, dirnames
, filenames
in os
.walk(".", topdown
=False):
15 for filename
in filenames
:
16 filename
= os
.path
.join(dirpath
, filename
)
17 if os
.path
.islink(filename
):
19 for dirname
in dirnames
:
20 dirname
= os
.path
.join(dirpath
, dirname
)
23 # Connect to the MythTV database based on the MythTV config
24 with
open(os
.path
.expanduser("~/.mythtv/config.xml")) as f
:
25 config_xml
= lxml
.etree
.parse(f
, lxml
.etree
.XMLParser(encoding
="utf-8"))
27 db_connection
= MySQLdb
.connect(
28 host
= config_xml
.xpath("/Configuration/Database/Host/text()")[0],
29 port
= int(config_xml
.xpath("/Configuration/Database/Port/text()")[0]),
30 user
= config_xml
.xpath("/Configuration/Database/UserName/text()")[0],
31 passwd
= config_xml
.xpath("/Configuration/Database/Password/text()")[0],
32 db
= config_xml
.xpath("/Configuration/Database/DatabaseName/text()")[0],
34 cursor
= db_connection
.cursor(MySQLdb
.cursors
.DictCursor
)
36 # Regexp for what is allowed in the symlink name
37 unsafe
= re
.compile("[^a-zA-Z0-9\-_ ,\\.]+")
40 localhostname
= config_xml
.xpath("/Configuration/LocalHostName/text()")[0]
42 localhostname
= socket
.gethostname()
44 # Find the recordings directory
46 SELECT * FROM settings
47 WHERE value='RecordFilePrefix' AND hostname='%s'
49 recordingsdir
= cursor
.fetchone()["data"]
51 # Now find all the recordings we have at the moment
53 SELECT title, subtitle, starttime, basename, watched FROM recorded
57 starttime
= str(row
["starttime"]).replace(":", "-")
58 subtitle
= row
["subtitle"]
59 basename
= row
["basename"]
60 watched
= bool(row
["watched"])
62 title
= unsafe
.sub("", title
)
63 subtitle
= unsafe
.sub("", subtitle
)
64 extn
= os
.path
.splitext(basename
)[1]
68 filename
= "%s - %s%s" % (starttime
, subtitle
, extn
)
70 filename
= "%s%s" % (starttime
, extn
)
72 filename
= "watched/" + filename
74 source
= "%s/%s" % (recordingsdir
, basename
)
75 dest
= "%s/%s" % (title
, filename
)
77 if not os
.path
.isfile(source
):
80 dirnames
= dest
.split("/")[:-1]
81 for i
in xrange(1, len(dirnames
)+1):
82 dirname
= "/".join(dirnames
[:i
])
83 if not os
.path
.isdir(dirname
):
86 os
.symlink(source
, dest
)