forked from rahims/SoCo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.py
82 lines (61 loc) · 2.37 KB
/
events.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
# -*- coding: utf-8 -*-
import socket
import requests
from gevent import pywsgi
class Events(object):
AVTRANSPORT_ENDPOINT = 'http://{0}:1400/MediaRenderer/AVTransport/Event'
def __init__(self, speaker_ip):
self.listeners = set()
self.server = None
self.speaker_ip = speaker_ip
def subscribe(self, callback):
self.listeners.add(callback)
def start(self, host='', port=8080):
self.server = pywsgi.WSGIServer((host, port), self.__event_server)
self.server.start()
ip = self.__get_local_ip()
headers = {
'Callback': '<http://{0}:{1}>'.format(ip, port),
'NT': 'upnp:event'
}
endpoint = self.AVTRANSPORT_ENDPOINT.format(self.speaker_ip)
# `SUBSCRIBE` is a custom HTTP/1.1 verb used by Sonos devices.
r = requests.request('SUBSCRIBE', endpoint, headers=headers)
# Raise an exception if we get back a non-200 from the speaker.
r.raise_for_status()
def stop(self):
# TODO: Investigate if there is an `UNSUBSCRIBE` verb.
self.server.stop()
def __event_server(self, environ, start_response):
status = '405 Method Not Allowed'
# `NOTIFY` is a custom HTTP/1.1 verb used by Sonos devices
headers = [
('Allow', 'NOTIFY'),
('Content-Type', 'text/plain')
]
response = "Sorry, I only support the HTTP/1.1 NOTIFY verb.\n"
if environ['REQUEST_METHOD'].lower() == 'notify':
body = environ['wsgi.input'].readline()
for callback in self.listeners:
# TODO: Parse the raw XML into something sensible.
# Right now, subscribed listeners will just get the raw XML
# sent from the Sonos device.
callback(body)
status = '200 OK'
headers = []
response = ''
start_response(status, headers)
return [response]
def __get_local_ip(self):
# Not a fan of this, but there isn't a good cross-platform way of
# determining the local IP.
# From http://stackoverflow.com/a/7335145
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.8', 9))
ip = s.getsockname()[0]
except socket.error:
raise
finally:
del s
return ip