Mercurial > hg > config
annotate python/dlna.py @ 727:03b66f90916f
works
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 02 Dec 2014 13:59:37 -0800 |
parents | 02aec49585ab |
children | af7b427b3b83 |
rev | line source |
---|---|
654 | 1 #!/usr/bin/env python |
2 # -*- coding: utf-8 -*- | |
3 | |
710 | 4 """ |
5 serve DLNA | |
6 """ | |
7 | |
654 | 8 import argparse |
9 import os | |
655 | 10 import shutil |
654 | 11 import subprocess |
12 import sys | |
13 import tempfile | |
14 from which import which | |
15 | |
16 here = os.path.dirname(os.path.realpath(__file__)) | |
17 string = (str, unicode) | |
18 | |
19 def main(args=sys.argv[1:]): | |
710 | 20 """CLI""" |
654 | 21 |
710 | 22 # parse command line |
654 | 23 parser = argparse.ArgumentParser(description=__doc__) |
24 parser.add_argument('--name', dest='name', default='protest servant', | |
25 help="friendly name") | |
26 parser.add_argument('--db-dir', dest='db_dir', | |
27 default=os.path.join(os.environ['HOME'], 'minidlna'), | |
28 help='db directory') | |
29 parser.add_argument('-p', '--port', dest='port', default=8200, type=int, | |
30 help="port") | |
656 | 31 parser.add_argument('-v', '--videos', dest='videos', nargs='+', |
32 help="videos") | |
654 | 33 parser.add_argument('audio', nargs='+') |
34 options = parser.parse_args(args) | |
35 | |
710 | 36 # dlna location |
37 dlna = which('minidlnad') | |
38 if not dlna: | |
39 parser.error("minidlna command not found") | |
40 | |
654 | 41 lines = [('friendly_name', options.name), |
42 ('db_dir', options.db_dir), | |
43 ('log_dir', options.db_dir), | |
44 ('inotify', 'yes'), | |
45 ('enable_tivo', 'yes')] | |
46 lines.extend([('media_dir', 'A,{}'.format(os.path.abspath(d))) | |
47 for d in options.audio]) | |
656 | 48 lines.extend([('media_dir', 'V,{}'.format(os.path.abspath(d))) |
49 for d in options.audio]) | |
654 | 50 config = '\n'.join(['{}={}'.format(*line) for line in lines]) |
51 print (config) | |
52 | |
53 fd, name = tempfile.mkstemp() | |
54 os.write(fd, config) | |
55 os.close(fd) | |
56 | |
712
02aec49585ab
dont pass -d flag; i dont know why
Jeff Hammel <k0scist@gmail.com>
parents:
710
diff
changeset
|
57 command = [dlna, '-f', name, '-p', str(options.port)] |
654 | 58 print (subprocess.list2cmdline(command)) |
59 subprocess.check_call(command) | |
60 | |
61 os.remove(name) | |
655 | 62 shutil.rmtree(options.db_dir) |
654 | 63 |
64 if __name__ == '__main__': | |
65 main() |