Created
October 11, 2016 19:19
-
-
Save vitormeriat/54d363c41b0a397d39c47e14ce541bdc to your computer and use it in GitHub Desktop.
Simple reader Twitter streaming data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Importing modules Tweepy, Datetime and Json | |
from tweepy.streaming import StreamListener | |
from tweepy import OAuthHandler | |
from tweepy import Stream | |
import json | |
# Replace following access keys of your api twitter. Consumer Key, Consumer Secret, Access Token and Access Token Secret | |
consumer_key = "{your_consumer_key}" | |
consumer_secret = "{your_consumer_secret}" | |
access_token = "{your_access_token}" | |
access_token_secret = "{your_access_token_secret}" | |
# Creating authentication keys | |
auth = OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
# Creating a class to capture the Twitter data stream | |
class MyListener(StreamListener): | |
def on_data(self, dados): | |
tweet = json.loads(dados) | |
created_at = tweet["created_at"] | |
id_str = tweet["id_str"] | |
text = tweet["text"] | |
obj = {"created_at":created_at,"id_str":id_str,"text":text,} | |
tweetind = col.insert_one(obj).inserted_id | |
print (obj) | |
return True | |
mylistener = MyListener() | |
mystream = Stream(auth, listener = mylistener) | |
# Creating a list of keywords to search the Tweets | |
keywords = ['1', '2', '3', 'n'] | |
# Starting filter and tracks tweets | |
mystream.filter(track = keywords) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment