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

Automate Posts With Python

Automate Social Media Posting with Python

I wanted a way to schedule social media 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.

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 the 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 with 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.

The Origins of Python

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 destinations such as Korinthia, Khitai, Turan, Stygia, and so on.

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. 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!


Amazon
ASIN: 1492051365

Amazon
ASIN: 1449355730
Moving from Twitter to Mastodon according to:
pcmag.com
zapier.com
lifehacker.com
redhat.com

Then Elon Musk bought Twitter in late 2022. He soon welcomed 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, so nothing matters to him. 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 programming libraries to communicate with the social media services. 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 suggestions for developing 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.

That's the overview. Let's get started.

To the Linux / Unix Page