-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_tweets_by_user
51 lines (43 loc) · 1.43 KB
/
extract_tweets_by_user
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
import tweepy
from textblob import TextBlob
consumer_key = "CENSORED PRIVATE INFORMATION"
consumer_secret = "CENSORED PRIVATE INFORMATION"
access_key = "CENSORED PRIVATE INFORMATION"
access_secret = "CENSORED PRIVATE INFORMATION"
# API's setup:
def twitter_setup():
"""
Utility function to setup the Twitter's API
with our access keys provided.
"""
# Authentication and access using keys:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
# Return API with authentication:
api = tweepy.API(auth)
return api
def define_polarity(string):
analyze_polar = TextBlob(string).sentiment.polarity
if analyze_polar > 0:
return "positive"
elif analyze_polar == 0:
return "neutral"
else:
return "negative"
def extract_tweets(twitter_handle):
# We create a tweet list as follows:
extractor = twitter_setup()
# We create a tweet list as follows:
tweets = extractor.user_timeline(screen_name=twitter_handle, count=200)
print("Number of tweets extracted: {}.\n".format(len(tweets)))
# We print the most recent 5 tweets:
#for tweet in tweets[:200]:
# print(tweet.text)
# print()
#
for i in range(10):
print(tweets[i].id)
print(tweets[i].created_at)
print(tweets[i].text, define_polarity(tweets[i].text))
print()
extract_tweets("realdonaldtrump")