Tweeting Automatically, POC (Python and Pi)

Following on from my various Twitter auto reply blog posts and my purchase of a Raspberry Pi Zero W, it was time to all proof of concept (PoC) and put my Python program into a live environment.

Up first was transferring my Python program (with associated files) from my MacBook to the PiZero W. As my PiZero W is set up with a monitor/keyboard or mouse I wanted to do everything remotely from my MacBook.

ssh via bash

An ssh session is easy enough to create via bash/terminal on the Mac, with ssh username@IPAddress opening the session (and then requiring the password).

To move files I used the scp command which is in the following format:

scp path_to_files username@IPAddress:Path_to_copy_files_to

With the files copied I used an ssh session to make sure that the Raspbian OS was up to date (apt-get update, apt-get upgrade) and then installed the Twython Python library so that Python 3 on the Pi Zero W could use it (and run my program).

Next up, running the program. My whole idea with my Python auto-tweet program was for the program to run automatically – i.e. no need for a human to click run. I originally looked at automating it in Python using time delays and loops before thinking that I was a) making the program more complicated than it needs to be, b) that there must be an easy way to auto run the program automatically and c) if all the automation is in the program and it hits an error then it will crash out and need manually restarting.

On a Windows based OS this could be accomplished by a scheduled job, in steps Cron. Cron allows for programs to be run at scheduled times in a Unix or Linux environment. On Raspbian a cron job can be scheduled by typing crontab -e into bash.

cron via nano

cron opens in a text editor (in my case Nano) and then it just needs a line adding at the bottom of the comments (#) to say what is needed.

*/59**** /home/pi/

This line simply tells cron to run my Python program every 59 minutes (I didn’t want to do it every hour as I like a little variance). The five stars (*) are very important in Cron:

*                  *                 *                 *                 *

M                H                DOM         Mo              DoW


M = minute. Enter 0 to 59 and Cron will run it at that minute past the hour. Enter /5 for every five minutes, /10 for every ten minutes or /59 for every 59 minutes.

H = hour. Enter 0 to 23 and Cron will run it at that hour. Enter /2 for every 2 hours etc…etc…

DOM = Day of the Month. Enter 1 to 31 and Cron will run it on that date.

Mo = month. Enter 1 to 12 and Cron will run it that month.

DoW – Day of the week. Enter 0 to 6, with 0 being Sunday.

Any value left as a * will mean all variables are excepted.

With my cron job set up it was time to see if it worked and an issue became apparent straight away, not only was my program auto replying to tweets, it was also tweeting to replies and retweets.

Adding some refinement to my search criteria resolved that:

search_term = “geektechstuff -filter:retweets AND -filter:replies”


Originally I had set Cron to run the program every 5 minutes, which was a mistake as it soon hit Twitters API rate limit. Other than that, the program seems to be working OK.