-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_stream_consume.py
50 lines (44 loc) · 1.35 KB
/
twitter_stream_consume.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
#!/usr/bin/env python
import anyjson
import atexit
import hashlib
import multiprocessing
import os
import urllib
import subprocess
import sys
def twitter_stream(username, password, handler, **kwargs):
qs = urllib.urlencode(kwargs)
pipe_path = '/tmp/twitter_stream_%s' % hashlib.md5(qs).hexdigest()
if not os.path.exists(pipe_path):
os.mkfifo(pipe_path)
current_path = os.path.dirname(
os.path.abspath(sys.modules[__name__].__file__))
process = multiprocessing.Process(target=subprocess.call, args=([
os.path.join(current_path, 'twitter_stream_create.sh'),
pipe_path,
username,
password,
qs,
],))
process.start()
def at_exit():
print 'terminating'
if process.is_alive():
process.terminate()
atexit.register(at_exit)
while True:
with open(pipe_path, 'r') as pipe:
while True:
last_line = pipe.readline()
if last_line == '':
break
elif last_line == '\n':
continue
try:
tweet = anyjson.deserialize(last_line)
except ValueError:
print 'Error decoding tweet: "%s"' % last_line
else:
print 'Received tweet'
handler(tweet)