root/trunk/plagger/lib/Plagger/Plugin/CustomFeed/AmazonAssociateReportJP.pm

Revision 810 (checked in by miyagawa, 14 years ago)
  • added Plagger::Mechanize for a wrapper class around WWW::Mech (to set User-Agent from global config)
  • Updated plugins to use Plagger::Mechanize where WWW::Mech was used.
Line 
1 package Plagger::Plugin::CustomFeed::AmazonAssociateReportJP;
2 use strict;
3 use warnings;
4 use base qw (Plagger::Plugin);
5
6 use Plagger::Mechanize;
7
8 sub register {
9     my ($self, $context) = @_;
10     $context->register_hook(
11         $self,
12         'subscription.load' => \&load,
13     );
14 }
15
16 sub load {
17     my ($self, $context) = @_;
18     my $feed = Plagger::Feed->new;
19     $feed->aggregator(sub { $self->aggregate(@_) });
20     $context->subscription->add($feed);
21 }
22
23 sub aggregate {
24     my ($self, $context, $args) = @_;
25     my $mech = join('::', __PACKAGE__, "Mechanize")->new($self);
26     $mech->login or $context->error('login failed');
27
28     my $feed = Plagger::Feed->new;
29     $feed->type('amazonassociate');
30     $feed->title('Amazon.co.jp アソシエイト・レポート');
31     $feed->link('https://associates.amazon.co.jp/gp/associates/network/reports/main.html');
32
33     my $summary_entry = Plagger::Entry->new;
34     $summary_entry->title('現四半期レポート');
35     $summary_entry->link('https://associates.amazon.co.jp/gp/associates/network/reports/report.html');
36     $summary_entry->date( Plagger::Date->now() );
37     $summary_entry->body($mech->summary_html);
38     $feed->add_entry($summary_entry);
39
40     my $earnings_entry = Plagger::Entry->new;
41     $earnings_entry->title('売上レポート');
42     $earnings_entry->link('https://associates.amazon.co.jp/gp/associates/network/reports/report.html');
43     $earnings_entry->date( Plagger::Date->now() );
44     $earnings_entry->body( $mech->earnings_html);
45     $feed->add_entry($earnings_entry);
46
47     $context->update->add($feed);
48 }
49
50 package Plagger::Plugin::CustomFeed::AmazonAssociateReportJP::Mechanize;
51 use strict;
52 use warnings;
53 use Plagger::Mechanize;
54 use base qw(Class::Accessor::Fast);
55
56 __PACKAGE__->mk_accessors(qw(mech email password start_url));
57
58 sub new {
59     my $class = shift;
60     my $plugin = shift;
61     my $mech = Plagger::Mechanize->new;
62     $mech->agent_alias( "Windows IE 6" );
63     return bless {
64         mech     => $mech,
65         email    => $plugin->conf->{email},
66         password => $plugin->conf->{password},
67         start_url => 'http://www.amazon.co.jp/',
68     }, $class;
69 }
70
71 sub login {
72     my $self = shift;
73     my $mech = $self->mech;
74     my $res = $mech->get($self->start_url);
75     return unless $mech->success;
76     $mech->follow_link(url_regex => qr!associates/join/main\.html!);
77     $mech->follow_link(url_regex => qr!associates/login/login\.html!);
78     $mech->form_number(1);
79     $mech->field(email => $self->email);
80     $mech->field(password => $self->password);
81     $mech->click;
82     return if ($mech->content =~ m!<input name="email" type="text"!); # oops, login failed!
83     return 1;
84 }
85
86 sub summary_html {
87     my $self = shift;
88     if ($self->mech->content =~ m!(<table class="report" id="earningsSummary">.*?</table>)!is) {
89         my $html = $1;
90         $html =~ s!<a [^>]+>.+?</a>!!isg;
91         $html =~ s!<img [^>]+/>!!isg;
92         return $html;
93     }
94 }
95
96 sub earnings_html {
97     my $self = shift;
98     my $html;
99     $self->mech->follow_link(url_regex => qr/report\.html.*?earningsReport/);
100     my $content = $self->mech->content;
101     if ($content =~ m!(<table class="report" id="earningsReport">.*?</table>)!is) {
102         $html = $1;
103     }
104     if ($content =~ m!(<table class="earningsReportSummary">.*?</table>)!is) {
105         $html .= $1;
106     }
107     return $html;
108 }
109
110 1;
111
112 __END__
113
114 =head1 NAME
115
116 Plagger::Plugin::CustomFeed::AmazonAssociateReportJP - Custom feed for
117 Amazon.co.jp associate central
118
119 =head1 SYNOPSIS
120
121   - module: CustomFeed::AmazonAssociateReportJP
122     config:
123       email: foobar@example.com
124       password: barbaz
125
126 =head1 DESCRIPTION
127
128 This plugin fetches your report for Amazon affiliate.
129
130 =head1 CONFIGRATION
131
132 =over 4
133
134 =item email, password
135
136 Credential you need to login to Amazon.co.jp associate central.
137
138 =back
139
140 =head1 AUTHOR
141
142 Naoya Ito E<lt>naoya@bloghackers.netE<gt>
143
144 =head1 SEE ALSO
145
146 L<Plagger>, L<Plagger::Mechanize>
147
148 =cut
Note: See TracBrowser for help on using the browser.