9

I started exploring tweepy a couple days ago and was able to stream filtered (with keywords) tweets at real time. Now I want to stream not only filtered tweets but also tweets from several specific twitter users. Is this possible by using tweepy? It seems stream.userstream() only fetches real-time tweets from my twitter account NOT from other specific users, right? I have tried it with another twitter account that I created for testing but it does not fetch any new tweets that I tweeted, at all.

But if it works, can I download tweets using stream.userstream() and stream.filter() at the same time? If not then how can I get both filtered tweets and users' real time tweets?

Btw I used the example code from @alexhanna.

api      = tweepy.API(auth)

def main( mode = 1 ):
follow = []
track  = ['Houston Rockets','Lakers','Chicago Bulls']

listen = SListener(api, 'test')
stream = tweepy.Stream(auth, listen)

try: 
    stream.userstream('NBA','ESPN')
    stream.filter(track = track, follow = follow)

except:
    print "error!"
    stream.disconnect()

Would really appreciate your help! Thanks.

Blue482
  • 2,926
  • 5
  • 29
  • 40

3 Answers3

5

Try using the .filter(follow="") without .userstream() beforehand. Userstream is only the tweets from the account associated with your application. Here is a (very well-annotated) example.

If you want to get the user's tweets and filtered tweets at the same time you need to make two separate Stream() objects.

Edit: the page I linked to is now dead. The Internet Archive link should remain active indefinitely, but all the relevant information to solving the user's question is already contained within this answer. I have not copied and pasted the example from the linked page as I am not the author of it, and because it is only an example that illustrates the proper use of a Stream listener.

Luigi
  • 4,129
  • 6
  • 37
  • 57
  • 1
    Thanks Louis! filter(follow) does work for streaming real-time tweets from specific users. And i have tested it seems I am able to get users' tweets and filtered tweets at the same time with only one Stream() object. I have created another twitter test account for 'user's tweets' and using my 'OAuth-ed' account for filtered tweets, and seems to getting both tweets in a single json file. Is there any issues with it? Would it be better to make two separate Stream() objects as you suggested? – Blue482 Mar 27 '14 at 22:23
  • I suggested that as an alternative, as long as collecting both in the same file works for you. If you want to separate the two it'd probably be easier conceptually to collect as two separate `Stream()` objects but you could easily filter the output from one `Stream()` object yourself. – Luigi Mar 27 '14 at 23:14
  • can you please update the link given in answer? It doesn't work. oreilly shows support home page – mrtipale Apr 07 '17 at 07:33
  • @mrtipale Editing now, link has broken so I'm adding a link to the internet archived page and copying the relevant code – Luigi Apr 07 '17 at 14:57
  • Note that the Archive seems to be undergoing maintenance, things are running kind of slowly this morning – Luigi Apr 07 '17 at 15:04
  • Thanks @Luigi! Appreciate your time to fix the link :) – mrtipale Apr 10 '17 at 10:30
  • ---Edit: I figured it out. I need to use the user_id, not the screenname--- I can't get .`.filter(follow="")` to work. When I use `.filter(track=["test"])`, for instance, I get a constant stream of tweets with the text "test" (as expected); however, if I change that to `.filter(follow=["test"])`, I would expect to get a constant stream of tweets from user `@test`, but instead the program exits with no errors. – Greg Feb 09 '18 at 00:39
  • How can I search for tweets with a specific text from a specific user? I put both parameters in the filter method but I get ALL tweets from twitter with the text. I cant combine both. – Alexandre Allegro Feb 16 '21 at 09:45
2

If you want to stream specific user time-line:

1) Find his/her ID with this website or google for How to find twitter ID.

2) Use following code:

    from tweepy import OAuthHandler
    from tweepy import Stream
    from tweepy import StreamListener

    listener = StreamListener()
    auth = OAuthHandler(config.API_KEY, config.API_SECRET)
    auth.set_access_token(config.ACCESS_TOKEN, config.ACCESS_TOKEN_SECRET)
    stream = Stream(auth, listener)
    stream.filter(follow=['3511430425']) # user ID for random account
Amir
  • 16,067
  • 10
  • 80
  • 119
2

You can fetch a particular users tweets in real time as per the below code sample.

import tweepy
from tweepy import OAuthHandler, Stream, StreamListener

username="maheshmnj" # Add your target users username here
userId=12345678 # Add target users userId 

class TweetListener(StreamListener):
    """ A listener handles tweets that are received in real time.  """

    def on_data(self, status):
        try:
            data = json.loads(status)
            user = data['user']['screen_name']
            tweet_id = data['id']
            if(user == f'{username}'):
                print(f'user tweeted, his tweetId=@{tweet_id} do something with the Tweet'); 
            else:
                print('found user',user)
        except Exception as e:
            print(e)
            print('failed to retweet')
        return True

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

if __name__ == '__main__':
    print(f'bot started listening to {username}')
    listen = TweetListener()
    auth = tweepy.OAuthHandler(APP_KEY, APP_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    api = tweepy.API(auth)
    stream = Stream(auth, listen)
    stream.filter(follow=[userId],)
Mahesh Jamdade
  • 17,235
  • 8
  • 110
  • 131