annotate python/dlna.py @ 865:20aa4a6ef719

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