##MAJOR Code Change
[pyTivo/krkeegan.git] / beacon.py
blob25efc89a0b96799131cacbe6ed2ac92ad66b5f6a
1 from socket import *
2 from threading import Timer
3 from Config import config
5 class Beacon:
7 UDPSock = socket(AF_INET, SOCK_DGRAM)
8 UDPSock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
9 services = []
11 def add_service(self, service):
12 self.services.append(service)
13 self.send_beacon()
15 def format_services(self):
16 return ';'.join(self.services)
18 def format_beacon(self):
19 beacon = []
21 from Config import config
23 if config.has_option('Server', 'GUID'):
24 guid = config.get('Server', 'GUID')
25 else:
26 guid = '123456'
28 beacon.append('tivoconnect=1')
29 beacon.append('swversion=1')
30 beacon.append('method=broadcast')
31 beacon.append('identity=%s' % guid)
33 import socket
34 beacon.append('machine=%s' % socket.gethostname())
35 beacon.append('platform=pc')
36 beacon.append('services=' + self.format_services())
38 return '\n'.join(beacon)
40 def send_beacon(self):
41 if config.has_option('Server', 'beacon'):
42 beacon_ips = config.get('Server', 'beacon')
43 else:
44 beacon_ips = '255.255.255.255'
45 for beacon_ip in beacon_ips.split():
46 self.UDPSock.sendto(self.format_beacon(), (beacon_ip, 2190))
48 def start(self):
49 self.send_beacon()
50 self.timer = Timer(60, self.start)
51 self.timer.start()
53 def stop(self):
54 self.timer.cancel()
56 if __name__ == '__main__':
57 b = Beacon()
60 b.add_service('TiVoMediaServer:9032/http')
61 b.send_beacon_timer()