Last active
March 7, 2018 14:11
-
-
Save cvkmohan/8836e24a6a18bd28b8191b3f8efd30da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import re | |
import tweepy | |
from tweepy import OAuthHandler | |
from textblob import TextBlob | |
class TwitterClient(object): | |
''' | |
Generic Twitter Class for sentiment analysis. | |
''' | |
def __init__(self): | |
# keys and tokens from the Twitter Dev Console | |
consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
# 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") | |
def get_tweets(self, query, count = 10): | |
# empty list to store parsed tweets | |
tweets = [] | |
try: | |
# call twitter api to fetch tweets | |
fetched_tweets = self.api.search(q = query, count = count) | |
# parsing tweets one by one | |
for tweet in fetched_tweets: | |
# empty dictionary to store required params of a tweet | |
parsed_tweet = {} | |
parsed_tweet['text'] = tweet.text | |
# appending parsed tweet to tweets list | |
if tweet.retweet_count > 0: | |
# if tweet has retweets, ensure that it is appended only once | |
if parsed_tweet not in tweets: | |
tweets.append(parsed_tweet) | |
else: | |
tweets.append(parsed_tweet) | |
# return parsed tweets | |
return tweets | |
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 = 'Narendra Modi', count = 10) | |
print("\n\nTweets:") | |
for tweet in tweets[:10]: | |
print(tweet['text']) | |
if __name__ == "__main__": | |
# calling main function | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment