updated XBMC NFO support, added movies, ratings, stars
[pyTivo/wmcbrine/lucasnz.git] / pyTivo.py
blob82cc53e33737e4aa22a91e5441c23711018b8eac
1 #!/usr/bin/env python
3 import logging
4 import os
5 import sys
6 import time
8 if sys.version_info[0] != 2 or sys.version_info[1] < 4:
9 print ('ERROR: pyTivo requires Python >= 2.4, < 3.0.\n')
10 sys.exit(1)
12 import beacon
13 import config
14 import httpserver
15 from plugin import GetPlugin
17 def exceptionLogger(*args):
18 sys.excepthook = sys.__excepthook__
19 logging.getLogger('pyTivo').error('Exception in pyTivo', exc_info=args)
21 def setup(in_service=False):
22 config.init(sys.argv[1:])
23 config.init_logging()
24 sys.excepthook = exceptionLogger
26 port = config.getPort()
28 httpd = httpserver.TivoHTTPServer(('', int(port)),
29 httpserver.TivoHTTPHandler)
31 logger = logging.getLogger('pyTivo')
33 for section, settings in config.getShares():
34 httpd.add_container(section, settings)
35 # Precaching of files: does a recursive list of base path
36 if settings.get('precache', 'False').lower() == 'true':
37 plugin = GetPlugin(settings.get('type'))
38 if hasattr(plugin, 'pre_cache'):
39 logger.info('Pre-caching the ' + section + ' share.')
40 pre_cache_filter = getattr(plugin, 'pre_cache')
42 def build_recursive_list(path):
43 try:
44 for f in os.listdir(path):
45 f = os.path.join(path, f)
46 if os.path.isdir(f):
47 build_recursive_list(f)
48 else:
49 pre_cache_filter(f)
50 except:
51 pass
53 build_recursive_list(settings.get('path'))
55 b = beacon.Beacon()
56 b.add_service('TiVoMediaServer:%s/http' % port)
57 b.start()
58 if 'listen' in config.getBeaconAddresses():
59 b.listen()
61 httpd.set_beacon(b)
62 httpd.set_service_status(in_service)
64 logger.info('pyTivo is ready.')
65 return httpd
67 def serve(httpd):
68 try:
69 httpd.serve_forever()
70 except KeyboardInterrupt:
71 pass
73 def mainloop():
74 httpd = setup()
75 serve(httpd)
76 httpd.beacon.stop()
77 return httpd.restart
79 if __name__ == '__main__':
80 while mainloop():
81 time.sleep(5)