Still need to skip "_tivo_4K" sections when building list of found TiVos.
[pyTivo/wmcbrine.git] / pyTivo.py
blobaec92328e45ae723d1e66d665589324c9d7e8a3a
1 #!/usr/bin/env python
3 import logging
4 import os
5 import platform
6 import sys
7 import time
9 if sys.version_info[0] != 2 or sys.version_info[1] < 5:
10 print ('ERROR: pyTivo requires Python >= 2.5, < 3.0.\n')
11 sys.exit(1)
13 try:
14 import ssl
15 ssl._create_default_https_context = ssl._create_unverified_context
16 except:
17 pass
19 import beacon
20 import config
21 import httpserver
23 def exceptionLogger(*args):
24 sys.excepthook = sys.__excepthook__
25 logging.getLogger('pyTivo').error('Exception in pyTivo', exc_info=args)
27 def last_date():
28 lasttime = -1
29 path = os.path.dirname(__file__)
30 if not path:
31 path = '.'
32 for root, dirs, files in os.walk(path):
33 for name in files:
34 if name.endswith('.py'):
35 tm = os.path.getmtime(os.path.join(root, name))
36 if tm > lasttime:
37 lasttime = tm
39 return time.asctime(time.localtime(lasttime))
41 def setup(in_service=False):
42 config.init(sys.argv[1:])
43 config.init_logging()
44 sys.excepthook = exceptionLogger
46 port = config.getPort()
48 httpd = httpserver.TivoHTTPServer(('', int(port)),
49 httpserver.TivoHTTPHandler)
51 logger = logging.getLogger('pyTivo')
52 logger.info('Last modified: ' + last_date())
53 logger.info('Python: ' + platform.python_version())
54 logger.info('System: ' + platform.platform())
56 for section, settings in config.getShares():
57 httpd.add_container(section, settings)
59 b = beacon.Beacon()
60 b.add_service('TiVoMediaServer:%s/http' % port)
61 b.start()
62 if 'listen' in config.getBeaconAddresses():
63 b.listen()
65 httpd.set_beacon(b)
66 httpd.set_service_status(in_service)
68 logger.info('pyTivo is ready.')
69 return httpd
71 def serve(httpd):
72 try:
73 httpd.serve_forever()
74 except KeyboardInterrupt:
75 pass
77 def mainloop():
78 httpd = setup()
79 serve(httpd)
80 httpd.beacon.stop()
81 return httpd.restart
83 if __name__ == '__main__':
84 while mainloop():
85 time.sleep(5)