]>
code.delx.au - mediapc-tools/blob - mythsymlink
9 from socket
import gethostname
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
25 for line
in open(os
.path
.expanduser("~/.mythtv/mysql.txt")):
27 if line
and not line
.startswith("#"):
28 (key
, value
) = line
.split("=")
29 config_values
[key
] = value
31 db_connection
= MySQLdb
.connect(
32 host
= config_values
["DBHostName"],
33 user
= config_values
["DBUserName"],
34 passwd
= config_values
["DBPassword"],
35 db
= config_values
["DBName"]
37 cursor
= db_connection
.cursor(MySQLdb
.cursors
.DictCursor
)
39 # Regexp for what is allowed in the symlink name
40 unsafe
= re
.compile("[^a-zA-Z0-9\-_ ,\\.]+")
42 # Find the recordings directory
44 SELECT * FROM settings
45 WHERE value='RecordFilePrefix' AND hostname='%s'
46 """ % socket
.gethostname())
47 recordingsdir
= cursor
.fetchone()["data"]
49 # Now find all the recordings we have at the moment
51 SELECT title, subtitle, starttime, basename, watched FROM recorded
55 starttime
= str(row
["starttime"]).replace(":", "-")
56 subtitle
= row
["subtitle"]
57 basename
= row
["basename"]
58 watched
= bool(row
["watched"])
60 title
= unsafe
.sub("", title
)
61 subtitle
= unsafe
.sub("", subtitle
)
62 extn
= os
.path
.splitext(basename
)[1]
66 filename
= "%s - %s%s" % (starttime
, subtitle
, extn
)
68 filename
= "%s%s" % (starttime
, extn
)
70 filename
= "watched/" + filename
72 source
= "%s/%s" % (recordingsdir
, basename
)
73 dest
= "%s/%s" % (title
, filename
)
75 if not os
.path
.isfile(source
):
78 dirnames
= dest
.split("/")[:-1]
79 for i
in xrange(1, len(dirnames
)+1):
80 dirname
= "/".join(dirnames
[:i
])
81 if not os
.path
.isdir(dirname
):
84 os
.symlink(source
, dest
)