-
Notifications
You must be signed in to change notification settings - Fork 1
/
TweeterProcessing.py
executable file
·63 lines (45 loc) · 1.77 KB
/
TweeterProcessing.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
import tweeter_config
import tweepy
from MongoTasks import MongoTasks
import mongo_config
import json
class TwitterProcessing:
global mongo
mongo = MongoTasks()
def get_auth(self):
print "read from config file!"
auth = tweepy.OAuthHandler(tweeter_config.consumer_key, tweeter_config.consumer_secret)
auth.set_access_token(tweeter_config.access_token, tweeter_config.access_token_secret)
return auth
def connect_twitter_api_search(self, auth):
print "connect"
api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())
return api
def connect_twitter_stream(self, auth):
l = tweepy.StdOutListener()
stream = tweepy.Stream(auth, l)
stream.filter(languages=["en"], track=["a", "the", "i", "you", "u"]) # etc
return stream
def get_tweet(self,api,keywords):
print "get tweets"
public_tweets = api.search(keywords)
#for tweet in public_tweets:
#print tweet.text
return public_tweets
def put_in_mongo(self, tweets):
print "put in mongo"
def run(self, dbName,collectionName):
auth = self.get_auth()
api = self.connect_twitter_api_search(auth)
tweets = self.get_tweet(api, "policy")
collection = mongo.connect(mongo_config.host, mongo_config.port,dbName,collectionName)
print json.dumps(tweets, indent=4, sort_keys=True)
for tweet in tweets["statuses"]:
#print tweet
#t = json.loads(tweet)
if tweet["lang"] == "en":
mongo.appendDataToMongo(collection, tweet)
# print tweet
x = TwitterProcessing()
x.run("test_policy","policy_analysis2")
print ("Public Policy related tweets collected and pushed to mogodb Succefully!")