-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfilter_daemon.py
executable file
·86 lines (65 loc) · 2.61 KB
/
filter_daemon.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
import time
import json
import redis
from twython import TwythonStreamer
from auth import consumer_key, consumer_secret, access_token_secret, access_token
class FilterRedis(object):
key = "tweets"
r = redis.Redis(host='localhost', port=6379)
num_tweets = 20
trim_threshold = 100
def __init__(self):
self.trim_count = 0
def push(self, data):
self.r.lpush(self.key, data)
self.trim_count += 1
if self.trim_count >= self.trim_threshold:
self.r.ltrim(self.key, 0, self.num_tweets)
self.trim_count = 0
def tweets(self, limit=15, since=0):
data = self.r.lrange(self.key, 0, limit - 1)
return [json.loads(x) for x in data if int(json.loads(x)['received_at']) > since]
class StreamWatcherListener(TwythonStreamer):
fr = FilterRedis()
tweet_count = 0
def on_success(self, data):
tweet = data
if not 'text' in tweet:
return
try:
if '@' in tweet['text'] or not tweet['text'].endswith('?'):
return True
print tweet['text'].encode('utf-8')
print '\n %s %s\n' % (tweet['user']['screen_name'], tweet['created_at'])
self.fr.push(json.dumps( {
'id': tweet['id'],
'text': tweet['text'],
'username': tweet['user']['screen_name'],
'userid': tweet['user']['id'],
'name': tweet['user']['name'],
'profile_image_url': tweet['user']['profile_image_url'],
'received_at': time.time()
}))
self.tweet_count += 1
if self.tweet_count >= 10:
print 'got 10 tweets sleeping'
time.sleep(25)
self.tweet_count = 0
except Exception, e:
# Catch any unicode errors while printing to console
# and just ignore them to avoid breaking application.
print e
def on_error(self, status_code, data):
print 'An error has occured! Status code = %s' % status_code
time.sleep(20)
def on_timeout(self):
print 'Snoozing Zzzzzz'
time.sleep(10)
if __name__ == '__main__':
words = ["why", "how", "when", "where", "who", "feeling", "lol"]
#auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
#auth.set_access_token(access_token, access_token_secret)
#stream = tweepy.Stream(auth, StreamWatcherListener(), timeout=None)
#stream.filter(None, words)
stream = StreamWatcherListener(consumer_key, consumer_secret, access_token, access_token_secret)
stream.statuses.filter(track=words)