I am pulling tweets from twitter's streaming api using tweepy. I'm then using this to autoreply to that user.
For example if I want to pull live tweets from and then reply to Donald Trump I use:
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
import json
class StdOutListener(StreamListener):
def on_data(self, data):
clean_data = json.loads(data)
tweetId = clean_data["id"]
tweet = "YOUR MESSAGE HERE"
respondToTweet(tweet, tweetId)
def setUpAuth():
auth = tweepy.OAuthHandler("consumer_token", "consumer_secret")
auth.set_access_token("access_token", "Access_token_secret")
api = tweepy.API(auth)
return api, auth
def followStream():
api, auth = setUpAuth()
listener = StdOutListener()
stream = Stream(auth, listener)
stream.filter(follow=["25073877"], is_async=True)
def respondToTweet(tweet, tweetId):
api, auth = setUpAuth()
api.update_status(tweet, in_reply_to_status_id=tweetId, auto_populate_reply_metadata=True)
if __name__ == "__main__":
followStream()
If you run my above code, you'll notice it does reply to Donald Trump, but also replies to all new replies to his tweets
What do I need to add to exclude replies to his tweets from the stream?
Thanks in advance for any help.
follow
id.tweetId
only when the followed account replies.in
:
if user_id in [25073877, id2, id3,...]:
class StdOutListener(StreamListener):
def on_data(self, data):
clean_data = json.loads(data)
tweetId = clean_data["id"]
user_id = clean_data['user']['id']
tweet = 'Trying to figure out this code to answer a question on SO, just ignore this'
if user_id == 25073877:
print('Tweeting a reply now') # optional
respondToTweet(tweet, tweetId)