forked from gitlaura/get_tweets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_tweets.py
53 lines (39 loc) · 1.51 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
#!/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 100 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; twitter only allows 200 at once
number_of_tweets = 100
#get tweets
tweets = api.user_timeline(screen_name = username,count = number_of_tweets)
#create array of tweet information: username, tweet id, date/time, text
tweets_for_csv = [[username,tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in tweets]
#write to a new csv file from the array of tweets
print "writing to {0}_tweets.csv".format(username)
with open("{0}_tweets.csv".format(username) , '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)