How to make an autoretweet bot on Python?
My small bot for my auto retweet on twitter which is based on python and is hosted on my small raspberry PI:-
-
- Install PIP
- pip install twython
- Create an application on twitter via https://apps.twitter.com/ in order to get the following variables
- APP_KEY
APP_SECRET
OAUTH_TOKEN
OAUTH_TOKEN_SECRET
- APP_KEY
- The small script (my link on GITHUB soon – sorry for indentation) and save as tweet.py
import time from twython import TwythonStreamer, Twython , TwythonError #Twitter app auth APP_KEY = '' APP_SECRET = '' OAUTH_TOKEN = '' OAUTH_TOKEN_SECRET = '' #Let's gather a list of words we DON'T want to RT naughty_words = ["CPU", "TEMP", "courses", "hiring", "load average", "Temperature and humidity sensor", "Comunidade"] #And a list of words we WOULD like to RT good_words = ["#linux", "twitter" ] #OR is Twitter's search operator to search for this OR that #So let's join everything in good_words with an OR! filter = " OR ".join(good_words) # The - is Twitter's search operator for negative keywords # So we want to prefix every negative keyword with a - blacklist = " -".join(naughty_words) #And finally our list of keywords that we want to search for #This will search for any words in good_words minus any naughty_words keywords = filter #+ blacklist #Setting Twitter's search results as a variable while (True): try: twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) search_results = twitter.search(q=keywords, count=1) #time.sleep(600) for tweet in search_results["statuses"]: #print tweet try: twitter.retweet(id = tweet["id_str"]) except TwythonError as e: print e time.sleep(1000) except TwythonError as e: print e
- Run script, python tweet.py & (for linux shell – to run the application in background.
- A small “monitoring” script to check the status of python tweet.py, if the latter is crashed it will start it automatically
check_tweet.sh
#!/bin/bash #Check tweets ps ax |grep -v grep |grep tweet.py result=$? if [ "${result}" -eq 0 ] then echo $(date) "Tweet Bot is running" else su - root -c "/usr/bin/python <pathTO>tweet.py &" fi
create a cron job and the script executable:-
chmod +x /<PatchToscripts>/check_tweet.sh
*/10 * * * sudo /<PatchToscripts>/check_tweet.sh