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

Toot and Tweet With Python

Automate Mastodon and 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.

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 the server? I can use cron to run a program once a minute. Much of the time it quickly decides that there's nothing to do. But when there is a post for that time, it uploads the data.

I started using Twitter the ToiletGuru identity. It was an experiment to see if Twitter posts could entice page views of my toilet-guru.com site. The initial posts were entirely about that travel-and-plumbing site. Later I started adding references to my travel, Linux, cybersecurity, and networking pages.

Then I created the Conan the Sysadmin identity, which initially only posted tweets about my Linux, cybersecurity, and networking pages before expanding into Hyborian-era-adjacent travel pages.

This approach 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 650 msec of user time and 25 msec of system time. That's with a data file of about 7.8 MB containing over 39,000 scheduled posts.

Both Mastodon and Twitter

I was only interested in posting to Twitter when I first started this project. Then the availability and good reputation of the Twython library to access the Twitter API from Python drove me to use Python.

Besides, at the time I had not yet done anything with Python and it was past time to do that!

Then Elon Musk bought Twitter in late 2022. He soon announced that he would be welcoming all the neo-Nazis and racists and conspiracy theorists and Russian bots back onto the platform. Musk has said multiple times that he believes that we're all living in a computer simulation and nothing matters. People quickly began moving to Mastodon in large numbers. So, I extended the project to also automatically post to Mastodon.

You could follow all of my steps to create your own automated multi-platform social media poster. Or, you could do just the Mastodon steps or just the Twitter steps.

Two Major Parts

There are two major parts to this project. First, I defined an application on each social media server so I can set up the authentication for my automated program.

Second, I wrote a Python program to communicate via HTTPS using the API of the social media service. I found an existing script. It was written in the long-replaced Python 2 and it had a number of problems regarding the current API syntax and semantics, environment variables to handle non-ASCII content, and more. However, it provided useful guidance for writing my new script.

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

To the Linux / Unix Page