Python Programming - Creating A Twitter Retweet Bot w/ Tweepy
Vložit
- čas přidán 23. 07. 2024
- Welcome to Part 12 of the Python Programming tutorial series here on Tutorial Spot.
In Part 12 I took inspiration from an article I saw online showing how a programmer had managed to win quite a few competitions using a Python script to automatically enter him into competitions on Twitter.
I took the premise that we access the API from Twitter and simply search for tweets containing a certain keyword and then retweet the tweets found to contain that very keyword, or keywords!
You will need to create a Twitter App and install Tweepy before using the code.
Tweepy: github.com/tweepy/tweepy
Twitter App Page: apps.twitter.com
Code uploaded to PasteBin: pastebin.com/4yYJdVrn
Python 3.6.4 was used to make this tutorial.
Twitter: @TutorialSpotUK
This is a very good tutorial, the only problem is that my bot retweets recent tweets that don´t meet the requirements(5 retweets) and it also tweets bots from days ago rather than current tweets, should I import time?
Nice tutorial!
Got a question: I've heard that the search functionality is being updated, the current method being deprecated in the next few months. Have you heard about that? Will we be able to use Tweepy in the future to search Twitter and Twitter users?
How is it being updated exactly? If they're added more functionality, then I imagine the core search feature through the API will stay pretty much the same, maybe.
Hi there thanks for tutorial if i wanted to like the status with out retweeting it what do i need to do
never mind i got it , tweet.favorite()
Great script - but how do you get it to loop? So for example it goes away and does 5 tweets, but while it runs what if another 5 tweets are done with the same search term? without re running the script it never finds them.
How do you get around this?
TIA
Can you search for hashtags ?
nice tutorial u got like & subs but how I can let the bot retweet mine tweet ?
Is that possible to make the bot run forever? or maybe in loop
Instead of retweeting can I just replace tweet.retweet() with tweet.favorite()?
what do I do if I get the error " tweepy not defined "
You need to install tweepy by the sounds of it. It's a bit of a pain to install, I'd recommend searching around online to find a tutorial. I tried many
have you installed it?
thanks. how many tweets is it possible to retweet in an hour without facing an error?
it should always be below 40 ... read twitter rules
it keeps saying “StopIteration” is an invalid syntax? what did i do wrong?
never mind i figured it out
Can I retweet tweets from a twitter list?
If you set the values right then yes
Query : how to run this code every 15min and how can yo search for multiple key words ... I also hv a Raspberry pi ... thank you
I would like to know this as well
why u just dont create a python code that create a subprocess that start with system that open the twitter bot.py every 15min?
multiple criteria is just standard boolean e.g. python OR coding
to run every 60 seconds, just add a while loop at the end of the program, assuming you still have function named main...
while True:
main()
time.sleep(60)
Finally I Found Perfect Man
Instructions unclear. How do I open that note pad thing at 3:13 where do i go for that
Ctrl+n
It's called 'Python Shell' or 'IDLE', then open a new file
Mine is not retweeting things but it does show the username.
Can you help me?
Sorry for the late reply, did you ever get this fixed?
I am having the same problem. It shows my name but nothing more. I tried doing a control+X on "user = api.me()
print(user.name)" just to see what it would do without that command and it did nothing. If I find the issue I will let you know.
So I looked into my own issue (the same issue Dominic Campos was having) and for me, I can't put in more than one search term. If I do, only my name shows up then it kicks me out. (I even tried just 2 terms and it didn't work) If I only put in one, it works and will retweet. So I am not sure if I am missing something? Here is my code: (with all search terms)
import tweepy
access_token = "You ain't getting my code!l"
access_token_secret = "You ain't getting my code!"
consumer_key = "You ain't getting my code!"
consumer_secret = "You ain't getting my code!"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
user = api.me()
print(user.name)
def main():
search = ("sciencediscoveries","internetdiscoveries","3dprinting","art",
"computerscience","flyingcars","innovation","newtechnology",
"supercomputers","inventions","technology","tech","science",
"batteries","solar","ev","design","architecture")
numberofTweets = 18
for tweet in tweepy.Cursor(api.search, search).items(numberofTweets):
try:
tweet.retweet()
print("Tweet Retweeted")
except tweepy.TweepError as e:
print(e.reason)
except StopIterartion:
break
main()
Looking into this more, it seems as though some search terms work and some don't. Like "internetdiscoveries" wont return anything but "3dprinter","science" together will.. Does this look for posts that have all the tags you mentioned or will it look for each on individually? If it does so alltogether do you suggestion I copy and past the def main () and everything below it to repeat another term? Message me in your spare time if you can.
@@masterjtricksters same issue
i hope you changed those key's, you shouldn't publicly post them
Yeah, blurring them out in editing was difficult and looked ugly so I left them visible and regenerated my keys.