-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_stream.py
49 lines (34 loc) · 1.26 KB
/
twitter_stream.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
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
ckey = ''
csecret = ''
atoken = ''
asecret = ''
class listener(StreamListener):
def on_data(self, raw_data):
try:
#print (raw_data)
tweet = raw_data.split(',"text":"')[1] .split('","source')[0]
#print(tweet)
username = raw_data.split(',"screen_name":"')[1] .split('","location')[0]
#print(username)
location = raw_data.split(',"location":"')[1] .split('","url')[0]
#print(location)
saveThis = 'Time - '+ str(time.time()) + '\n'+ 'Username - ' + username + '\n'+ 'Tweet - '+ "'" +tweet +"'" + '\n' + 'Location - ' + location + '\n'
print(saveThis)
savetweet = open('twitDB89.csv', 'a')
savetweet.write(saveThis)
savetweet.write('\n')
savetweet.close()
return True
except BaseException as e:
print('failed ondata',str(e))
time.sleep(5)
def on_error(self, status_code):
print (status_code)
auth = OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
twitterStream = Stream(auth,listener())
twitterStream.filter(track=["beer"])