SHARE
TWEET

Untitled

a guest Mar 19th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from tweepy.streaming import StreamListener
  2. from tweepy import OAuthHandler
  3. from tweepy import Stream
  4. import twitter_credentials
  5. import json
  6.  
  7. class TwitterStreamer():
  8.  
  9.     def __init__(self):
  10.         pass
  11.  
  12.     def stream_tweets(self, fetched_tweets_filename, hash_tag_list):
  13.        
  14.         listener = StdOutListener(fetched_tweets_filename)
  15.         auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)
  16.         auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)
  17.         stream = Stream(auth, listener)
  18.         stream.filter(track=hash_tag_list)
  19.        
  20. class StdOutListener(StreamListener):
  21.        
  22.     def __init__(self, fetched_tweets_filename):        
  23.        
  24.         auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)
  25.         self.fetched_tweets_filename = fetched_tweets_filename
  26.  
  27.     def on_data(self, data):
  28.         try:
  29.             with open(self.fetched_tweets_filename, 'a') as tf:
  30.                 tf.write({'id':json.loads(data)['id']
  31.                          ,'text':json.loads(data)['text']})
  32.            
  33.             return True
  34.         except BaseException as e:
  35.             print("Error on_data {}".format(e))
  36.         return True
  37.    
  38.     def on_error(self, status):
  39.         print(status)
  40.        
  41. if __name__ == '__main__':
  42.  
  43. """
  44. colocar um while 1==1
  45. """
  46.     hash_tag_list = ["bolsonaro"]
  47.     fetched_tweets_filename = "tweets.txt"
  48.     """
  49.     colocar um loop que quebra a cada 5 minutos
  50.     """
  51.     twitter_streamer = TwitterStreamer()
  52.     twitter_streamer.stream_tweets(fetched_tweets_filename, hash_tag_list)
  53.     """
  54.     colocar um loop pra ler tweets.txt e separar em N jsons e salvar individualente
  55.     """
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