Python tweepy: Tweets only after bot starts - python

I want to write bot that searches for tweets after the bot starts. If the tweet has been tweeted before the bot starts working, the bot should ignore it. I have this code right now:
I am using Python and tweepy.
for tweet in tweepy.Cursor(api.mentions_timeline,
since_id=since_id).items():
new_since_id = max(tweet.id, new_since_id)
I don't know how to implement it, I thought about use time function and compare it with the time right now, but I don't know hot to get the time of the tweet.

Twitter's API doesn't allow for searching for tweets by time. You would have to compare the tweet's created_at attribute to the time at which your bot started.

Related

Tweepy, Twitter Rest API user_timeline doesn't return any tweet

I am using tweepy for communicating with the twitter API using python.
I am trying to get a users latest tweet using api.user_timeline(id = user_id, exclude_replies=True, count = 1)[0]
But sometimes this returns 0 tweets and sometimes it works thats why I don't know what could be the problem.
Why does this happen?

Trying to figure out a way to get my Twitter bot to tweet users based on requested dates

I've managed to get my bot to parse it's mentions for the proper method of requesting a date. For example, if a user mentions my bot with "Reminder 09/20/2019" or any other random date, it'll respond with Date Test Successful.
The problem i'm currently having is that I want my bot to only respond to the mention(s) on the date requested. For example, if a user mentions my bot with "Reminder 10/1/2019", I want my bot to respond with "Date Test Succesful" on 10/1/2019.
If my problem isn't explained clear enough, I can go more in depth.
The code i'm currently working with is here:
> # Extended tweet mode is for showing longer tweets in mentions
mentions = api.mentions_timeline(tweet_mode='extended')
for tweets in reversed(mentions):
date_search_match = re.compile(r"(?:reminder|Reminder)\s\d{2}/\d{2}/\d{4}")
if date_search_match.search(tweets.full_text)
api.update_status(f"#{tweets.user.screen_name} Date Test Successful")
You can store the dates in a database and then once a day query for reminders by using a python scheduling package like this one: github.com/dbader/schedule .
For example:
import schedule
import time
def job():
print("I'm searching the DB for today's reminders...")
<logic for querying the DB>
print("and tweeting out the reminders...")
<logic for tweeting the reminders with tweepy>
schedule.every().day.at("10:30").do(job)
while True:
schedule.run_pending()
time.sleep(1)
Or you can run a python script on a cron job.

How to create a twitter bot that replies back as soon as another person tweets?

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.

Replying to a specific user with Tweepy

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

How to check if a given tweet can be retweeted or not?

Currently am working on a retweet function using twitter API's. While the feature works fine, but who those tweets that are not retweetable i get this error, "sharing is not permissable for this status (Share validations failed)".
So i need a way to determine if a particular tweet is retweetable or not using the JSON returned by the statuses/:id API. Is it possible??
Based on this i need to place a retweet button only for those tweets that are eligible for retweet.
In original twitter website, i see the following tweets are not retweetable.
My own tweets
Tweets of my followers who had set it to protected.
Please explain more about this to me.
If someone have protected his tweets, it means his tweets can only be seen by his followers. If you retweet these tweets it may break this protection. So twitter do not let you retweet these tweets
If you want to check whether a user have protected his tweets or not, you can try this API:
http://api.twitter.com/version/users/show.format
The result of this API contains a field name "protected", if "protected" is set to true, you can't retweet his tweets
Document is here: https://dev.twitter.com/docs/api/1/get/users/show

Categories

Resources