-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_saveDataFile.py
executable file
·69 lines (44 loc) · 2.1 KB
/
4_saveDataFile.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
61
62
63
64
65
66
67
68
69
#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import time
import os
import sys
# 1MB = 1024*1024.0 = 1048576
MAX_FILE_SIZE = 1048576 * 1024 # 1 GB
#keys of AppPkr16
consumer_key='7uxGIGQjbSRqvRFWhzK5ydmio'
consumer_secret = 'UNL9OAe68NaWyqwpBDEDVXvpksS0x3ksdfJvlfHfncozwrFyTo'
access_token = '4213955833-8TpiXlB60zfcLw4gIpfPt0jJXLjm8LLm0WoIBQe'
access_token_secret = '0crJHDGQpN7lCEhsQ6UCkoTslbL16osVeLzI8GGAUL0vh'
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
# Open a file to wrtie the tweets
fo = open("twt.txt", "a+")
# print time.ctime()
# print data;
fo.write( data ); # write data to file
#close the file
fo.close()
#fileSize = os.path.getsize("twt.txt")
if((os.path.getsize("twt.txt") ) > MAX_FILE_SIZE):
exit(1)
return True
def on_error(self, status):
print status
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
if __name__ == '__main__':
fo = open("twt.txt","w")
fo.write(str(time.ctime())+"\n" )
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
#This line filter Twitter Streams to capture data by the keywords: 'flu', 'cough'
stream.filter(track=['flu','cough','common cold', 'sneeze', 'head cold','h1n1','swineflu','pneumonia','fever','chills', 'runny nose','Noscapine','Triprolidine','Pseudoephedrine','Oxymetazoline','Chlorpheniramine','Levodropropizine','Benzonatate','Aspirin','Codeine', 'Colistimethate','Guaifenesin','Homatropine','Hydrocodone ','Chlorpheniramine'])
#stream.filter(track=['manchester united'],locations=[-122.75,36.8,-121.75,37.8])