-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2b.py
More file actions
42 lines (28 loc) · 1.04 KB
/
task2b.py
File metadata and controls
42 lines (28 loc) · 1.04 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
import tweepy
import json
from pymongo import MongoClient
MONGO_HOST= 'mongodb://localhost/twitterdb'
#WORDS = ['wonderful']
#insert keys
class Listener(tweepy.StreamListener):
def on_error(self, status_code):
print('An Error has occured: ' + repr(status_code))
return False
def on_data(self, data):
try:
client = MongoClient(MONGO_HOST)
db = client.twitterdb
datajson = json.loads(data)
created = datajson['created_at']
print("Tweet collected at " + str(created))
db.search.insert(datajson)
except Exception as e:
print(e)
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
listener = Listener(api=tweepy.API(wait_on_rate_limit=True))
streamer = tweepy.Stream(auth=auth, listener=listener)
#print("Tracking: " + str(WORDS))
print("Tracking: " + str('wonderful'))
streamer.filter(track='wonderful')
#streamer.filter(track=WORDS)