日向夏特殊応援部隊

俺様向けメモ

Japanese Mozilla HackersなブログのフィードをOPML化

実は、ニッチなJavaScriptブログと言う位置づけで、Ci.nsIZIGOROu - Mozilla 拡張機能勉強会と言うブログもやってる訳ですが、
最近こんなエントリを書きました。

で見かけたブクマコメントですが、id:tokuhiromさんが、

OPMLにしてほしい

と申していたのでOPML化してみました。

ご自由にお使い下さい。

OPML化したときのスクリプト

なんかちまちま作るのはバカらしいので、こういうときはPerlで書かないと。
元の記事から本文っぽぃ部分にあるアンカーをモリっとXPathで取って来て、Feed Discoveryかけて〜ってな感じです。

#!/usr/bin/perl

use strict;
use warnings;

use DateTime;
use DateTime::Format::HTTP;
use URI;
use URI::Match;
use LWP::UserAgent;
use HTML::TreeBuilder::XPath;
use XML::Atom;
use XML::Feed;
use XML::Feed::RSS;
use XML::OPML;
use XML::Liberal;
use Encode;

$XML::Feed::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
$XML::Atom::ForceUnicode = 1;

my $ua = LWP::UserAgent->new;
my $res = $ua->get("http://moz-addon.g.hatena.ne.jp/ZIGOROu/20071223/1198434207");
die unless ($res->is_success);

my $tree = HTML::TreeBuilder::XPath->new;
$tree->parse_content($res->content);

my $nodes = $tree->findnodes('/descendant::div[@class="section"]/descendant::a[@target="_blank"]');
die if ($nodes->size == 0);

my $opml = XML::OPML->new(version => "1.1");
$opml->head(
    title => 'Mozilla Japanese Hackers OPML',
    dateCreated => 'Wed, 26 Dec 2007 06:25:38 GMT',
    dateModified => DateTime::Format::HTTP->format_datetime(DateTime->now),
    ownerEmail => "zigorou\x40cpan.org",
);

my %seen = ();

XML::Liberal->globally_override('LibXML');

for (my $i = 0; $i < $nodes->size; $i++) {
    my $node = $nodes->get_node($i);
    my $uri = URI->new($node->attr("href"));
    next if (exists $seen{$uri->as_string});
    $seen{$uri->as_string} = 1;

    next if ($uri->match_parts(host => qr/^(b\.hatena\.ne\.jp|twitter\.com)/));

    my @feeds = XML::Feed->find_feeds($uri);

    for my $feed_url (grep { $_->match_parts(scheme => qr/^https?$/) } map { URI->new($_) } @feeds) {
        next if ($feed_url->match_parts(path => qr/rss2$/));

        my $res = $ua->get($feed_url->as_string);
        next unless ($res->is_success);

        my $content = $res->content;

        my $feed = XML::Feed->parse(\$content);
        if (XML::Feed->errstr) {
            XML::Feed->error("");
            next;
        }

        $opml->add_outline(type => 'rss', version => 'RSS', title => $feed->title, description => $feed->description, htmlUrl => $feed->link, xmlUrl => $feed_url);
        last;
    }
}

print Encode::encode("utf-8", $opml->as_string);

多分にPlagger::FeedParserのソースを参考にしました。
と言うかもの凄くパクった。


あとURI::Match便利だゎー。dmaki++

駄文

最近Perl界隈ではNEXT問題が取りざたされてるけど、Mozilla界隈もblog間でそういう技術的な意見交換とかあっていいと思った。とか思ってめぼしいMozillaな人々のブログを抽出してまずは日の目に当たる状況に〜なんて言うのがバックエンドにあったりなかったり。

やっぱり技術的な面であれこれ意見交換してかないとコミュニティ全体のレベルが上がらないんじゃないかとか思ってみたりみなかったり。

ちなみに僕はNEXTもC3も嫌いです。でCatalystDBICプラグイン機構で継承順意識するなんてバカらし過ぎる。