-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
136 lines (101 loc) · 4.27 KB
/
app.py
File metadata and controls
136 lines (101 loc) · 4.27 KB
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
from tweepy import API
from tweepy import Cursor
import tweepy
from tweepy import OAuth2BearerHandler
from tweepy import Stream
from textblob import TextBlob
from tabulate import tabulate
import twitter_credentials
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import re
# Getting our api objects, which contains the methods for accessing twitter data
class TwitterFetch():
def __init__(self, twitter_user = None):
self.auth = TwitterAuthenticator().authenticate()
self.twitter_api = API(self.auth)
self.twitter_user = twitter_user
def get_twitter_api(self):
return self.twitter_api
def get_user_timeline_tweets(self, num_tweets):
tweets = []
for tweet in Cursor(self.twitter_api.user_timeline, id=self.twitter_user).items(num_tweets):
tweets.append(tweet)
return tweets
def get_friends(self, num_friends):
friend_list = []
for friend in Cursor(self.twitter_api.friends, id=self.twitter_user).items(num_friends):
friend_list.append(friend)
return friend_list
def get_home_timeline_tweets(self, num_tweets):
home_timeline_tweets = []
for tweet in Cursor(self.twitter_api.home_timeline, id=self.twitter_user).items(num_tweets):
home_timeline_tweets.append(tweet)
return home_timeline_tweets
# Authenticating the credentials to the twitter api
class TwitterAuthenticator():
def authenticate(self):
auth = OAuth2BearerHandler(twitter_credentials.BEARER_TOKEN)
return auth
# Processing tweets in real time.
class TwitterStream():
def __init__(self):
self.twitter_autenticator = TwitterAuthenticator()
def stream_tweets(self, fetched_tweets_filename, hash_tag_list):
#Connect to twitter streaming api
listener = TwitterListener(fetched_tweets_filename)
auth = self.twitter_autenticator.authenticate()
stream = Stream(auth, listener)
# Filter data with some paramters
stream.filter(track=hash_tag_list)
# Configuration of twitter streamer
class TwitterListener(tweepy.Stream):
def __init__(self, fetched_tweets_filename):
self.fetched_tweets_filename = fetched_tweets_filename
def on_data(self, data):
try:
print(data)
with open(self.fetched_tweets_filename, 'a') as tf:
tf.write(data)
return True
except BaseException as e:
print("Error %s" % str(e))
return True
def on_error(self, status):
if status == 420:
return False
print(status)
# Preprocessing the string and getting the sentiment value.
class TweetAnalyzer():
#Remove characters that we don't want
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 1
elif analysis.sentiment.polarity == 0:
return 0
else:
return -1
# We give a dataframe format to our tweets.
def tweets_to_data_frame(self, tweets):
df = pd.DataFrame(data = [tweet.text for tweet in tweets], columns=['tweets'])
df['id'] = np.array([tweet.id for tweet in tweets])
df['length'] = np.array([len(tweet.text) for tweet in tweets])
df['date'] = np.array([tweet.created_at for tweet in tweets])
df['platform'] = np.array([tweet.source for tweet in tweets])
df['likes'] = np.array([tweet.favorite_count for tweet in tweets])
df['retweets'] = np.array([tweet.retweet_count for tweet in tweets])
return df
if __name__ == '__main__':
twitter_api = TwitterFetch()
tweet_analyzer = TweetAnalyzer()
api = twitter_api.get_twitter_api()
#Modify this line for analyzing other twitter user(ej. : elonmusk)
user_to_analyze = "elonmusk"
tweets = api.user_timeline(screen_name= user_to_analyze, count = 200)
df = tweet_analyzer.tweets_to_data_frame(tweets)
df['sentiment'] = np.array([tweet_analyzer.analyze_sentiment(tweet) for tweet in df['tweets']])
print(tabulate(df, headers = 'keys', tablefmt = 'pretty'))