-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_data.py
More file actions
58 lines (42 loc) · 1.41 KB
/
Copy pathget_data.py
File metadata and controls
58 lines (42 loc) · 1.41 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
import csv
import sys
import tweepy
from cred import *
#get twitter api using credentials in cred.py
def get_api():
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
try:
api.verify_credentials()
print('successfully created twitter API')
return api
except Exception as e:
print('Error creating API')
raise e
#get tweets from twitter users using a list of their screen names
def get_tweets(twitter_api, username):
output = 'data/train_data.csv'
number_of_tweets = 2000
tweet_list = []
#make sure not to include retweets
for tweet in tweepy.Cursor(twitter_api.user_timeline, tweet_mode='extended', include_rts=False, screen_name=username).items(number_of_tweets):
tweet_list.append([tweet.full_text])
#write tweets onto a csv file
with open(output, 'a') as file:
writer = csv.writer(file, delimiter=',')
writer.writerows(tweet_list)
def main():
#supply users here
usernames=[]
#get api
api = get_api()
try:
#get tweets
for user in usernames:
print('Currently on ' + user)
get_tweets(api, user)
except:
print('error with: ' + user + ' moving to next user...')
if __name__ == "__main__":
main()