-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterhook.py
76 lines (59 loc) · 2.13 KB
/
twitterhook.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
#Twitter hook
# import tweepy
import tweepy as tw
#reads login info
f=open("../twitter_credentials.txt","r")
lines=f.readlines()
# your Twitter API key and API secret
my_api_key=lines[1].rstrip("\n")
my_api_secret=lines[3].rstrip("\n")
access_token=lines[5].rstrip("\n")
access_token_secret=lines[7].rstrip("\n")
f.close()
#print(twitter_key)
#print(twitter_secret)
#print(access_token)
#print(access_token_secret)
# authenticate
auth = tw.OAuthHandler(my_api_key, my_api_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)
search_query = "#defundthepolice -filter:retweets"
# get tweets from the API
tweets = tw.Cursor(api.search,
q=search_query,
lang="en",
since="2020-09-16").items(50)
# store the API responses in - list third part
tweets_copy = []
for tweet in tweets:
try:
tweets_copy.append(tweet)
except tw.TweepError as e:
print("Something went wrong")
print("Tweepy Error: {}".format(e))
print("Total Tweets fetched:", len(tweets_copy))
#organize tweets from a data frame
import pandas as pd
# intialize the dataframe
tweets_df = pd.DataFrame()
# populate the dataframe
for tweet in tweets_copy:
hashtags = []
try:
for hashtag in tweet.entities["hashtags"]:
hashtags.append(hashtag["text"])
text = api.get_status(id=tweet.id, tweet_mode='extended').full_text
except:
pass
tweets_df = tweets_df.append(pd.DataFrame({'user_name': tweet.user.name,
'user_location': tweet.user.location,\
'user_description': tweet.user.description,
'user_verified': tweet.user.verified,
'date': tweet.created_at,
'text': text,
'hashtags': [hashtags if hashtags else None],
'source': tweet.source}))
tweets_df = tweets_df.reset_index(drop=True)
# show the dataframe
tweets_df.head()