I'm trying to use Twython to tweet at a specific user when they tweet. I can't seem to figure out how to only stream the users tweets.
I've tried:
stream = MyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.statuses.filter(follow=['25073877'])
The stream returned tweets mentioning the user, possibly as well as the user's own tweets.
I can't find the answer anywhere so if someone could help that'd be great. Thanks!
Related
I'm trying to use Twythonstreamer on a specific Twitter account and I found a thread asking the same thing from years ago, but I still can't seem to get it to work. But maybe I misunderstood the answer.
I currently have:
stream = MyStreamer(consumer_key, consumer_secret, access_token, access_token_secret)
stream.statuses.filter(track='7831092', tweet_mode='extended')
I'm quite new to python and coding in general, and I'm having difficulty understanding how to interact with streamed data from the Twitter API using Tweepy.
Here's my example code which prints out any new tweet that the specified user makes.
import tweepy
auth = tweepy.OAuthHandler("****", "****")
auth.set_access_token("****", "****")
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
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=['user_id_goes_here']))
If I want to do something such as check if a certain word exists inside each tweet as they are made, I do not know how, given it is a constant stream of data.
How does one analyze each tweet as it is delivered and parse it?
The tweepy documentation on streaming is very limited, but it does say
This page aims to help you get started using Twitter streams with Tweepy by offering a first walk through. Some features of Tweepy streaming are not covered here. See streaming.py in the Tweepy source code.
so searching for that file in the tweepy github repository we find
https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py
There you can find the method on_status and see that status should be an instance of the class Status
Looking at the twitter API documentation reveals that
Tweets are also known as “status updates.”
Unfortunately, looking at the source code for Status or the tweepy documentation does not yield much information.
Looking at the twitter API documentation for tweet
https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/tweet
We should expect a field called text that should be the tweet actual text
Another thing we can try is just using a breakpoint and then looking at the variable status using the debugger in order to see what fields it has (this is done a lot of times in python due to it's dynamic nature)
I believe I have found what I am looking for, the analysis of the status needs to happen within the on_status method, for example:
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
if keyword in status.text:
print ("keyword found")
Yesterday I wrote a twitter bot with Python that takes the most recent tweet from Donald Trump, translates it over in Google Translate 45 times, and tweets back at him the final translation in English. Everything works, except the fact that I now need to add some sort of "listener" at the beginning of the code to automatically detect when he tweets so that the rest of the code can do its magic. I've been looking over the internet for some time now and I can't seem to find any sort of event handler that would allow the script to detect when he tweets. So that's why I've come to you guys. Is there any way to use Tweepy or other Python libraries to actively detect when someone tweets? I'll include my code so you guys can see what I want to happen exactly when he does Tweet. It's annotated so hopefully it's not too complicated to understand. Thanks!
import tweepy
from googletrans import Translator
#Keys for accessing the Twitter API
consumer_key = 'PLACEHOLDER'
consumer_secret = 'PLACEHOLDER'
access_token = 'PLACEHOLDER'
access_token_secret = 'PLACEHOLDER'
#Setting up authentification
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
#Scrapes my timeline for DT's latest tweet. I want this to be done AUTOMATICALLY when he tweets.
tweet = api.user_timeline(screen_name = 'realDonaldTrump', count = 1, include_rts = False, tweet_mode = 'extended')
#Translates the text from the .json file that is pulle from the previous line using the Google translate library.
for status in tweet:
translator = Translator()
translation = translator.translate(translation.text, 'mn')
translation = translator.translate(status._json["full_text"], 'ja')
#There are more translations in the actual code, but to reduce the length and complexity, I've taken those out. They don't matter to the specific question.
#Include his handle in a message so that the tweet is tweeted back at him once the translation is complete.
message = ('#realDonaldTrump', translation.text)
#Tweets the message back at him under that specific tweet using it's ID.
send = api.update_status(message, status._json["id"])
I just want the code to be able to scrape my timelines for one of DT's tweets in real time. Thanks!
To Automate your script, you might need to push it into a production server and then create a cron job that will run the script at given regular intervals. To run it locally, I use 1. NGROK - Exposes your localhost addresses and ports to the outside world ie it gives you a way to reach your localhost from the internet, and 2. Invictify - This allows you to run your scripts at a schedule. Also, the script as it is will need a web service to be triggered. Use Flask or FastAPI to create endpoints that call the script when triggered.
I have built a script to read my Gmail inbox for a new email, if found store it for posting and mark the email as "read". Everything has been working fine there.
I have since been trying to fuse Tweepy into this to post said stored email to twitter. Except it looks like tweepy doesn't like my variable "part.get_payload()"
api.update_status("Test")
....works fine and posts to twitter
TweetMsg = part.get_payload()
api.update_status(TweetMsg)
....doesnt post anything to twitter
Looking through Tweepy documentation I couldnt find any info on using variables with Status Updates. Any Advice?
I'm a complete noob when it comes to writing bots, so forgive me for my ignorance. I'd like to write a bot that automatically replies to a specific user. Every time they tweet, I want my bot to reply. I know for a fact that it's possible, I just don't know how to do it. I don't even know where to start. I created my Twitter app, I got the token access token codes, but what do I do next? Any help would be greatly appreciated.
I'm assuming at this point you already authenticated into tweepy.
toReply = "someonesTwitterName" #user to get most recent tweet
api = tweepy.API(auth)
#get the most recent tweet from the user
tweets = api.user_timeline(screen_name = toReply, count=1)
for tweet in tweets:
api.update_status("#" + toReply + " This is what I'm replying with", in_reply_to_status_id = tweet.id)
Now if you want to create a bot you will have to check for new tweets every n seconds, if they have a tweet you haven't replied to yet, you would then reply to that tweet using something like this