just enough to get you started and leave you confused

Tweeting a new Octopress-Post to Twitter

| Kommentare

As I said, there were a few nuisances with this new shiny blogging engine1:

  • No queue
  • No automatic tweets
  • No automatic Quicklinks posts

In this post I’ll look at this Twitter thing.

I want, that a new tweet gets tweeted each time I publish a new post to While running on Wordpress, a nice plugin handled this for me. But, well, there are probably a few thousand other ways to do this, even for a static HTML-site.

One of the easiest surely is Twitterfeed. Just point this free service at your RSS-feed and let it forward your postings to Twitter and/or Facebook. They even got some tracking going on.

Another candidate is ifttt2. This service allows it to define events, which trigger actions like mails, tweets or facebook posts and you can do some really awesome things with it.

But I did it yet another way, by using a tiny Ruby script which lives on the Mac mini at home. If you’re interested in the nitty gritty details, be sure to follow me after the click…

Still with me? Good.

Script Files

The script is based on twitterscript2 by Michael Morin. It was modified by me to use the current OAuth-authentication used by Twitter, use YAML instead of Marshal to serialize and deserialize data and to better suit my needs in general.

The whole thing consists of the script itself, it_tweet.rb, a config file3 named config.yml, a cache file4 named tweet_times.yaml and a LaunchAgent which goes by the name of de.instant-thinking.it_tweet.plist.

Script Function

So what does it do?


The script gets called every five minutes by the LauchAgent. It loads some gems and reads it’s config variables from the config file.

It then proceeds to load the configured RSS-feed from the web and turns it upside down, to have a look at the oldest entry first. If the publication date of the entry is newer than the timestamp from the last run of the script, it’s title and link get extracted and tweeted as a New Blogpost tweet to my Twitter account. Then the script sends me a mail, telling me that it just tweeted a new tweet and updates the timestamp to the date and time of the just tweeted entry.

It then sleeps a minute, before it continues with the second oldest feed entry.

If the cached timestamp is newer than the currently processed feed entry no tweeting, mailing or sleeping takes place, the script simply continues to the next feed entry.

So on the first run, you end up with all your feed items getting tweeted with a pause of 60 seconds between each tweet5. After this initial tweetstorm things settle down and become much saner for you and your follower.

And that’s about it, really.

Lessons learned

It is not always necessary to use a fancy service on the Internet to get things done. Sometimes all it takes is a trusty Mac mini running a customized script.

I also learned, that for every major problem one could encounter when starting to write a script, somebody probably released a nice rubygem suited to help you. The twitter gem makes tweeting trivial, the mail notification would be much harder to code, without pony.

How to tweet along

The whole thing lives on github6 and I hope it can be of some use for somebody. Suggestions and patches are also very welcome.

If you decide to use it, just go ahead and fork/clone from Github to some place on your computer.

You then have to remove the .example extension from the config file and from the LaunchAgent-plist, to make them usable.

The feed-variable in config.yml obviously gets filled with your feed-address.

You’ll have to obtain your Twitter-OAuth-credentials by registering an app at Twitters developer site and copy & paste them to your config file.

The mail settings are pre-configured for Google-Mail, if you got a Gmail-account7 you’ll simply have to insert your address and password in the from- and smtp-fields. Configuration for any other mail-provider is left as an exercise to the reader and will probably require some work on the pony section in the script itself…

When you finished the config you can execute the script. It should start to tweet the items of your feed to your Twitter-stream.

To automate the process, you’ll have to move the plist-file to ~/Library/LaunchAgents and change the absolute path to it_tweet.rb to wherever you put it.

Also: It should be no problem to run the script on a Linux-system. In this case, you’d have to make a cron-entry instead of a LaunchAgent. I’ll continue with the OS X way because that’s what I used.

So, you load the LaunchAgent with launchctl:

Load LaunchAgent
launchctl load ~/Library/LaunchAgents/de.instant-thinking.it_tweet.plist

And check that it got loaded:

Check LaunchAgent
~ > launchctl list | grep  de.instant-thinking.it_tweet
-	0	de.instant-thinking.it_tweet

The 0 is the exit code of the last run. A zero is fine, any other number is bad.

After five minutes you should see some activity in your system.log:

Check system.log
~ > grep instant-thinking.it_tweet  /var/log/system.log
May  5 14:33:36 Mac-mini de.instant-thinking.it_tweet[84008]: Checking for tweetable posts...

If the script encounters new feed entries, it will tell you about these in the logs, too.

Congratulations, you just set up your very own RSS-to-Twitter app with mail notifications.

  1. I’m pretty sure there are some nuisances with my english, too. Let’s see how it turns out…

  2. Stands for If This Then That. I really like this acronym…

  3. For storing things like filenames, feed-URL, OAuth- and SMTP-credentials

  4. to save a timestamp of the last run

  5. Unless you uncomment the tweet in the script, maybe I’ll add a no-tweet-option in a future version

  6. Alas not the complete development is online. I use a private development, public release workflow because most early commits were just to embarrassing…

  7. And who doesn’t?