skip to Main Content

Hello I am fairly new with Python and I am trying to write code that will detect when a Twitter account posts with Tweepy, save the text of the Tweet as a variable, then post the Tweet to a subreddit with PRAW. I found a code online that will post to a specified subreddit and it does work so I just need to get the stream of incoming data from Tweepy. I tried to set up a stream.filter.follow(user_id) and I get no error message, but it doesn’t seem to be connected to the Twitter account I made to test it. When I Tweet I don’t see them coming in on my code although I thought I had it set up to print the raw_data.full_text for the Tweets. Here is my code can anyone see if there is something wrong with it. I have all the access codes and from my developer account, and I got my screen_id from a website where I put my @ in to get a number id. I have removed this sensitive info from the code.

import time
import tweepy
import praw

#Variables that contains the credentials to access Twitter API and REDDIT
USERNAME = 
PASSWORD = 
CLIENT_ID = 
CLIENT_SECRET = 
consumer_key = 
consumer_secret = 
access_token = 
access_secret =

#Creating stream listener
class Listener(tweepy.Stream):
    
    def on_data(self, raw_data):
        self.proccess_data(raw_data)

        return True
    
    def process_data(self, raw_data):
        print("ID: {}".format(raw_data.id))
        print(raw_data.full_text)


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

#Twitter authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

api = tweepy.API(auth)
        
#Creating Stream
stream_tweet_listener = Listener(consumer_key, consumer_secret, access_token, access_secret)
stream_tweet = tweepy.Stream(consumer_key, consumer_secret, access_token, access_secret)

#auth = api.auth, listener = stream_tweet_listener

#stream tweets
screen_id = 
stream_tweet.filter(follow = screen_id)

2

Answers


  1. You’re using an instance of tweepy.Stream, which by default just logs the data received, rather than your subclass of it.

    Login or Signup to reply.
  2.     PersonId = client.get_user(username="username").data.id
        class MyStream(tweepy.StreamingClient):
    
            def on_connect(self):
                print("Connected")
    
            def on_tweet(self, tweet):
    
                if tweet.author_id == PersonId : 
                    print(tweet.text)
    
        stream = MyStream(bearer_token=bearer_token)
        #Pay attention to bearer_token recently you must tou use it
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search