m2skills

all_tweets_of user python

Oct 13th, 2017
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. # program to download all tweets of a users on twitter and store in csv file
  2. import sys
  3. import csv
  4. import tweepy  # http://www.tweepy.org/ or pip install tweepy
  5. import json
  6.  
  7. # Get your Twitter API credentials and enter them here
  8. consumer_key = ''
  9. consumer_secret = ''
  10. access_key = ''
  11. access_secret = ''
  12.  
  13. # method to get a user's last 200 tweets
  14. def get_tweets(username):
  15.  
  16.     # http://tweepy.readthedocs.org/en/v3.1.0/getting_started.html#api
  17.     auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  18.     auth.set_access_token(access_key, access_secret)
  19.     api = tweepy.API(auth)
  20.    
  21.     alltweets = []
  22.    
  23.     #set count to however many tweets you want; twitter only allows 200 at once
  24.     number_of_tweets = 200
  25.  
  26.     #get tweets
  27.     tweets = api.user_timeline(screen_name = username,count = number_of_tweets)
  28.  
  29.     #save most recent tweets
  30.     alltweets.extend(tweets)
  31.    
  32.     #save the id of the oldest tweet less one
  33.     oldest = tweets[-1].id - 1
  34.    
  35.     #keep grabbing tweets until there are no tweets left to grab
  36.     while len(tweets) > 0:
  37.         print ("getting tweets before %s" % (oldest))
  38.        
  39.         #all subsiquent requests use the max_id param to prevent duplicates
  40.         tweets = api.user_timeline(screen_name = username, count = 200, max_id=oldest)
  41.        
  42.         #save most recent tweets
  43.         alltweets.extend(tweets)
  44.        
  45.         #update the id of the oldest tweet less one
  46.         oldest = alltweets[-1].id - 1
  47.        
  48.         print ("...%s tweets downloaded so far" % (len(alltweets)))
  49.    
  50.     # for the example we are extracting only 6 fields
  51.     # You can add to the list and extract more fields
  52.     #transform the tweepy tweets into a 2D array that will populate the csv
  53.     outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8"), tweet.in_reply_to_screen_name, tweet.user.name, tweet.user.screen_name] for tweet in alltweets]
  54.    
  55.     #write to a new csv file from the array of tweets
  56.     print ("writing to {0}_tweets.csv".format(username))
  57.     with open("{0}_tweets.csv".format(username) , 'w+') as file:
  58.         writer = csv.writer(file, delimiter=',')
  59.         writer.writerows(outtweets)
  60.  
  61.  
  62. if __name__ == '__main__':
  63.  
  64.     # put the usernames in the below list
  65.     # names = ['user1','user2']
  66.     names = []
  67.     userCount = 0
  68.     for username in names:     
  69.         try:
  70.             print("User number = " + str(userCount))
  71.             print("starting with user " + username)
  72.             #get tweets for username passed at command line
  73.             get_tweets(username)
  74.             print ("\nDownloaded Files for " + username + "\n")
  75.             userCount += 1
  76.         except:
  77.             pass
Add Comment
Please, Sign In to add comment