23 Apr
Making Google Reader Tweet
Looking for a way to get shared items from Google Reader into Twitter, I came across a service caller TwitterFeed, which periodically queries an RSS or Atom feed and tweets any new posts – useful for automatically tweeting your blog, for instance. Well, Reader’s shared items are publicly accessible as an Atom feed (choose Your Stuff->Shared Items, there’s a link to it in your shared items in the blue box, and on that page there’s an Atom feed link). Bingo! Except not quite, because those items are kind of verbose for a tweet – if you’ve set a note, it starts “Shared by Douglas” (if you’re called Douglas, which handily I am). That’s a whole extra 17 characters! Plus, after the note, the actual content of the article starts, and it’s not clear which bit I wrote.
To fix this, I broke out the PHP – the idea being I’d read in the feed, extract the note, and write out a new feed with only what I wanted to tweet in it. Half an hour’s hackery later and I had a working feed for just the notes (you need to get TwitterFeed to tweet the description only for this to work, and if you share without a note it just uses the title). If you want to use it, you need SimplePie (remember to have a writable cache directory) – also, I’m pretty sure I’m not using preg_match properly, but it’s been ages since I’ve done any real PHP, and I’m not really that bothered about it.
Oh, and if you want to follow me on Twitter, I’m douglasgresham. Say hi
<?php
// Change this depending on your shared items’ public URL.
$feedurl = ‘http://www.google.com/reader/public/atom/user%2F06975983408107596′.
‘343%2Fstate%2Fcom.google%2Fbroadcast’;
// The author’s name to show for feed items.
$author = ‘Douglas Gresham’;
include(’simplepie.inc’);
$feed = new SimplePie();
$feed->set_feed_url($feedurl);
$feed->init();
$feed->handle_content_type();
if ($feed->data) {
header(‘Content-Type: text/xml;charset=iso-8859-1′);
// Write the feed info.
echo ‘<?xml version=”1.0″ encoding=”ISO-8859-1″ ?>’;
echo ‘<rss version=”2.0″>’;
echo ‘<channel>’;
echo ‘<title>’.$feed->get_title().’</title>’;
echo ‘<link>’.$feed->get_link().’</link>’;
echo ‘<language>en-gb</language>’;
foreach ($feed->get_items() as $item) {
// Extract the note only.
$regex = ‘/<blockquote>Shared by.+?<br>(.+?)<\/blockquote>/is’;
preg_match($regex, $item->get_description(), $matches);
if (sizeof($matches) >= 2) {
// We have a matched note! Let’s make that the description.
$desc = trim(strip_tags($matches[1]));
} else {
// No note – use the title as the description.
$desc = $item->get_title();
}
// Write out the item.
echo ‘<item>’;
echo ‘<title>’.$item->get_title().’</title>’;
echo ‘<link>’.$item->get_link().’</link>’;
echo ‘<description>’.$desc.’</description>’;
echo ‘<author>’.$author.’</author>’;
echo ‘<pubDate>’.$item->get_date(‘D, d M Y G:i:s O’).’</pubDate>’;
echo ‘</item>’;
}
echo ‘</channel>’;
echo ‘</rss>’;
}
?>


Respond to this post