-
Notifications
You must be signed in to change notification settings - Fork 0
/
getKeywordTweets.py
61 lines (51 loc) · 1.67 KB
/
getKeywordTweets.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
from datetime import datetime
import tweepy
import codecs
import time
########################
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
storePath = ""
project = ""
keywords = ["a", "b", "c"]
########################
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
start = datetime.utcnow()
write = codecs.open(storePath+project+'_'+str(start.year)+'_'+str(start.month)+'_'+str(start.day)+".json",'a','utf-8')
class StdOutListener(tweepy.StreamListener):
def on_data(self, data):
global write
try:
write.write(str(data))
except Exception as e:
print(e)
print(str(data))
end = datetime.utcnow()
global start
if(end.day != start.day):
start = end
write.close()
write = codecs.open(storePath+project+'_'+str(start.year)+'_'+str(start.month)+'_'+str(start.day)+".json",'a','utf-8')
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
l = StdOutListener()
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = tweepy.Stream(auth, l)
while(True):
try:
#https://dev.twitter.com/streaming/overview/request-parameters#track
stream.filter(track = keywords,encoding='utf-8')
except KeyboardInterrupt:
stream.disconnect()
break
except Exception as e:
print (time.gmtime())
print ("severe problem",e)
continue