Skip to content

Posting from XML Feeds to Twitter. Or, This post’s title is entirely too long to fit in a single Tweet; whatever can be done about this grave situation?

I recently decided that I’d like to link to all of my blog posts from Twitter; obviously, this is a task for a script, and while I found a lot of blog posts describing one or two pieces of the process, when I put together a list of features I wanted:

  • Periodically poll an XML Feed for blog posts
  • In chronological order, link to new posts from Twitter
  • Use an URL shortening/tracking service
  • Automatically truncate long titles*
  • Limit number of posts linked to per run
  • No external dependencies (i.e., nonstandard PHP libraries, MySQL, etc)

and started Googling for it, all I came across were postings on various script-for-hire sites offering to buy or sell such a script, mostly in the $5-15 range. While that seems like a pretty small price to pay for the convenience, I don’t think I’d have been able to hold my head up high knowing that I’d paid someone else to write such a simple script. So, I started piecing together all of the parts, and came up with this PHP class, which I tossed up on Gist for public consumption:

TwitterFeedSync.php (raw)

As with many of my one-off scripts, this one is sorely lacking in documentation, and I’m not going to break it down line-by-line here as I often do, but I’ve intentionally broken down the steps into discrete chunks that should be pretty easy to digest. In general, the logical flow goes something like this:

  1. Load the timestamp of the last linked post from a file
  2. Load up the XML feed
  3. Starting at the end of the feed, loop through all entries
  4. Compare the date of each entry to the saved timestamp
  5. Shorten link to post using tr.im
  6. Calculate how much space we have left in the tweet for the title, and truncate if needed
  7. Post the title + link to Twitter
  8. Check if we’ve posted our max for this run
  9. Update the saved timestamp

The code linked above includes a sample invocation at the bottom, and can be called every other hour with a cron job entry similar to:

45 */2 * * * /export/scripts/TwitterFeedSync.php

I think it’s likely that the file I/O code, used to store and retrieve the timestamp of the most recent post, could be much more robust; in particular, you mught have to ‘touch’ the timestamp file into existence in order to get the script to run for the first time. Also, the default $date_format parameter is structured to work well with the feed for this blog, which of course is generated by Wordpress; other feeds may require some tweaking. But hopefully, someone out there besides myself can make use of this little snippet of code; if so, I’d encourage you to consider yourself $5-15 richer, and think about how you in turn can pass that value on to others in your world!

* as it turns out, I haven’t needed this yet; maybe I’ll pump up the length on this post’s title…  :)

Categories: Random.

Tags: , , , , ,

Comment Feed

No Responses (yet)



Some HTML is OK

or, reply to this post via trackback.