-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathget_tweets.py
54 lines (41 loc) · 1.5 KB
/
get_tweets.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import csv
#http://www.tweepy.org/
import tweepy
#Get your Twitter API credentials and enter them here
consumer_key = "your_consumer_key"
consumer_secret = "your_consumer_secret"
access_key = "your_access_key"
access_secret = "your_access_secret"
#method to get a user's last tweets
def get_tweets(username):
#http://tweepy.readthedocs.org/en/v3.1.0/getting_started.html#api
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
#set count to however many tweets you want
number_of_tweets = 100
#get tweets
tweets_for_csv = []
for tweet in tweepy.Cursor(api.user_timeline, screen_name = username).items(number_of_tweets):
#create array of tweet information: username, tweet id, date/time, text
tweets_for_csv.append([username, tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")])
#write to a new csv file from the array of tweets
outfile = username + "_tweets.csv"
print "writing to " + outfile
with open(outfile, 'w+') as file:
writer = csv.writer(file, delimiter=',')
writer.writerows(tweets_for_csv)
#if we're running this as a script
if __name__ == '__main__':
#get tweets for username passed at command line
if len(sys.argv) == 2:
get_tweets(sys.argv[1])
else:
print "Error: enter one username"
#alternative method: loop through multiple users
# users = ['user1','user2']
# for user in users:
# get_tweets(user)