Skip to content

Instantly share code, notes, and snippets.

@Yoxem
Forked from dmn001/tweet_dumper.py
Last active August 29, 2015 14:06
Show Gist options
  • Save Yoxem/0b8de721bac013433b4c to your computer and use it in GitHub Desktop.
Save Yoxem/0b8de721bac013433b4c to your computer and use it in GitHub Desktop.
list tweet dumper - get word of tweets of the timeline of a list.
#!/usr/bin/env python
# encoding: utf-8
#list_tweet_dumper - get word of tweets of the timeline of a list.
import tweepy #https://github.com/tweepy/tweepy
import csv
#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
#name of user & slug name of list
user_name = "Yoxem" #example
list_slug_name = "Holo" #example
def get_all_tweets(screen_name):
#authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
#initialize a list to hold all the tweepy Tweets
alltweets = []
new_tweets = api.list_timeline(user_name, list_slug_name)
i = 1
for tweet in new_tweets:
print i
print tweet.text
i += 1
#save most recent tweets
alltweets.extend(new_tweets)
#save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
print oldest
while len(new_tweets) > 0:
print "getting tweets before %s" % (oldest)
#all subsiquent requests use the max_id param to prevent duplicates
new_tweets = api.list_timeline(user_name, list_slug_name, max_id=oldest)
for tweet in new_tweets:
print i
print tweet.text
i += 1
#save most recent tweets
alltweets.extend(new_tweets)
#update the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
print oldest
print "...%s tweets downloaded so far" % (len(alltweets))
#transform the tweepy tweets into a 2D array that will populate the csv
print "Ok."
outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]
#write the csv
with open('%s_tweets.csv' % screen_name, 'wb') as f:
writer = csv.writer(f)
writer.writerow(["id","created_at","text"])
writer.writerows(outtweets)
pass
if __name__ == '__main__':
#pass in the username of the account you want to download
get_all_tweets("Yoxem")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment