forked from armooo/pytivo
-
Notifications
You must be signed in to change notification settings - Fork 42
/
beacon.py
235 lines (196 loc) · 7.46 KB
/
beacon.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import logging
import re
import socket
import struct
import time
import uuid
from threading import Timer
from urllib import quote
import zeroconf
import config
from plugin import GetPlugin
SHARE_TEMPLATE = '/TiVoConnect?Command=QueryContainer&Container=%s'
PLATFORM_MAIN = 'pyTivo'
PLATFORM_VIDEO = 'pc/pyTivo' # For the nice icon
class ZCListener:
def __init__(self, names):
self.names = names
def removeService(self, server, type, name):
self.names.remove(name.replace('.' + type, ''))
def addService(self, server, type, name):
self.names.append(name.replace('.' + type, ''))
class ZCBroadcast:
def __init__(self, logger):
""" Announce our shares via Zeroconf. """
self.share_names = []
self.share_info = []
self.logger = logger
self.rz = zeroconf.Zeroconf()
self.renamed = {}
old_titles = self.scan()
address = socket.inet_aton(config.get_ip())
port = int(config.getPort())
logger.info('Announcing shares...')
for section, settings in config.getShares():
try:
ct = GetPlugin(settings['type']).CONTENT_TYPE
except:
continue
if ct.startswith('x-container/'):
if 'video' in ct:
platform = PLATFORM_VIDEO
else:
platform = PLATFORM_MAIN
logger.info('Registering: %s' % section)
self.share_names.append(section)
desc = {'path': SHARE_TEMPLATE % quote(section),
'platform': platform, 'protocol': 'http',
'tsn': '{%s}' % uuid.uuid4()}
tt = ct.split('/')[1]
title = section
count = 1
while title in old_titles:
count += 1
title = '%s [%d]' % (section, count)
self.renamed[section] = title
info = zeroconf.ServiceInfo('_%s._tcp.local.' % tt,
'%s._%s._tcp.local.' % (title, tt),
address, port, 0, 0, desc)
self.rz.registerService(info)
self.share_info.append(info)
def scan(self):
""" Look for TiVos using Zeroconf. """
VIDS = '_tivo-videos._tcp.local.'
names = []
self.logger.info('Scanning for TiVos...')
# Get the names of servers offering TiVo videos
browser = zeroconf.ServiceBrowser(self.rz, VIDS, ZCListener(names))
# Give them a second to respond
time.sleep(1)
# Any results?
if names:
config.tivos_found = True
# Now get the addresses -- this is the slow part
for name in names:
info = self.rz.getServiceInfo(VIDS, name + '.' + VIDS)
if info:
tsn = info.properties.get('TSN')
if config.get_server('togo_all'):
tsn = info.properties.get('tsn', tsn)
if tsn:
address = socket.inet_ntoa(info.getAddress())
port = info.getPort()
config.tivos[tsn] = {'name': name, 'address': address,
'port': port}
config.tivos[tsn].update(info.properties)
self.logger.info(name)
return names
def shutdown(self):
self.logger.info('Unregistering: %s' % ' '.join(self.share_names))
for info in self.share_info:
self.rz.unregisterService(info)
self.rz.close()
class Beacon:
def __init__(self):
self.UDPSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.UDPSock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.services = []
self.platform = PLATFORM_VIDEO
for section, settings in config.getShares():
try:
ct = GetPlugin(settings['type']).CONTENT_TYPE
except:
continue
if ct in ('x-container/tivo-music', 'x-container/tivo-photos'):
self.platform = PLATFORM_MAIN
break
if config.get_zc():
logger = logging.getLogger('pyTivo.beacon')
try:
self.bd = ZCBroadcast(logger)
except:
logger.error('Zeroconf failure')
self.bd = None
else:
self.bd = None
def add_service(self, service):
self.services.append(service)
self.send_beacon()
def format_services(self):
return ';'.join(self.services)
def format_beacon(self, conntype, services=True):
beacon = ['tivoconnect=1',
'method=%s' % conntype,
'identity={%s}' % config.getGUID(),
'machine=%s' % socket.gethostname(),
'platform=%s' % self.platform]
if services:
beacon.append('services=' + self.format_services())
else:
beacon.append('services=TiVoMediaServer:0/http')
return '\n'.join(beacon) + '\n'
def send_beacon(self):
beacon_ips = config.getBeaconAddresses()
beacon = self.format_beacon('broadcast')
for beacon_ip in beacon_ips.split():
if beacon_ip != 'listen':
try:
packet = beacon
while packet:
result = self.UDPSock.sendto(packet, (beacon_ip, 2190))
if result < 0:
break
packet = packet[result:]
except Exception, e:
print e
def start(self):
self.send_beacon()
self.timer = Timer(60, self.start)
self.timer.start()
def stop(self):
self.timer.cancel()
if self.bd:
self.bd.shutdown()
def recv_bytes(self, sock, length):
block = ''
while len(block) < length:
add = sock.recv(length - len(block))
if not add:
break
block += add
return block
def recv_packet(self, sock):
length = struct.unpack('!I', self.recv_bytes(sock, 4))[0]
return self.recv_bytes(sock, length)
def send_packet(self, sock, packet):
sock.sendall(struct.pack('!I', len(packet)) + packet)
def listen(self):
""" For the direct-connect, TCP-style beacon """
import thread
def server():
TCPSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
TCPSock.bind(('', 2190))
TCPSock.listen(5)
while True:
# Wait for a connection
client, address = TCPSock.accept()
# Accept (and discard) the client's beacon
self.recv_packet(client)
# Send ours
self.send_packet(client, self.format_beacon('connected'))
client.close()
thread.start_new_thread(server, ())
def get_name(self, address):
""" Exchange beacons, and extract the machine name. """
our_beacon = self.format_beacon('connected', False)
machine_name = re.compile('machine=(.*)\n').search
try:
tsock = socket.socket()
tsock.connect((address, 2190))
self.send_packet(tsock, our_beacon)
tivo_beacon = self.recv_packet(tsock)
tsock.close()
name = machine_name(tivo_beacon).groups()[0]
except:
name = address
return name