root/trunk/plagger/lib/Plagger/Plugin/Subscription/HatenaRSS.pm

Revision 107 (checked in by miyagawa, 14 years ago)

Added HatenaRSS Subscription using HTML scraper

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin::Subscription::HatenaRSS;
2 use strict;
3 use base qw( Plagger::Plugin::Subscription::OPML );
4
5 use WWW::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 $start = "https://www.hatena.ne.jp/login?backurl=http%3A%2F%2Fr.hatena.ne.jp%2F";
20
21     # TODO: we should save the cookie and reuse
22     my $mech = WWW::Mechanize->new;
23     $mech->get($start);
24
25     $mech->submit_form(
26         fields => {
27             key      => $self->conf->{username},
28             password => $self->conf->{password},
29         },
30     );
31
32     if ( $mech->content =~ m!<div class="erorr">! ) {
33         $context->log(error => "Login to HatenaRSS failed.");
34     }
35
36     $context->log(info => "Login to HatenaRSS succeed.");
37
38     $mech->get("http://r.hatena.ne.jp/miyagawa/config");
39
40     # HatenaRSS config has two different 'opml' forms :/
41     # so we loop through it and use the latter one
42     for my $form ($mech->forms) {
43         $mech->{form} = $form if $form->attr('name') eq 'opml';
44     }
45
46     $mech->submit_form();
47
48     my $opml = $mech->content;
49     $context->log(info => "Exported OPML: " . length($opml) . " bytes");
50
51     $self->load_opml($context, \$opml);
52 }
53
54 1;
55
56 __END__
57
58 =head1 NAME
59
60 Plagger::Plugin::Subscrption::HatenaRSS - HatenaRSS Subscription via OPML
61
62 =head1 SYNOPSIS
63
64   - module: Subscription::HatenaRSS
65     config:
66       username: example
67       password: xxxxxxxx
68
69 =head1 DESCRIPTION
70
71 This plugin creates Subscription by fetching Hatena RSS
72 L<http://r.hatena.ne.jp> OPML by HTTP. Since Hatena RSS OPML export
73 requires login state, it uses WWW::Mechanize module to emulate the
74 browser's login authentication procedure.
75
76 =head1 AUTHOR
77
78 Tatsuhiko Miyagawa
79
80 =head1 SEE ALSO
81
82 L<Plagger>, L<Plagger::Plugin::Subscription::OPML>, L<WWW::Mechanize>
83
84 =cut
Note: See TracBrowser for help on using the browser.