Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. # import all modules
  2. from pymongo import MongoClient
  3. from tweepy.streaming import StreamListener
  4. from tweepy import OAuthHandler
  5. from tweepy import Stream
  6. from datetime import datetime
  7. import json
  8.  
  9. # Connect to mongoDB
  10. client = MongoClient('localhost', 27017)
  11. db = client['mgo_db']
  12. collection = db['time100_collection']
  13.  
  14.  
  15. # Set the twitter API keys and tokens
  16. consumer_key="#########################"
  17. consumer_secret="##################################################"
  18. access_token="##################################################"
  19. access_token_secret="#############################################"
  20.  
  21.  
  22. # Define a listener handles tweets that are received from the stream.
  23. # This is a basic listener that just prints received tweets to stdout.
  24. class StdOutListener(StreamListener):
  25.  
  26. def on_data(self, data):
  27. tweet = json.loads(data)
  28. try:
  29. tweet_id = collection.insert_one(tweet).inserted_id
  30. print(tweet_id)
  31. except:
  32. pass
  33. return True
  34.  
  35. def on_error(self, status):
  36. print(status)
  37.  
  38. # Define the main function.
  39. if __name__ == '__main__':
  40. l = StdOutListener()
  41. auth = OAuthHandler(consumer_key, consumer_secret)
  42. auth.set_access_token(access_token, access_token_secret)
  43. stream = Stream(auth, l)
  44. # Set the track list.
  45. track_list = ['TIME100']
  46. stream.filter(track = track_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement