Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- A script for downloading a Twitter user's
- entire tweet archive
- '''
- import csv
- import os
- import argparse
- import tweepy
- PATH = os.path.dirname(os.path.realpath(__file__))
- def get_args():
- parser = argparse.ArgumentParser()
- parser.add_argument('-u', '--username', type=str, help='The user you would like to get an archive of')
- return parser.parse_args()
- def get_api():
- '''
- Creates an instance of the tweepy API class
- '''
- with open(PATH+'/config') as f:
- api_key = f.readline().strip()
- api_secret = f.readline().strip()
- access_token = f.readline().strip()
- access_token_secret = f.readline().strip()
- auth = tweepy.OAuthHandler(api_key, api_secret)
- auth.set_access_token(access_token, access_token_secret)
- return tweepy.API(auth)
- def get_tweets(username):
- api = get_api()
- tweets = []
- current = api.user_timeline(screen_name=username, count=200)
- tweets.extend(current)
- last_id = tweets[-1].id - 1
- while len(current) > 0:
- current = api.user_timeline(screen_name=username, count=200, max_id=last_id)
- if len(current) > 0:
- tweets.extend(current)
- last_id = tweets[-1].id - 1
- return [[tweet.id_str, tweet.created_at, tweet.text.encode('utf-8')] for tweet in tweets]
- if __name__ == '__main__':
- args = get_args()
- tweets = get_tweets(args.username)
- filename = '%s_tweets.csv' % args.username
- print 'Got %d tweets from user %s' % (len(tweets), args.username)
- print
- print 'Writing to CSV file named %s' % filename
- with open(filename, 'wb') as csv_file:
- writer = csv.writer(csv_file)
- writer.writerow(['id', 'created_at', 'text'])
- writer.writerows(tweets)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement