-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_script.py
45 lines (36 loc) · 1.78 KB
/
twitter_script.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
keywords = raw_input("Enter keywords with comma seperated :")
print "search keywords : " + keywords
# Import the necessary package to process data in JSON format
try:
import json
except ImportError:
import simplejson as json
# Import the necessary methods from "twitter" library
from twitter import Twitter, OAuth, TwitterHTTPError, TwitterStream
# Variables that contains the user credentials to access Twitter API
ACCESS_TOKEN = '970879699629912064-J3LkOsWfQNYBVygkJ6l0vNa5APYJMjT'
ACCESS_SECRET = 'mqopj5kDosPWpzokH8VXvht7znHPlR1QwdcTntB9JilqM'
CONSUMER_KEY = 'v08rJASMheq0q0I4xZxDGmd6y'
CONSUMER_SECRET = 'ep9dtXFBoMUZRhmJdCmXbHJz3DrIlJmOJSf9Ch08oqQgr3TDix'
oauth = OAuth(ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
# Initiate the connection to Twitter Streaming API
twitter_stream = TwitterStream(auth=oauth)
# Get a sample of the public data following through Twitter
iterator = twitter_stream.statuses.filter(track=keywords, language="en", result_type="popular")
# Print each tweet in the stream to the screen
# Here we set it to stop after getting 1000 tweets.
# You don't have to set it to stop, but can continue running
# the Twitter API to collect data for days or even longer.
tweet_count = 1000
for tweet in iterator:
tweet_count -= 1
# Twitter Python Tool wraps the data returned by Twitter
# as a TwitterDictResponse object.
# We convert it back to the JSON format to print/score
#print json.dumps(tweet)
if 'text' in tweet: # only messages contains 'text' field is a tweet
print "{message : " + tweet['text'] + ", created_at : " + tweet['created_at'] + "}\n" # content of the tweet
# The command below will do pretty printing for JSON data, try it out
# print json.dumps(tweet, indent=4)
if tweet_count <= 0:
break