-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter.py
More file actions
54 lines (39 loc) · 1.06 KB
/
twitter.py
File metadata and controls
54 lines (39 loc) · 1.06 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
def main():
# connect to twitter
api = connect_to_twitter()
# retrieve tweets
tweets = get_tweets(api)
# run sentiment analysis on tweets
sentiment_analysis(tweets)
def connect_to_twitter():
"""
connect to twitter using tweepy
"""
# authenticate
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
# connect to twitter
api = tweepy.API(auth)
return api
def get_tweets(api):
"""
retrieve tweets addressed to @talktoboi
"""
# get tweets
tweets = api.search(q='@talktoboi', count=100)
return tweets
def sentiment_analysis(tweets):
"""
run sentiment analysis on tweets
"""
# run sentiment analysis on tweets
for tweet in tweets:
# get text
text = tweet.text
# get sentiment
sentiment = TextBlob(text).sentiment.polarity
# print results
print('\nTweet: {}'.format(text))
print('Sentiment: {}'.format(sentiment))
if __name__ == '__main__':
main()