Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. '''
  2. A script for downloading a Twitter user's
  3. entire tweet archive
  4. '''
  5. import csv
  6. import os
  7. import argparse
  8.  
  9. import tweepy
  10.  
  11. PATH = os.path.dirname(os.path.realpath(__file__))
  12.  
  13.  
  14. def get_args():
  15. parser = argparse.ArgumentParser()
  16. parser.add_argument('-u', '--username', type=str, help='The user you would like to get an archive of')
  17. return parser.parse_args()
  18.  
  19.  
  20. def get_api():
  21. '''
  22. Creates an instance of the tweepy API class
  23. '''
  24. with open(PATH+'/config') as f:
  25. api_key = f.readline().strip()
  26. api_secret = f.readline().strip()
  27. access_token = f.readline().strip()
  28. access_token_secret = f.readline().strip()
  29. auth = tweepy.OAuthHandler(api_key, api_secret)
  30. auth.set_access_token(access_token, access_token_secret)
  31. return tweepy.API(auth)
  32.  
  33.  
  34. def get_tweets(username):
  35. api = get_api()
  36. tweets = []
  37. current = api.user_timeline(screen_name=username, count=200)
  38. tweets.extend(current)
  39. last_id = tweets[-1].id - 1
  40. while len(current) > 0:
  41. current = api.user_timeline(screen_name=username, count=200, max_id=last_id)
  42. if len(current) > 0:
  43. tweets.extend(current)
  44. last_id = tweets[-1].id - 1
  45. return [[tweet.id_str, tweet.created_at, tweet.text.encode('utf-8')] for tweet in tweets]
  46.  
  47.  
  48. if __name__ == '__main__':
  49. args = get_args()
  50. tweets = get_tweets(args.username)
  51. filename = '%s_tweets.csv' % args.username
  52. print 'Got %d tweets from user %s' % (len(tweets), args.username)
  53. print
  54. print 'Writing to CSV file named %s' % filename
  55. with open(filename, 'wb') as csv_file:
  56. writer = csv.writer(csv_file)
  57. writer.writerow(['id', 'created_at', 'text'])
  58. writer.writerows(tweets)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement