Sentiment Analysis Python
Sentiment Analysis Python
import tweepy
from tweepy import OAuthHandler
from textblob import TextBlob
class TwitterClient(object):
'''
Generic Twitter Class for sentiment analysis.
'''
def __init__(self):
'''
Class constructor or initialization method.
'''
# keys and tokens from the Twitter Dev Console
consumer_key = 'UGltbTQESNIVtf8pOIrJWCpG7'
consumer_secret = '57RdUCtKEFYaDx4CFCsghZ1hat9FhYZ1RdL7YebYQltRzSfyXi'
access_token = '850196913404035072-TtDkG3unFCq6CkPcYz92uk5TWBaN38w'
access_token_secret = 'vw36X0r3t4iaLjBxb4uDCzORV18uY09DHEJiUTZ4Nxqeb'
# attempt authentication
try:
# create OAuthHandler object
self.auth = OAuthHandler(consumer_key, consumer_secret)
# set access token and secret
self.auth.set_access_token(access_token, access_token_secret)
# create tweepy API object to fetch tweets
self.api = tweepy.API(self.auth)
except:
print("Error: Authentication Failed")
try:
# call twitter api to fetch tweets
fetched_tweets = self.api.search(q = query, count = count)
except tweepy.TweepError as e:
# print error (if any)
print("Error : " + str(e))
def main():
# creating object of TwitterClient Class
api = TwitterClient()
# calling function to get tweets
tweets = api.get_tweets(query = 'Donald Trump', count = 200)
if __name__ == "__main__":
# calling main function
main()