daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Nov 21st, 2017 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # usernames is the list of my users
  2. for i in usernames:
  3.     get_all_tweets(i)
  4.    
  5. # Load the required Libraries
  6. import tweepy #https://github.com/tweepy/tweepy
  7. import csv
  8. import sys
  9.  
  10. #Twitter API credentials
  11.  
  12. consumer_key = "fRw12aumIqkAWD6PP5ZHk7vva"
  13. consumer_secret = "K9K0yL2pwngp3JXEdMGWUOEB7AaGWswXcq72WveRvnD4ZSphNQ"
  14. access_key = "771287280438968320-XnbtNtBt40cs6gUOk6F9bjgmUABM0qG"
  15. access_secret = "afUppGRqcRi2p9fzLhVdYQXkfMEm72xduaWD6uNs3HhKg"
  16.  
  17. def get_all_tweets(screen_name):
  18.  
  19.     #authorize twitter, initialize tweepy
  20.     auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  21.     auth.set_access_token(access_key, access_secret)
  22.     api = tweepy.API(auth)
  23.  
  24.     #initialize a list to hold all the tweepy Tweets
  25.     alltweets = []    
  26.  
  27.     #make initial request for most recent tweets (200 is the maximum allowed count)
  28.     new_tweets = api.user_timeline(screen_name = screen_name,count=200)
  29.  
  30.     #save most recent tweets
  31.     alltweets.extend(new_tweets)
  32.  
  33.     #save the id of the oldest tweet less one
  34.     oldest = alltweets[-1].id - 1
  35.  
  36.     #keep grabbing tweets until there are no tweets left to grab
  37.     while len(new_tweets) > 0:
  38.         print ("getting tweets before %s" % (oldest))
  39.  
  40.         #all subsiquent requests use the max_id param to prevent duplicates
  41.         new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
  42.  
  43.         #save most recent tweets
  44.         alltweets.extend(new_tweets)
  45.  
  46.         #update the id of the oldest tweet less one
  47.         oldest = alltweets[-1].id - 1
  48.  
  49.         print ("...%s tweets downloaded so far" % (len(alltweets)))
  50.  
  51.     #transform the tweepy tweets into a 2D array that will populate the csv    
  52.     user_tweet = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]
  53.  
  54.  
  55.     connection = MongoClient()
  56.     db = connection.Stage2_DataBase
  57.  
  58.     db.screen_name.insert(user_tweet)
  59.  
  60.     pass
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top