forked from armooo/pytivo
-
Notifications
You must be signed in to change notification settings - Fork 42
/
pyTivo.py
executable file
·85 lines (67 loc) · 1.97 KB
/
pyTivo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
import logging
import os
import platform
import sys
import time
if sys.version_info[0] != 2 or sys.version_info[1] < 5:
print ('ERROR: pyTivo requires Python >= 2.5, < 3.0.\n')
sys.exit(1)
try:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
except:
pass
import beacon
import config
import httpserver
def exceptionLogger(*args):
sys.excepthook = sys.__excepthook__
logging.getLogger('pyTivo').error('Exception in pyTivo', exc_info=args)
def last_date():
lasttime = -1
path = os.path.dirname(__file__)
if not path:
path = '.'
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith('.py'):
tm = os.path.getmtime(os.path.join(root, name))
if tm > lasttime:
lasttime = tm
return time.asctime(time.localtime(lasttime))
def setup(in_service=False):
config.init(sys.argv[1:])
config.init_logging()
sys.excepthook = exceptionLogger
port = config.getPort()
httpd = httpserver.TivoHTTPServer(('', int(port)),
httpserver.TivoHTTPHandler)
logger = logging.getLogger('pyTivo')
logger.info('Last modified: ' + last_date())
logger.info('Python: ' + platform.python_version())
logger.info('System: ' + platform.platform())
for section, settings in config.getShares():
httpd.add_container(section, settings)
b = beacon.Beacon()
b.add_service('TiVoMediaServer:%s/http' % port)
b.start()
if 'listen' in config.getBeaconAddresses():
b.listen()
httpd.set_beacon(b)
httpd.set_service_status(in_service)
logger.info('pyTivo is ready.')
return httpd
def serve(httpd):
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
def mainloop():
httpd = setup()
serve(httpd)
httpd.beacon.stop()
return httpd.restart
if __name__ == '__main__':
while mainloop():
time.sleep(5)