-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_analyser_new.py
158 lines (128 loc) · 5.49 KB
/
tweet_analyser_new.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from tweepy import API
from tweepy import Cursor
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import twitter_credentials as credentials
from textblob import TextBlob
import pandas as pd
import re
import numpy as np
class tweetListener(StreamListener):
"""
Listener that gives out tweets received by stdout.
"""
def __init__(self, file_of_tweets):
self.file_of_tweets = file_of_tweets
def on_data(self, data):
try:
with open(self.file_of_tweets, 'a') as f:
f.write(data)
return True
except Exception as e:
print(f"Error with on_data method: {e}")
return True
def on_error(self, error):
if error == 420:
#error 420 is given out by twitter when it feels that you are abusing their data
#happens when rate limits occur
print("Not a good time to access tweets. Killing the connection.")
return False
print(error)
#end of class tweetListener
class twitterAuthenticator():
"""
Uses OAuthHandler to authenticate keys and tokens from Twitter API
"""
def authenticate(self):
#method to obtain keys or tokens
auth = OAuthHandler(credentials.consumer_key, credentials.consumer_key_secret)
auth.set_access_token(credentials.access_token, credentials.access_token_secret)
return auth
#end of class twitterAuthenticator
class twitterStreamer():
"""
This class streams and processes live tweets.
"""
def __init__(self):
self.twitterAuthenticatorObj = twitterAuthenticator()
def stream_tweets(self, file_of_tweets, list_of_keywords):
# method to handle twitter authentication and API
listener = tweetListener(file_of_tweets)
auth = self.twitterAuthenticatorObj.authenticate()
#streaming
stream = Stream(auth, listener)
stream.filter(track=list_of_keywords)
#end of class twitterStreamer
class twitterClient():
"""
Gets tweets from specific timelines
"""
def __init__(self,twitter_user=None):
self.auth = twitterAuthenticator().authenticate()
self.client = API(self.auth)
self.twitter_user = twitter_user
def get_twitter_client(self):
return self.client
def get_tweets_from_user(self,num_of_tweets):
#gets tweets from given username
list_of_tweets = []
for tweet in Cursor(self.client.user_timeline, id=self.twitter_user).items(num_of_tweets):
list_of_tweets.append(tweet)
return list_of_tweets
def get_friendlist_from_client(self,num_of_friends):
list_of_friends=[]
for friend in Cursor(self.client.friends, id=self.twitter_user).items(num_of_friends):
list_of_friends.append(friend)
return list_of_friends
def get_tweets_from_home_timeline(self, num_of_tweets):
list_of_home_tweets=[]
for tweet in Cursor(self.client.home_timeline,id=self.twitter_user).items(num_of_tweets):
list_of_home_tweets.append(tweet)
return list_of_home_tweets
def get_keyworded_tweets(self, num_of_tweets, keyword):
keyword_tweets = []
for tweet in Cursor(self.client.search,q=[keyword]).items(num_of_tweets):
keyword_tweets.append(tweet)
return keyword_tweets
#end of twitterClient
class tweetAnalyzer():
"""
methods to analyse tweets
"""
def tweets_to_dataframe(self, tweets):
df = pd.DataFrame(data=[tweet.text for tweet in tweets], columns = ['Tweets'])
#df['Tweet ID'] = np.array([tweet.id for tweet in tweets])
#df['Word Count'] = np.array([len(tweet.text) for tweet in tweets])
#df['Date'] = np.array([tweet.created_at for tweet in tweets])
#df['Likes'] = np.array([tweet.favourites_count for tweet in tweets])
#df['Retweets'] = np.array([tweet.retweet_count for tweet in tweets])
#df['Source'] = np.array([tweet.source for tweet in tweets])
return df
def clean_tweet(self, tweet):
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())
def analyze_sentiment(self, tweet):
analysis = TextBlob(self.clean_tweet(tweet))
if analysis.sentiment.polarity > 0:
return 2
elif analysis.sentiment.polarity == 0:
return 1
else:
return 0
#end of tweetAnalyzer
if __name__ == "__main__":
twitterClientObj = twitterClient()
tweetAnalyzerObj = tweetAnalyzer()
client_api = twitterClientObj.get_twitter_client()
keyword = input("Enter the keyword for which the tweets are to be extracted:")
#ip = input("Enter Twitter ID of person whose tweets are to be analysed:")
#count_of_ip_tweets = int(input("Enter the number of tweets to be analysed:"))
#tweets = client_api.user_timeline(screen_name=ip, count=count_of_ip_tweets)
keyworded_tweets=twitterClientObj.get_keyworded_tweets(50,keyword)
#print(tweets)
tweet_df = tweetAnalyzerObj.tweets_to_dataframe(keyworded_tweets)
#tweet_df['Sentiment'] = np.array([tweetAnalyzerObj.analyze_sentiment(tweet) for tweet in tweet_df['Tweets']])
print(tweet_df)
header = ["Tweets"]
tweet_df.to_csv(keyword+"_tweets.csv", columns = header)
# print(f"The average sentiment of the tweets is {round(np.mean(tweet_df['Sentiment']),4)}")