Micro-Star International motherboard with AMD Phenom II 4-core processor.

Schedule Tweets With Python

Automate Twitter Posting with Python

I wanted a way to schedule Twitter postings with a very simple interface. Ideally, a text file with scheduled times and content. TweetDeck is a great dashboard interface for many tasks on Twitter, but it didn't provide what I want. You can schedule tweets with TweetDeck, but the interface requires several steps of point-and-click. Plus it requires a fairly tall window. On my laptop, I must scroll up and then back down within the process of scheduling just one tweet. Then, if you have clicked in the wrong place (all too easy), you can't see that until you have reloaded the TweetDeck dashboard page and scrolled over to your column of scheduled tweets.

TweetDeck has the great feature of posting scheduled content whether you're logged in or not, but the interface is too clumsy for me to put up with the hassle of scheduling more than a small number at a time.

There are some programs to interact with Twitter, but these solutions can only post right then and so they need to run on an always-on computer. I haven't found any programmed method to schedule future postings as you have with TweetDeck.

Then it occurred to me: my web server is always up. Why not use the cron daemon to schedule a program that posts the messages from my server? The crontab file has its own quirky syntax. So, I will have a program that runs once a minute and most of the time quickly decides that there's nothing to do.

It imposes no significant load on the server, which isn't very busy to start with. The resulting Python program runs once per minute, taking just about 320 msec of user time and 80 msec of system time.

Two Major Parts

There are two major parts to this project. First, I must define a Twitter app so I can set up the authentication for my automated program.

Second, I wrote a Python program based on an existing script written by someone else. I first updated it from Python 2 to 3. That required some adjustments in environment variables, in order to handle non-ASCII content.

Along the way I changed a lot of the program. I replaced some deprecated Twitter-specific code, and I modified the overall logic. Now it supports multiple Twitter identities.

After testing, I scheduled it to run automatically. The cron runs the program once a minute. It reads its configuration files containing tweets scheduled for the multiple identities. Then it posts those updates, along with optional images.

To the Linux / Unix Page