-
Notifications
You must be signed in to change notification settings - Fork 1
/
getTweets.py
53 lines (43 loc) · 1.56 KB
/
getTweets.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
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler, Cursor, Stream, API, TweepError
import json
consumer_key = 'consumer_key'
consumer_secret = 'consumer_secret'
access_token = 'access_token'
access_token_secret = 'access_token_secret'
class StdOutListener(StreamListener):
def on_data(self, data):
print data
def on_error(self, error_code):
print error_code
return True
def on_timeout(self):
print 'Timeout!'
return True
def on_disconnect(self):
print 'Disconnected'
return True
def streamTweets(keywords, languages):
listener = StdOutListener()
authentication = OAuthHandler(consumer_key, consumer_secret)
authentication.set_access_token(access_token, access_token_secret)
stream = Stream(authentication, listener)
stream.filter(track=keywords, languages=languages)
def findTweets(keywords):
authentication = OAuthHandler(consumer_key, consumer_secret)
authentication.set_access_token(access_token, access_token_secret)
api = API(authentication)
cursor = Cursor(api.search, q=keywords).items()
while True:
try:
tweet = cursor.next()
print ','.join([str(tweet.created_at), tweet.lang, tweet.text]).encode('utf-8')
except TweepError:
time.sleep(60 * 15)
continue
except StopIteration:
break
if __name__== "__main__":
keywords=['na', 'w', 'o', 'na', 'to', 'z', 'i', 'do', 'nie', 'to', 'lub']
languages=['pl']
streamTweets(keywords, languages)