So I am trying to create a function that basically is able to use the stream.listener function in tweepy to send a message when a user follows a new person. So basically the function would be in pseudocode of course
if user follow new:
print(¨User has followed{user}¨)
This is the code I have right now I am able to stream new tweets that come out of a user using the documentation but I couldn't find anything to stream new followers and such. Any help would be appreciated!!! Thanks!
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
myStream.filter(follow=["(Id private)"])
Related
I am currently trying to stream live tweets to my console of a specific user using the tweepy library (twitter api v2). I have found documentation for key words or hashtags but not to filter by the user who tweeted. Anyone know how I can add a rule to filter all tweets by say #elonmusk?
client = tweepy.Client(keys.BEARER_TOKEN, keys.API_KEY, keys.API_KEY_SECRET, keys.ACCESS_TOKEN, keys.ACCESS_TOKEN_SECRET)
auth = tweepy.OAuth1UserHandler(keys.API_KEY,keys.API_KEY_SECRET, keys.ACCESS_TOKEN, keys.ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
class MyStream(tweepy.StreamingClient):
def on_tweet(self, tweet):
print(tweet.text)
time.sleep(0.2)
stream = MyStream(bearer_token=keys.BEARER_TOKEN)
stream.add_rules({'from: elonmusk'})
print(stream.get_rules())
stream.filter()
class TweetListener(tweepy.StreamingClient):
def on_tweet(self, tweet):
print(tweet.text)
stream = TweetListener(bearer_token=tw.bearer_token)
stream.add_rules(tweepy.StreamRule("from:username01 OR from:username02"))
stream.filter()
ANSWER: I used the twarc CLT to solve this issue.
Steps taken to add a rule to specify tweets by a user:
pip3 install twarc
twarc configure
twarc2 stream-rules add "from:____" <--- put username there
run program with code I have above just delete
stream.add_rules({'from: elonmusk'})
print(stream.get_rules())
I can't get the full text of statuses.
I tried "status.full_text" and some similar things, but status has no such attributes.
On the contrary,
tweets = api.search(q='athens', tweet_mode='extended')
for tweet in tweets:
print(tweet.full_text)
this worked just fine. It seems like tweet has "full_text" attribute but status not.
So what do I have to change in the code below to get the full text of the status?
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
my_stream_listener = MyStreamListener()
my_stream = tweepy.Stream(auth=api.auth, listener=myStreamListener, tweet_mode='extended')
my_stream.filter(track=['Athens'])
Try status.extended_tweet["full_text"], as discussed here.
I am using recommended code from tweepy.
Documentation says to use:
#override tweepy.StreamListener to add logic to on_status
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
myStream.filter(track=['OWASP'])
I get an error stating that "myStreamListner" cannot be found, which is on the line of the tweepy.Stream(auth = api.auth, listener=myStreamListener)
I'm not sure why, because I have the "import tweepy" being used.
Is there any additional code I need such as an extra import
or do I need to add some more authorization code?
I have the tokens and the keys all 4 of them added.
Any advice would be nice, because the only thing I am trying to do is search for OWASP using tweepy.
I have the myStreamListener = MyStreamListener() which defines the myStreamListener which I would think is correct from tweepy's documentation.
I think you want to remove all indentation from those last 3 lines. They look like they should be executed at the base level.
I'm trying to create a listener to a very specific twitter account (mine), so I can do some automation, if I tweet something with a "special" code at the end (could be a character like "…") it will trigger an action, like adding the previous characters to a database.
So, I used Tweepy and I'm able to create the listener, filter keywords and so, but it will filter keywords from all the Tweetverse. This is my code:
import tweepy
cfg = {
"consumer_key" : "...",
"consumer_secret" : "...",
"access_token" : "...",
"access_token_secret" : "..."
}
auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret'])
auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
api = tweepy.API(auth)
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
return True
def on_error(self, status):
print('error ',status)
return False
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth=auth, listener=myStreamListener)
myStream.filter(track=['…'])
It will filter all the messages containing a "…" no matter who wrote it, so I added to the last line the parameter follow='' like:
myStream.filter(follow='myTwitterName', track=['…'])
It always gives me a 406 error, if I use myStream.userstream('myTwitterName') it will give me, not just the Tweets I write, but also my whole timeline.
So, what am I doing wrong?
EDIT
I just find my first error. I was using user's screen name, not Twitter ID. Now I got rid of the 406 error, but still doesn't work. I placed the Twitter ID in the follow parameter, but does absolutely nothing. I tried both, with my account and with an account that is too "live", like CNN (ID = 759251), I see new tweets coming in my browser, but nothing on the listener.
If you're interested on knowing your own Twitter ID, I used this service: http://gettwitterid.com/
OK, solved. It was working from the very beggining, I made two mistakes:
To solve the 406 error all it has to be done, is to use Twitter id instead of Twitter name.
The listener was apparently doing nothing, because I was sending "big" tweets, that is, tweets longer than 140 chars. In this case, you shouldn't use status.text, but status.extended_tweet['full_text']
You must check for the existance of the extended_tweet, if it is not in the status received, then you should use the text
I have a twitter bot that responds to tweets containing a hashtag. I've been using the twt = api.search(q='param') to pull tweets and it's been working perfectly but I've since switched to myStreamListener = MyStreamListener()
twt = tweepy.Stream(auth = api.auth, listener=myStreamListener()) to pull tweets in real time. this isn't working though and I don't know why. Here's my code:
myStreamListener = MyStreamListener()
twt = tweepy.Stream(auth = api.auth, listener=myStreamListener())
twt.filter(track=['#www'], async=True)
#list of specific strings we want to omit from responses
badWords = ['xxx','yyy' ]
#list of specific strings I want to check for in tweets and reply to
genericparam = ['#zzz']
def does_contain_words(tweet, wordsToCheck):
for word in wordsToCheck:
if word in tweet:
return True
return False
for currentTweet in twt:
#if the tweet contains a good word and doesn't contain a bad word
if does_contain_words(currentTweet.text, genericparam) and not does_contain_words(currentTweet.text, badWords):
#reply to tweet
screen = currentTweet.user.screen_name
message = "#%s this is a reply" % (screen)
currentTweet = api.update_status(message, currentTweet.id)
I believe your problem is you're trying to send the reply through the stream instead of using an http request in a separate thread. It's not possible to do this as explained here:
https://dev.twitter.com/streaming/overview
Just stumbled across this.
When you create myStreamListener = MyStreamListener(self.api) self.api instance you want to use needs to be passed to StreamListner constructor or it will be null.