Perl で PubSubHubBub 対応の RSS フィードを作成するメモです。
PubSubHubBub って何だというのはググればわかると思うので割愛。とりあえず Ping の改良版みたいな感じで Ping より早く Google にインデックスされるよって認識で良いと思う。
オリジナルの CMS で作成したコンテンツのインデックスを促すために PubSubHubBub を使おうという魂胆です。
モジュールは XML::FeedPP を使います。そして、下記記事の続きです。
まずは PubSubHubBub 対応のコードから。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #!/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」だけでも大丈夫だと思う。
これを実行すると下記のような感じに。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <? 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 > |
これで受け ? は出来たので、後はこれを送信するための機能を作るだけです。
続きは次回書きます。