-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackISU2017.py
More file actions
73 lines (56 loc) · 2.04 KB
/
hackISU2017.py
File metadata and controls
73 lines (56 loc) · 2.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
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
70
71
from tweepy.streaming import StreamListener
from tweepy import API
from tweepy import OAuthHandler
from tweepy import Stream
from auth import TwitterAuth
import json
import pymongo
#collect the tweets in this list
search = []
class StdOutListener(StreamListener):
#This function gets called every time a new tweet is received on the stream
def on_data(self, data):
#Convert the tweet data to a json object
j=json.loads(data)
#append tweet to search list
search.append(j)
print len(search)
if __name__ == '__main__':
# Connection to Mongo DB
try:
conn=pymongo.MongoClient()
print "Connected successfully!!!"
except pymongo.errors.ConnectionFailure, e:
print "Could not connect to MongoDB: %s" % e
###PLEASE CHANGE THE FOLLOWING LINE
db = conn.HACKISU2017_tweetsdb #creates a database, if needed
posts = db.posts #creates collection, if needed
try:
#Create the listener
l = StdOutListener()
auth = OAuthHandler(TwitterAuth.consumer_key, TwitterAuth.consumer_secret)
auth.set_access_token(TwitterAuth.access_token, TwitterAuth.access_token_secret)
#Connect to the Twitter stream
stream = Stream(auth, l)
#Terms to track
#stream.Filter(track=['hackisu'])
#Alternatively, location box for geotagged tweets
#stream.filter(locations=[-0.530, 51.322, 0.231, 51.707]) #london
#stream.filter(locations=[-96.0,40.5,-91.0,43.5]) #iowa
stream.filter(locations=[-124.5, 25.0, -67.0, 47.5]) #continental U.S. rectangle
except KeyboardInterrupt:
#User pressed ctrl+c -- get ready to exit the program
pass
for tweet in search:
# Empty dictionary for storing tweet related data
data ={}
data['followers_count'] = tweet[u'user'][u'followers_count']
data['lang'] = tweet[u'lang']
data['text'] = tweet[u'text']
data['hashtags'] =tweet[u'entities'][u'hashtags']
data['screen_names']=tweet[u'user'][u'screen_name']
#data['text'] = tweet[u'text']
#data ['city']=tweet[u'place'][u'street_address']
data['place']=tweet[u'place']
# Insert document into collection
posts.insert(data)