-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_sampler.py
58 lines (45 loc) · 1.23 KB
/
tweet_sampler.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
# Code by Artur Pimentel de Oliveira - apiment2
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.utils import import_simplejson
json = import_simplejson()
# Please assign your keys strings to the variables below
# Consumer keys
consumer_key = ""
consumer_secret = ""
# Access keys
access_token=""
access_token_secret=""
lon = 0
lat = 1
longitude_left_point = -86.33
latitude_left_point = 41.63
longitude_right_point = -86.20
latitude_right_point = 41.74
class StdOutListener(StreamListener):
n_good_tweets = 0
def on_data(self, raw_data):
data = json.loads(raw_data)
if data.has_key("text"):
text = data["text"]
if text is None:
return True
else:
if self.n_good_tweets < 50000:
#print str(self.n_good_tweets) + ". " + text.encode("utf-8")
print raw_data
self.n_good_tweets += 1
return True
else:
return False
else:
return True
def on_error(self, status):
print status
if __name__ == '__main__':
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.filter(track=['Zika virus', 'virus Zika'])