-
Notifications
You must be signed in to change notification settings - Fork 10
/
tweetstream.py
227 lines (199 loc) · 8.29 KB
/
tweetstream.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
"""
The TweetStream class is just a simple HTTP client for handling the
twitter streaming API.
Usage is pretty simple:
import tweetstream
def callback(message):
# this will be called every message
print message
configuration = {
"twitter_consumer_secret": "ABCDEF1234567890",
"twitter_consumer_key": "0987654321ABCDEF",
"twitter_access_token_secret": "1234567890ABCDEF",
"twitter_access_token": "FEDCBA09123456789"
}
stream = tweetstream.TweetStream(configuration)
stream.fetch("/1/statuses/filter.json?track=foobar", callback=callback)
# if you aren't on a running ioloop...
from tornado.ioloop import IOLoop
IOLoop.instance().start()
The constructor takes two optional arguments, `ioloop` and `clean`.
The `ioloop` argument just lets you specify a specific loop to run on,
and `clean` is just a boolean (False by default) that will strip out
basic data from the twitter message payload.
"""
from tornado.iostream import IOStream, SSLIOStream
from tornado.ioloop import IOLoop
import json
import socket
import time
import oauth2
import urlparse
class MissingConfiguration(Exception):
"""Raised when a configuration value is not found."""
pass
class TweetStream(object):
""" Twitter stream connection """
def __init__(self, configuration, ioloop=None, clean=False):
""" Just set up the cache list and get first set """
# prepopulating cache
self._ioloop = ioloop or IOLoop.instance()
self._callback = None
self._error_callback = None
self._clean_message = clean
self._configuration = configuration
consumer_key = self._get_configuration_key("twitter_consumer_key")
consumer_secret = self._get_configuration_key(
"twitter_consumer_secret")
self._consumer = oauth2.Consumer(
key=consumer_key, secret=consumer_secret)
access_token = self._get_configuration_key("twitter_access_token")
access_secret = self._get_configuration_key(
"twitter_access_token_secret")
self._token = oauth2.Token(key=access_token, secret=access_secret)
self._twitter_stream_host = self._get_configuration_key(
"twitter_stream_host", "stream.twitter.com")
self._twitter_stream_scheme = self._get_configuration_key(
"twitter_stream_scheme", "https")
self._twitter_stream_port = self._get_configuration_key(
"twitter_stream_port", 443)
def _get_configuration_key(self, key, default=None):
"""
Retrieve a configuration option, raising an exception if no
default is provided.
"""
configured_value = self._configuration.get(key, default)
if configured_value is None:
raise MissingConfiguration("Missing configuration item: %s" % key)
return configured_value
def set_error_callback(self, error_callback):
"""Pretty self explanatory."""
self._error_callback = error_callback
def fetch(self, path, method="GET", callback=None):
""" Opens the request """
parts = urlparse.urlparse(path)
self._method = method
self._callback = callback
self._path = parts.path
self._full_path = self._path
self._parameters = {}
if parts.query:
self._full_path += "?%s" % parts.query
# throwing away empty or extra query arguments
self._parameters = dict([
(key, value[0]) for key, value in
urlparse.parse_qs(parts.query).iteritems()
if value
])
self.open_twitter_stream()
def on_error(self, error):
""" Just a wrapper for the error callback """
if self._error_callback:
return self._error_callback(error)
else:
raise error
def open_twitter_stream(self):
""" Creates the client and watches stream """
address_info = socket.getaddrinfo(self._twitter_stream_host,
self._twitter_stream_port, socket.AF_INET, socket.SOCK_STREAM,
0, 0)
af, socktype, proto = address_info[0][:3]
socket_address = address_info[0][-1]
sock = socket.socket(af, socktype, proto)
stream_class = IOStream
if self._twitter_stream_scheme == "https":
stream_class = SSLIOStream
self._twitter_stream = stream_class(sock, io_loop=self._ioloop)
self._twitter_stream.connect(socket_address, self.on_connect)
def on_connect(self):
parameters = {
"oauth_token": self._token.key,
"oauth_consumer_key": self._consumer.key,
"oauth_version": "1.0",
"oauth_nonce": oauth2.generate_nonce(),
"oauth_timestamp": int(time.time())
}
parameters.update(self._parameters)
request = oauth2.Request(
method="GET",
url="%s://%s%s" % (
self._twitter_stream_scheme,
self._twitter_stream_host,
self._path),
parameters=parameters)
signature_method = oauth2.SignatureMethod_HMAC_SHA1()
request.sign_request(signature_method, self._consumer, self._token)
headers = request.to_header()
headers["Host"] = self._twitter_stream_host
headers["User-Agent"] = "TweetStream"
headers["Accept"] = "*/*"
request = ["GET %s HTTP/1.1" % self._full_path]
for key, value in headers.iteritems():
request.append("%s: %s" % (key, value))
request = "\r\n".join(request) + "\r\n\r\n"
self._twitter_stream.write(str(request))
self._twitter_stream.read_until("\r\n\r\n", self.on_headers)
def on_headers(self, response):
""" Starts monitoring for results. """
status_line = response.splitlines()[0]
response_code = status_line.replace("HTTP/1.1", "")
response_code = int(response_code.split()[0].strip())
if response_code != 200:
exception_string = "Could not connect: %s\n%s" % (
status_line, response)
headers = dict([
(l.split(":")[0].lower(), ":".join(l.split(":")[1:]))
for l in response.splitlines()[1:]
])
content_length = int(headers.get("content-length") or 0)
if not content_length:
return self.on_error(Exception(exception_string))
def get_error_body(content):
full_string = "%s\n%s" % (exception_string, content)
self.on_error(Exception(full_string))
return self._twitter_stream.read_bytes(
content_length, get_error_body)
self.wait_for_message()
def wait_for_message(self):
""" Throw a read event on the stack. """
self._twitter_stream.read_until("\r\n", self.on_result)
def on_result(self, response):
""" Gets length of next message and reads it """
if (response.strip() == ""):
return self.wait_for_message()
length = int(response.strip(), 16)
self._twitter_stream.read_bytes(length, self.parse_json)
def parse_json(self, response):
""" Checks JSON message """
if not response.strip():
# Empty line, happens sometimes for keep alive
return self.wait_for_message()
try:
response = json.loads(response)
except ValueError:
print "Invalid response:"
print response
return self.wait_for_message()
self.parse_response(response)
def parse_response(self, response):
""" Parse the twitter message """
if self._clean_message:
try:
text = response["text"]
name = response["user"]["name"]
username = response["user"]["screen_name"]
avatar = response["user"]["profile_image_url_https"]
except KeyError, exc:
print "Invalid tweet structure, missing %s" % exc
return self.wait_for_message()
response = {
"type": "tweet",
"text": text,
"avatar": avatar,
"name": name,
"username": username,
"time": int(time.time())
}
if self._callback:
self._callback(response)
self.wait_for_message()