| 1 | package Plagger::Plugin::Subscription::HatenaRSS; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin::Subscription::OPML ); | ||
| 4 | |||
| 5 | use Plagger::Mechanize; | ||
| 6 | |||
| 7 | sub register { | ||
| 8 | my($self, $context) = @_; | ||
| 9 | |||
| 10 | $context->register_hook( | ||
| 11 | $self, | ||
| 12 | 'subscription.load' => \&load, | ||
| 13 | ); | ||
| 14 | } | ||
| 15 | |||
| 16 | sub load { | ||
| 17 | my($self, $context) = @_; | ||
| 18 | |||
| 19 | my $username = $self->conf->{username} | ||
| 20 | or $context->error("username is missing"); | ||
| 21 | |||
| 22 | my $mech = Plagger::Mechanize->new(cookie_jar => $self->cookie_jar); | ||
| 23 | $mech->get("http://r.hatena.ne.jp/$username/opml"); | ||
| 24 | |||
| 25 | if ($mech->content !~ /<opml version/) { | ||
| 26 | $mech->get("https://www.hatena.ne.jp/login?backurl=http%3A%2F%2Fr.hatena.ne.jp%2F"); | ||
| 27 | $mech->submit_form( | ||
| 28 | fields => { | ||
| 29 | key => $username, | ||
| 30 | password => $self->conf->{password}, | ||
| 31 | }, | ||
| 32 | ); | ||
| 33 | |||
| 34 | if ( $mech->content =~ m!<div class="error">! ) { | ||
| 35 | $context->log(error => "Login to HatenaRSS failed."); | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | $context->log(info => "Login to HatenaRSS succeed."); | ||
| 41 | |||
| 42 | my $opml = $mech->content; | ||
| 43 | $context->log(info => "Exported OPML: " . length($opml) . " bytes"); | ||
| 44 | |||
| 45 | $self->load_opml($context, \$opml); | ||
| 46 | } | ||
| 47 | |||
| 48 | 1; | ||
| 49 | |||
| 50 | __END__ |