<jupyter_start><jupyter_text>Preparing a connection to Twitter<jupyter_code># Install the tweepy package
# Using pip
#!pip install tweepy
# Using conda
#conda install -c conda-forge tweepy
# Imports
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import tweepy
from datetime import datetime
import json
import pandas as pd
import numpy as np
# Consumer Key
consumer_key = "xxxx"
# Consumer Secret 
consumer_secret = "xxxx"
# Access Token
access_token = "xxxx"
# Access Token Secret
access_token_secret = "xxxx"
# Creating the authentication keys
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
class MyListener(StreamListener):
    
    def __init__(self):
        super().__init__()
        self.counter = 0
        self.limit = 200
        self.tweet_list = []
    
    def on_data(self, dados):
        tweet = json.loads(dados)
        if tweet['text'].startswith('RT @'):
            return True
        else:
            
            created_at = tweet["created_at"]
            id_str = tweet["id_str"]
            if tweet["truncated"]:
                text = tweet['extended_tweet']['full_text']
            else:
                text = tweet["text"]
            obj = {"created_at":created_at,"id_str":id_str,"text":text,}
            self.tweet_list.append(obj)
            print(obj)

            self.counter += 1
            if self.counter <= self.limit:
                return True
            else:
                data = pd.DataFrame(self.tweet_list)
                data.to_csv('bolsonaro_tweets.csv', encoding='UTF-8', index=False)
                mystream.disconnect()



mylistener = MyListener()


mystream = Stream(auth, listener = mylistener)
mylistener = MyListener()
mystream = Stream(auth, listener = mylistener, tweet_mode='extended')
keywords = ['Bolsonaro']
mystream.filter(track=keywords)<jupyter_output>{'created_at': 'Sat May 23 23:51:22 +0000 2020', 'id_str': '1264343194214502405', 'text': 'Klaus não decepciona'}
{'created_at': 'Sat May 23 23:51:22 +0000 2020', 'id_str': '1264343195036590082', 'text': 'eu gostava mais dos pré-adolescentes quando eles ficavam o dia inteiro na internet idolatrando o bolsonaro'}
{'created_at': 'Sat May 23 23:51:22 +0000 2020', 'id_str': '1264343195103657985', 'text': 'Pronunciamento do senhor presidente da república, <NAME>. https://t.co/haDDu8V4mb'}
{'created_at': 'Sat May 23 23:51:22 +0000 2020', 'id_str': '1264343196177502209', 'text': 'vtnc besteira do caralho so pq 17 eh o numero do bolsonaro'}
{'created_at': 'Sat May 23 23:51:23 +0000 2020', 'id_str': '1264343196437557250', 'text': '#VaiTrabalharVagabundo \nFora Bolsonaro e Mourão.'}
{'created_at': 'Sat May 23 23:51:23 +0000 2020', 'id_str': '1264343198119464962', 'text': 'Senhoras e senhores, o humorista que diz comer até um bebê está escandalizado com palavrões.'}
{'created_at': 'Sat Ma[...]
{'fraction_non_alphanumeric': 0.11842551747539871, 'fraction_numerical': 0.07465218866644045, 'mean_word_length': 3.521472392638037, 'pattern_counts': {'":': 3, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '528268', 'license': 'Apache-2.0', 'n_tokens_mistral': 1156, 'n_tokens_neox': 955, 'n_words': 262, 'path': 'Stream_Twitter_Pandas.ipynb', 'repo_name': 'marcosppaiva/PythonTwtitte', 'size': '2955'}