A Twitter bot for translating and posting an account's tweets into the Martian language of Mars Attacks.
Now Donald Trump has been suspended indefinitely from Twitter, I figured it would be worth recording the code behind @MartianDTrump for posterity.
Way back in 2012 @somegreyboke tweeted asking if it was possible to make a Twitter bot which translated Donald Trump's tweets into Martian. Being bored and drunk at the time, a justification I've used many times in the years before and since, I bashed something together in Perl that evening and pushed it live. It was a batch process which ran every 5 minutes and had its fair share of bugs, but it got the job done and raised a few laughs.
In 2018 I decided to finally teach myself Python while laid up from surgery. This was the first thing I upgraded as part of that teaching. It's messy, and downright hacky in places, but it works well enough. I only really fixed it when something broke; at one point it reciprocated followers (Twitter didn't approve), and mirrored retweets (which I disabled after a run of especially problematic ones).
A few people asked how it worked over the years, so maybe this will be useful to someone else out there for whatever reason.
It's intended for Python 3 on Linux. Python imports are detailed in Requirements.txt, and should play nicely with pip. You could probably change the filepaths in the config.ini file to get it working on another OS, but I haven't tested it and don't intend to.
The config.ini file needs a consumer key, consumer secret, access key, and access secret as generated by the Twitter Developer resources for an account doing the tweeting. Trump blocked the bot's account about 6 months in (which was hilarious in itself) so it also accepts credentials for a second account to listen for events in the event of a blockage. You'd think Twitter wouldn't approve of that either, but it passed their internal code review process so your guess is as good as mine. Don't be mean with it.
If you run it without any arguments, it runs as a continuous process to translate in near-realtime, using a pid file to prevent multiple simultaneous processes conflicting.
If you run it with any arguments, it just translates them and spits them back out to STDOUT to help with testing.
The config.ini file also contains manual translations which are used first before any automatic attempts. I compiled these from about a year's worth of Trump's tweets, and it covers most of his vocabulary with a few extras for good measure. It can even use emoji! There's probably some character set gremlins in there so be careful if you do, I just haven't encountered them.
I've included some sample systemd unit files to start it, and restart it again when tweepy falls over. The martiandtrump.service file needs to be edited with a path to the script and a user who'll run it. Don't use root unless you like to live dangerously.