RSS Integrator for mixi
ピアノの話題とそれ以外で2つに分けているブログを、マイミク(笑)のみんなのために両方 mixi の日記更新情報に表示したいぜ!と自意識過剰気味なことを思ったので、2つのRSSフィードを統合するPHPスクリプトを書いてみた。ていうかライブラリ使ったら一瞬で完成した。なんという素晴らしい時代。
読み込みはLastRSS、出力は、PHP Universal Feed Generator を使わせていただきました。
1点だけ、PHP Universal Feed Generator の makeNode メソッド(311行目) の htmlentities 関数で第3引数が指定されていないせいで UTF-8 文字列のエンティティ化がこけていたので修正した。
$nodeText .= (in_array($tagName, $this->CDATAEncoding)) ?
$tagContent : htmlentities($tagContent);
↓
$nodeText .= (in_array($tagName, $this->CDATAEncoding)) ?
$tagContent : htmlentities($tagContent, ENT_COMPAT, 'UTF-8');
あとは、エントリの更新時刻を mixi のクローラーが更新後最初に訪れた時間に書き換えてやるようにするとうれしいかも(主に自分が)。
追記
Sun, 08 Jun 2008 00:23:40 JST が mixi 側では 6/8 9:23 更新 と表示されていたので pubDate のタイムゾーンを GMT にしてみた。日時文字列の "JST" を "+0900" にしてもいける気がするけど。
コード全文
<?php date_default_timezone_set('GMT'); include_once (dirname(__FILE__) . '/lib/FeedWriter/FeedWriter.php'); include_once (dirname(__FILE__) . '/lib/lastRSS/lastRSS.php'); $feed_urls = array( 'http://d.hatena.ne.jp/shinichi_nomura/rss2', 'http://piano.izanagi-izanami.net/blog/?feed=rss2' ); $last_rss = new lastRSS(); $last_rss->cache_dir = ''; $last_rss->cache_time = 0; $last_rss->CDATA = 'content'; $feeds = array(); $items = array(); // フィード読み込み&統合 foreach ($feed_urls as $url) { if ($feed = $last_rss->get($url)) { foreach ($feed['items'] as $item) { $items[] = array_merge( $item, array('feedUrl' => $url) ); } unset($feed['items']); $feeds[$url] = $feed; } } usort($items, 'sortfunc'); $writer = new FeedWriter(RSS2); // フィード作成 if (count($items) > 0) { $delegate = $feeds[$items[0]['feedUrl']]; foreach ($items as $item) { $itemObj = $writer->createNewItem(); $itemObj->setTitle($item['title']); $itemObj->setLink($item['link']); $itemObj->setDate($item['pubDate']); $itemObj->setDescription($item['description']); $writer->addItem($itemObj); } } else { $delegate = $feeds[$feed_urls[0]]; } $writer->setTitle($delegate['title']); $writer->setLink($delegate['link']); $writer->setDescription($delegate['description']); $writer->genarateFeed(); function sortfunc ($a, $b) { $atime = strtotime($a['pubDate']); $btime = strtotime($b['pubDate']); if ($atime == $btime) { return 0; } else { return ($atime > $btime) ? -1 : 1; } } ?>