-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterMiner.py
50 lines (33 loc) · 993 Bytes
/
twitterMiner.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
import tweepy
import time
import os
from tweepy import OAuthHandler
start_time = time.time()
keyword_list = [''] #track list
key = 'consumerkey'
secret = 'consumersecret'
access_key = 'accesskey'
access_secret = 'access_secret'
class listener(tweepy.StreamListener):
def __init__(self, start_time, time_limit=60):
self.time = start_time
self.limit = time_limit
def on_data(self, data):
while (time.time() - self.time) < self.limit:
try:
saveFile = open('raw_tweets.json', 'a')
saveFile.write(data)
saveFile.close()
return True
except BaseException, e:
print 'failed ondata,', str(e)
time.sleep(5)
pass
exit()
def on_error(self, status):
print statuses
auth = OAuthHandler(key, secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
twitterStream = tweepy.Stream(auth, listener(start_time, time_limit=10)) #initialize Stream object with a time out limit
twitterStream.sample(languages=['en'])