-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtwitter_streaming_api_csv.py
More file actions
54 lines (42 loc) · 1.25 KB
/
twitter_streaming_api_csv.py
File metadata and controls
54 lines (42 loc) · 1.25 KB
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
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import json
######################
## Load API Tokens
######################
tokenfile = open('/Users/psehgal/dev/Sentiment.Analysis/twitter_tokens.json')
tokens = json.load(tokenfile)
ckey = tokens['ckey']
csecret = tokens['csecret']
atoken = tokens['atoken']
asecret = tokens['asecret']
######################
## Create listener class
######################
class listener(StreamListener):
def on_data(self, data):
print(data)
savefile = open('twitDB1.csv','a')
savefile.write(data)
savefile.write('\n')
savefile.close()
# tweet = data.split(',"text":')[1].split('","source')[0]
# print(tweet)
# savethis = str(time.time())+':::'+tweet
# savefile = open('twitDB2.csv', 'a')
# savefile.write(savethis)
# savefile.write('\n')
# savefile.close()
return True
def on_error(self, status):
print (status)
######################
## main program
######################
print('api started')
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["$TSLA"])