Пример #1
0
import conexao
import tweepy
from time import sleep

api = conexao.get_api(stream=True)


# Sobre escrevendo tweepy.StreamListener
class MyStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        #print(status.user.screen_name)
        set_database(status)

    def on_error(self, status_code):
        if status_code == 420:
            #returning False in on_error disconnects the stream
            return False


def set_database(tweet):
    # cria conexão com o banco de dados
    con = conexao.get_mysql()
    cursor = con.cursor()

    try:
        cursor.execute('INSERT INTO user_aleatorio (nome)\
			VALUES(%s)', (tweet.user.screen_name))
        print("Adicionado.....")
    except:
        # essa exceção acontece caso o tweet já exista na base de dados
        print('NÂO ADICIONADO*********')
Пример #2
0
import tweepy
import conexao
from time import sleep

api = conexao.get_api()
con = conexao.get_mysql()
cursor = con.cursor()

username = '******'

count = 100
try:
    # Creation of query method using parameters
    tweets = tweepy.Cursor(api.user_timeline, id=username).items(count)
    tweets = [[tweet.user.screen_name, tweet.text, tweet.created_at, tweet.id]
              for tweet in tweets]

    for tweet in tweets:
        try:
            cursor.execute(
                'INSERT INTO tweet_paginas (nome, tweet, data, id_tweet) VALUES (%s, %s, %s, %s)',
                (tweet[0], tweet[1], tweet[2], tweet[3]))
            print('Adicionado')
        except:
            # essa exceção acontece caso o usuário já exista na base de dados
            print('Não adicionado')
            continue
        con.commit()

except tweepy.error.RateLimitError:
    print('RateLimitError aguardando...')