6 from threading
import Timer
7 from urllib
import quote
12 from plugin
import GetPlugin
14 SHARE_TEMPLATE
= '/TiVoConnect?Command=QueryContainer&Container=%s'
17 def __init__(self
, names
):
20 def removeService(self
, server
, type, name
):
21 self
.names
.remove(name
)
23 def addService(self
, server
, type, name
):
24 self
.names
.append(name
)
27 def __init__(self
, logger
):
28 """ Announce our shares via Zeroconf. """
32 self
.rz
= Zeroconf
.Zeroconf()
33 address
= inet_aton(config
.get_ip())
34 port
= int(config
.getPort())
35 for section
, settings
in config
.getShares():
36 ct
= GetPlugin(settings
['type']).CONTENT_TYPE
37 if ct
.startswith('x-container/'):
38 logger
.info('Registering: %s' % section
)
39 self
.share_names
.append(section
)
40 desc
= {'path': SHARE_TEMPLATE
% quote(section
),
41 'platform': 'pc', 'protocol': 'http'}
43 info
= Zeroconf
.ServiceInfo('_%s._tcp.local.' % tt
,
44 '%s._%s._tcp.local.' % (section
, tt
),
45 address
, port
, 0, 0, desc
)
46 self
.rz
.registerService(info
)
47 self
.share_info
.append(info
)
50 """ Look for TiVos using Zeroconf. """
51 VIDS
= '_tivo-videos._tcp.local.'
54 # Get the names of servers offering TiVo videos
55 browser
= Zeroconf
.ServiceBrowser(self
.rz
, VIDS
, ZCListener(names
))
57 # Give them half a second to respond
60 # Now get the addresses -- this is the slow part
62 info
= self
.rz
.getServiceInfo(VIDS
, name
)
63 if 'TSN' in info
.properties
:
64 tsn
= info
.properties
['TSN']
65 address
= inet_ntoa(info
.getAddress())
66 config
.tivos
[tsn
] = address
67 config
.tivo_names
[tsn
] = name
.replace('.' + VIDS
, '')
70 self
.logger
.info('Unregistering: %s' % ' '.join(self
.share_names
))
71 for info
in self
.share_info
:
72 self
.rz
.unregisterService(info
)
77 UDPSock
= socket(AF_INET
, SOCK_DGRAM
)
78 UDPSock
.setsockopt(SOL_SOCKET
, SO_BROADCAST
, 1)
83 logger
= logging
.getLogger('pyTivo.beacon')
84 logger
.info('Announcing shares...')
85 self
.bd
= ZCBroadcast(logger
)
86 logger
.info('Scanning for TiVos...')
91 def add_service(self
, service
):
92 self
.services
.append(service
)
95 def format_services(self
):
96 return ';'.join(self
.services
)
98 def format_beacon(self
, conntype
, services
=True):
99 beacon
= ['tivoconnect=1',
101 'method=%s' % conntype
,
102 'identity=%s' % config
.getGUID(),
103 'machine=%s' % gethostname(),
107 beacon
.append('services=' + self
.format_services())
109 beacon
.append('services=TiVoMediaServer:0/http')
111 return '\n'.join(beacon
)
113 def send_beacon(self
):
114 beacon_ips
= config
.getBeaconAddresses()
115 for beacon_ip
in beacon_ips
.split():
116 if beacon_ip
!= 'listen':
118 self
.UDPSock
.sendto(self
.format_beacon('broadcast'),
125 self
.timer
= Timer(60, self
.start
)
134 """ For the direct-connect, TCP-style beacon """
138 TCPSock
= socket(AF_INET
, SOCK_STREAM
)
139 TCPSock
.bind(('', 2190))
143 # Wait for a connection
144 client
, address
= TCPSock
.accept()
146 # Accept the client's beacon
147 client_length
= struct
.unpack('!I', client
.recv(4))[0]
148 client_message
= client
.recv(client_length
)
151 message
= self
.format_beacon('connected')
152 client
.send(struct
.pack('!I', len(message
)))
156 thread
.start_new_thread(server
, ())
158 def get_name(self
, address
):
159 """ Exchange beacons, and extract the machine name. """
160 our_beacon
= self
.format_beacon('connected', False)
161 machine_name
= re
.compile('machine=(.*)\n').search
165 tsock
.connect((address
, 2190))
167 tsock
.send(struct
.pack('!I', len(our_beacon
)))
168 tsock
.send(our_beacon
)
170 length
= struct
.unpack('!I', tsock
.recv(4))[0]
171 tivo_beacon
= tsock
.recv(length
)
175 name
= machine_name(tivo_beacon
).groups()[0]