Created
October 21, 2018 20:19
-
-
Save EricMarques/5bcaa21fa307411638bb60b651be32f2 to your computer and use it in GitHub Desktop.
Streaming Twitter
This file contains 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
# Instala o pacote tweepy | |
#!pip install tweepy | |
# Importando os módulos Tweepy, Datetime e Json | |
from tweepy.streaming import StreamListener | |
from tweepy import OAuthHandler | |
from tweepy import Stream | |
from datetime import datetime | |
import json | |
# Consumer Key | |
consumer_key = "consumer_key" | |
# AConsumer Secret | |
consumer_secret = "consumer_secret" | |
# Access Token | |
access_token = "access_token" | |
# Access Token Secret | |
access_token_secret = "access_token_secret" | |
# Criar as chaves de autenticação | |
auth = OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
# Classe para capturar os stream de dados do Twitter e armazenar no MongoDB(Banco de Dados) | |
class MyListener(StreamListener): | |
def on_data(self, dados): | |
tweet = json.loads(dados) | |
created_at = tweet["created_at"] | |
id_str = tweet["id_str"] | |
in_reply_to_screen_name = tweet["in_reply_to_screen_name"] | |
text = tweet["text"] | |
obj = {"created_at":created_at,"id_str":id_str, "in_reply_to_screen_name": in_reply_to_screen_name, "text":text, } #"name" : ["user"]["name"], "screen_name" : ["user"]["screen_name"], | |
tweetind = col.insert_one(obj)#.inserted_id | |
print (obj) | |
return True | |
# Criando o objeto mylistener | |
mylistener = MyListener() | |
# Criando o objeto mystream | |
mystream = Stream(auth, listener = mylistener) | |
# Importando do PyMongo o módulo MongoClient | |
from pymongo import MongoClient | |
# Conexão ao MongoDB | |
client = MongoClient('localhost', 27017) | |
# Criando o banco de dados twitterdb | |
db = client.twitterdb | |
# Criando a collection "col" | |
col = db.tweets | |
# Lista de palavras chave para buscar nos Tweets - Agências Espaciais | |
keywords = ['@NASA', '@SANSA7', '@AFSpace', '@CNSA_en', '@esa', '@isa', '@ILSpaceAgency', '@ASI_spazio', '@isro', '@JAXA_en', '@CNES', '@roscosmos',] | |
# Iniciando o filtro e gravando os tweets no MongoDB | |
mystream.filter(track=keywords) | |
#Fechando conexão com mystream | |
mystream.disconnect() |
fabiomathu
commented
Jul 16, 2020
via email
Ok, entao, por favor, passa seu WhatsApp
e Skype por favor
Em qui, 16 de jul de 2020 às 4:18 PM, Eric Marques <notifications@github.com>
escreveu:
… ***@***.**** commented on this gist.
------------------------------
Boa tarde Fabio!
Podemos sim, só marcar dia e horário que vejo se consigo entrar em
conferência com você.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/5bcaa21fa307411638bb60b651be32f2#gistcomment-3380125>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AO2GMQV37OH5DKU3QYCE4H3R35HANANCNFSM4O2M7PGA>
.
Veja se consegue me encontrar no skype pelo meu e-mail...
erc_m@hotmail.com
Eric, achei sim, vc eh de floripa? q legal ein, ta uma foto com a sua filha
blz, podemos marcar algum horario pra conversar quando vc puder
…On Thu, Jul 16, 2020 at 4:22 PM Eric Marques ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Veja se consegue me encontrar no skype pelo meu e-mail...
***@***.***
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/5bcaa21fa307411638bb60b651be32f2#gistcomment-3380128>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AO2GMQXJ4ON4ZFMNJ4SAMU3R35HOHANCNFSM4O2M7PGA>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment