Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Be notified when a user tweets, using Tweepy

Tags:

python

tweepy

I am playing around with a twitter bot using tweepy.

I have got the code to successfully filter, based on words contained in the tweets (e.g. my trigger phrase), doing something like:

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=MyStreamListener())
myStream.filter(track=['my trigger phrase'])

This works perfectly.

But I want my code to filter when a certain user tweets (e.g. 'someuser').

I had tried:

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=MyStreamListener())
myStream.filter(follow=['someuser'])

But when I run the code, after a second or two, it stops with no output.

Any help would be great.

In summary, I want to be able to do something whenever a certain user tweets.


The entire code:

import tweepy
import time
import sys
import inspect

consumer_key = 'xxxxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxx'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
auth.secure = True

api = tweepy.API(auth)

class MyStreamListener(tweepy.StreamListener):
    def on_status(self, status):
            if  status.user.screen_name.encode('UTF-8').lower() == 'someuser':
                print 'TWEET:', status.text.encode('UTF-8')
                print 'FOLLOWERS:', status.user.followers_count
                print time.ctime()
                print '\n'

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=MyStreamListener())
myStream.filter(follow=['someuser'])
like image 700
user1551817 Avatar asked Aug 12 '18 21:08

user1551817


People also ask

How do I get my tweets from Tweepy?

Steps to obtain keys: – For access token, click ” Create my access token”. The page will refresh and generate access token. Tweepy is one of the library that should be installed using pip. Now in order to authorize our app to access Twitter on our behalf, we need to use the OAuth Interface.

Does Tweepy work with v2?

For the v2 API, Tweepy provides the Client interface. This is available from Tweepy v4.

What is Wait_on_rate_limit Tweepy?

wait_on_rate_limit – Whether or not to automatically wait for rate limits to replenish. wait_on_rate_limit_notify – Whether or not to print a notification when Tweepy is waiting for rate limits to replenish. proxy – The full url to an HTTPS proxy to use for connecting to Twitter.

How does Python fetch data from Twitter?

To be able to interact with the Twitter API using Python, we'll use a library called Tweepy. To be able to hide the credentials from the source code and load them as environment variables, we'll use python-dotenv. First, create a . env file to hold your credentials.


2 Answers

I use this for retrive the Id_user for follow in Stream

auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = API(auth) 
user = api.get_user(screen_name = '@screenametofollow')
print ("User id:" + str(user.id))
....
stream.filter(follow=str(user.id))
like image 50
jacklondon Avatar answered Oct 10 '22 21:10

jacklondon


The problem was just that I was trying to enter:

follow=['someuser']

But I needed to be using the user id number and not just the screen name.

You can find the user id number for any screen name on various web sites.

like image 45
user1551817 Avatar answered Oct 10 '22 21:10

user1551817