| 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 |
my $orders_entry = Plagger::Entry->new; |
|---|
| 48 |
$orders_entry->title('注文レポート'); |
|---|
| 49 |
$orders_entry->link('https://associates.amazon.co.jp/gp/associates/network/reports/report.html'); |
|---|
| 50 |
$orders_entry->date( Plagger::Date->now() ); |
|---|
| 51 |
$orders_entry->body( $mech->orders_html); |
|---|
| 52 |
$feed->add_entry($orders_entry); |
|---|
| 53 |
|
|---|
| 54 |
$context->update->add($feed); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
package Plagger::Plugin::CustomFeed::AmazonAssociateReportJP::Mechanize; |
|---|
| 58 |
use strict; |
|---|
| 59 |
use warnings; |
|---|
| 60 |
use Plagger::Mechanize; |
|---|
| 61 |
use base qw(Class::Accessor::Fast); |
|---|
| 62 |
|
|---|
| 63 |
__PACKAGE__->mk_accessors(qw(mech email password start_url)); |
|---|
| 64 |
|
|---|
| 65 |
sub new { |
|---|
| 66 |
my $class = shift; |
|---|
| 67 |
my $plugin = shift; |
|---|
| 68 |
my $mech = Plagger::Mechanize->new; |
|---|
| 69 |
$mech->agent_alias( "Windows IE 6" ); |
|---|
| 70 |
return bless { |
|---|
| 71 |
mech => $mech, |
|---|
| 72 |
email => $plugin->conf->{email}, |
|---|
| 73 |
password => $plugin->conf->{password}, |
|---|
| 74 |
start_url => 'http://affiliate.amazon.co.jp/gp/associates/join/main.html', |
|---|
| 75 |
}, $class; |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
sub login { |
|---|
| 79 |
my $self = shift; |
|---|
| 80 |
my $mech = $self->mech; |
|---|
| 81 |
my $res = $mech->get($self->start_url); |
|---|
| 82 |
return unless $mech->success; |
|---|
| 83 |
$mech->follow_link(url_regex => qr!associates/login/login\.html!); |
|---|
| 84 |
$mech->form_number(1); |
|---|
| 85 |
$mech->field(email => $self->email); |
|---|
| 86 |
$mech->field(password => $self->password); |
|---|
| 87 |
$mech->click; |
|---|
| 88 |
return if ($mech->content =~ m!<input name="email" type="text"!); |
|---|
| 89 |
return 1; |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
sub summary_html { |
|---|
| 93 |
my $self = shift; |
|---|
| 94 |
if ($self->mech->content =~ m!(<table class="report" id="earningsSummary">.*?</table>)!is) { |
|---|
| 95 |
my $html = $1; |
|---|
| 96 |
$html =~ s!<a [^>]+>.+?</a>!!isg; |
|---|
| 97 |
$html =~ s!<img [^>]+/>!!isg; |
|---|
| 98 |
return $html; |
|---|
| 99 |
} |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
sub earnings_html { |
|---|
| 103 |
my $self = shift; |
|---|
| 104 |
my $html; |
|---|
| 105 |
$self->mech->follow_link(url_regex => qr/report\.html.*?earningsReport/); |
|---|
| 106 |
my $content = $self->mech->content; |
|---|
| 107 |
if ($content =~ m!(<table class="report" id="earningsReport">.*?</table>)!is) { |
|---|
| 108 |
$html = $1; |
|---|
| 109 |
} |
|---|
| 110 |
if ($content =~ m!(<table class="earningsReportSummary">.*?</table>)!is) { |
|---|
| 111 |
$html .= $1; |
|---|
| 112 |
} |
|---|
| 113 |
return $html; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
sub orders_html { |
|---|
| 117 |
my $self = shift; |
|---|
| 118 |
my $html; |
|---|
| 119 |
$self->mech->follow_link(url_regex => qr/report\.html.*?ordersReport/); |
|---|
| 120 |
$self->mech->submit_form(form_number => 8); |
|---|
| 121 |
my $content = $self->mech->content; |
|---|
| 122 |
if ($content =~ m!(<table class="report" id="ordersReport">.*?</table>)!is) { |
|---|
| 123 |
$html = $1; |
|---|
| 124 |
} |
|---|
| 125 |
if ($content =~ m!(<table class="ordersReportSummary">.*?</table>)!is) { |
|---|
| 126 |
$html .= $1; |
|---|
| 127 |
} |
|---|
| 128 |
return $html; |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
1; |
|---|
| 132 |
|
|---|
| 133 |
__END__ |
|---|
| 134 |
|
|---|
| 135 |
=head1 NAME |
|---|
| 136 |
|
|---|
| 137 |
Plagger::Plugin::CustomFeed::AmazonAssociateReportJP - Custom feed for |
|---|
| 138 |
Amazon.co.jp associate central |
|---|
| 139 |
|
|---|
| 140 |
=head1 SYNOPSIS |
|---|
| 141 |
|
|---|
| 142 |
- module: CustomFeed::AmazonAssociateReportJP |
|---|
| 143 |
config: |
|---|
| 144 |
email: foobar@example.com |
|---|
| 145 |
password: barbaz |
|---|
| 146 |
|
|---|
| 147 |
=head1 DESCRIPTION |
|---|
| 148 |
|
|---|
| 149 |
This plugin fetches your report for Amazon affiliate. |
|---|
| 150 |
|
|---|
| 151 |
=head1 CONFIGS |
|---|
| 152 |
|
|---|
| 153 |
=over 4 |
|---|
| 154 |
|
|---|
| 155 |
=item email, password |
|---|
| 156 |
|
|---|
| 157 |
Credential you need to login to Amazon.co.jp associate central. |
|---|
| 158 |
|
|---|
| 159 |
=back |
|---|
| 160 |
|
|---|
| 161 |
=head1 AUTHOR |
|---|
| 162 |
|
|---|
| 163 |
Naoya Ito E<lt>naoya@bloghackers.netE<gt> |
|---|
| 164 |
|
|---|
| 165 |
=head1 SEE ALSO |
|---|
| 166 |
|
|---|
| 167 |
L<Plagger>, L<Plagger::Mechanize> |
|---|
| 168 |
|
|---|
| 169 |
=cut |
|---|