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.
Related
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)"])
I have a small script (not created by me) in python to tweet, in this way however I can only tweet text, there is a way to also tweet images, videos, retweets and replies. The script work with tweepy
Here is the script:
import tweepy
auth = tweepy.OAuthHandler("API KEY", "API SECRET")
auth.set_access_token("ACCESS TOKEN", "ACCESS TOKEN SECRET")
api = tweepy.API(auth)
tweet = input("What Would You Like To Tweet? ")
api.update_status(status =(tweet))
print ("Done!")
In order to tweet images, videos, retweets etc. indeed you need other functions of the api to call (otherwise how it could differ between different commands?)
You need the checkout the tweepy documentation to get comprehensive answers but:
Tweet with Media:
filename = "image_to_be_sent.png"
status = "Hello World!"
api.update_with_media(filename = filename, status = status)
Retweet:
api.retweet(id) # you should now the tweet id you want to retweet
Reply:
In order to reply you can use either api.update_status() or api.update_with_media() depending on if, you want to attach an image or video or not. You should only set the optional argument in_reply_to_status_id.
e.g:
filename = "image_to_be_sent.png"
status = "Hello World!"
api.update_with_media(filename = filename, status = status, in_reply_to_status_id = in_reply_to_status_id)
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.