-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_tweets.py
More file actions
80 lines (70 loc) · 3.14 KB
/
Copy pathget_tweets.py
File metadata and controls
80 lines (70 loc) · 3.14 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
###############################################################
# imports
###############################################################
import config
import tweepy
import pandas as pd
###############################################################
# authorize twitter
###############################################################
def auth_init(consumer_key,
consumer_secret,
access_token_key,
access_token_secret
):
#Twitter API Oatuh
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) #replace with your own consumer key and consumer secret key
auth.set_access_token(access_token_key, access_token_secret) #replace with your own access tokey key and acccess toke nsecret key
api = tweepy.API(auth)
return api
###############################################################
# get tweets of user
###############################################################
def get_tweets(userID):
tweets_data = []
api = auth_init(config.consumer_key, config.consumer_secret, config.access_token_key, config.access_token_secret) #pass your won twiiter api credentials
tweets = api.user_timeline(screen_name=userID,
count=10,
include_rts=False,
tweet_mode='extended'
)
tweets_data.extend(tweets)
oldest_tweet = tweets_data[-1].id-1
while(len(tweets) > 0):
tweets = api.user_timeline(screen_name=userID,
count=200,
include_rts=False,
max_id=oldest_tweet,
tweet_mode='extended'
)
tweets_data.extend(tweets)
oldest_tweet = tweets_data[-1].id-1
print('tweets downloaded till now {}'.format(len(tweets_data)))
return tweets_data
###############################################################
# get tweets of user
###############################################################
def get_tweets_text(tweets_data):
tweets_text = []
tweets_text.extend([str(tweet.full_text)] for tweet in tweets_data) #store only the text of the tweets
return tweets_text
###############################################################
# convert list of tweets to csv file
###############################################################
def tweets_csv(tweets):
tw_df = pd.DataFrame(tweets, columns=["tweet"])
tw_df.to_csv('tweets.csv', index = False, encoding='utf-8') #create 'tweets.csv' to store tweets
###############################################################
# main
###############################################################
def main():
user_entry = input('Enter twitter handle: ')
tweets_arr = get_tweets(user_entry)
tweet_text_data = get_tweets_text(tweets_arr)
print('latest tweet:\n{}'.format(tweet_text_data[0]))
tweets_csv(tweet_text_data)
###############################################################
# driver code
###############################################################
if __name__ == '__main__':
main()