Perl で PubSubHubBub 対応の RSS フィードを作成するメモです。
PubSubHubBub って何だというのはググればわかると思うので割愛。とりあえず Ping の改良版みたいな感じで Ping より早く Google にインデックスされるよって認識で良いと思う。
オリジナルの CMS で作成したコンテンツのインデックスを促すために PubSubHubBub を使おうという魂胆です。
モジュールは XML::FeedPP を使います。そして、下記記事の続きです。
まずは PubSubHubBub 対応のコードから。
#!/usr/bin/perl
use strict;
use warnings;
use Encode;
use DateTime;
use XML::FeedPP;
my $language = 'ja';
my $title = 'Example RSS フィード';
my $url = 'http://example.jp/';
my $dt = DateTime->now(time_zone => 'local');
my $pubdate = $dt->strftime('%Y-%m-%dT%H:%M:%S%z');
my $pub = {
'-rel' => 'self',
'-type' => 'application/rss+xml',
'-href' => $url . 'index.rdf',
};
my $sub = {
'-rel' => 'hub',
'-href' => 'http://pubsubhubbub.appspot.com',
};
my $hub = {
'-rel' => 'hub',
'-href' => 'http://pubsubhubbub.superfeedr.com',
};
my $item1 = {
title => 'タイトルその1',
link => 'http://example.jp/1.html',
description => '<p>あああいいいうううえええおおお</p>',
};
my $item2 = {
title => 'タイトルその2',
link => 'http://example.jp/2.html',
description => '<p>かかかきききくくくけけけこここ</p>',
};
my @items = ($item1, $item2);
my $feed = XML::FeedPP::RSS->new();
$feed->xmlns('xmlns:atom' => 'http://www.w3.org/2005/Atom');
$feed->language($language);
$feed->set( 'atom:link' => [$pub, $sub, $hub] );
$feed->title($title);
$feed->link($url);
$feed->pubDate($pubdate);
foreach my $item (@items) {
$feed->add_item(
link => $item->{link},
title => $item->{title},
description => $item->{description},
pubDate => $pubdate,
);
}
print $feed->to_string('UTF-8');
前回とそんなに違いはないが、PubSubHubBub に必要な記述をいくつか追加している。
hub の URL は WordPress 用のプラグインを参考に二つ付けてみたが「pubsubhubbub.appspot.com」だけでも大丈夫だと思う。
これを実行すると下記のような感じに。
<?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> <channel> <title>Example RSS フィード</title> <link>http://example.jp/</link> <atom:link rel="self" type="application/rss+xml" href="http://example.jp/index.rdf" /> <atom:link rel="hub" href="http://pubsubhubbub.appspot.com" /> <atom:link rel="hub" href="http://pubsubhubbub.superfeedr.com" /> <language>ja</language> <pubDate>Thu, 07 Aug 2014 17:58:14 +0900</pubDate> <item> <title>タイトルその1</title> <link>http://example.jp/1.html</link> <guid isPermaLink="true">http://example.jp/1.html</guid> <pubDate>Thu, 07 Aug 2014 17:58:14 +0900</pubDate> <description><p>あああいいいうううえええおおお</p></description> </item> <item> <title>タイトルその2</title> <link>http://example.jp/2.html</link> <guid isPermaLink="true">http://example.jp/2.html</guid> <pubDate>Thu, 07 Aug 2014 17:58:14 +0900</pubDate> <description><p>かかかきききくくくけけけこここ</p></description> </item> </channel> </rss>
これで受け ? は出来たので、後はこれを送信するための機能を作るだけです。
続きは次回書きます。