Skip to content

Instantly share code, notes, and snippets.

@rakeshsukla53
Created March 9, 2015 09:06
Show Gist options
  • Save rakeshsukla53/197f287821c564dbfdfd to your computer and use it in GitHub Desktop.
Save rakeshsukla53/197f287821c564dbfdfd to your computer and use it in GitHub Desktop.
Twiter Sentiment Analysis
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import urllib, json
import urllib2
ckey = ''
csecret = ''
atoken = ''
asecret = ''
sentdexAuth = ""
def sentimentAnalysis(text):
encoded_text = urllib.quote(text)
API_Call = "http://sentdex.com/api/api.php?text="+encoded_text+'&auth='+sentdexAuth
output = urllib.urlopen(API_Call).read()
print output
return output
class listener(StreamListener):
def on_data(self, data):
all_data = json.loads(data)
tweet = all_data['text']
sentimentRating = sentimentAnalysis(tweet)
saveMe = tweet + "::" + sentimentRating+'\n'
output = open("output.csv", "a")
output.write(saveMe)
output.close()
return True
def on_error(self, status):
print status
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["car"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment