Plagger::Plugin::CustomFeed::GoogleWebmasterToolReport (不具合アリ)
なんとなくできた。Googleウェブマスターツールにログインして、上位の検索クエリ、サイトへのリンクを自動的に取ってくるPlaggerプラグイン。
package Plagger::Plugin::CustomFeed::GoogleWebmasterToolReport;
use strict;
use warnings;
use Plagger::Mechanize;
use base qw (Plagger::Plugin);
sub register {
my ($self, $context) = @_;
$context->register_hook(
$self,
'subscription.load' => \&load,
);
}
sub load {
my ($self, $context) = @_;
my $feed = Plagger::Feed->new;
$feed->aggregator(sub { $self->aggregate(@_) });
$context->subscription->add($feed);
}
sub aggregate {
my ($self, $context, $args) = @_;
my $mech = join('::', __PACKAGE__, "Mechanize")->new($self);
$mech->login or $context->error('login failed');
my $feed = Plagger::Feed->new;
$feed->type('Google Webmaster Tool Report');
$feed->title('Google Webmaster Tool Report');
$feed->link('http://www.google.com/webmasters/tools/?hl=ja');
my @sites = $mech -> find_targets;
my $num_sites = @sites;
$context -> log (debug => "$num_sites");
for (my $i = 1; $i <= $num_sites; $i++) {
my $queries_entry = Plagger::Entry->new;
$queries_entry->title('Search Queries');
$queries_entry->date( Plagger::Date->now() );
$mech->go_top();
$mech->go_dashboard($i);
$queries_entry->body($mech->search_queries_html);
$feed->add_entry($queries_entry);
my $external_links_entry = Plagger::Entry->new;
$external_links_entry->title('External Links');
$external_links_entry->date( Plagger::Date->now() );
$mech->go_top();
$mech->go_dashboard($i);
$external_links_entry->body( $mech->external_links_html);
$feed->add_entry($external_links_entry);
}
$context->update->add($feed);
}
package Plagger::Plugin::CustomFeed::GoogleWebmasterToolReport::Mechanize;
use strict;
use warnings;
use Plagger::Mechanize;
use base qw(Class::Accessor::Fast);
__PACKAGE__->mk_accessors(qw(mech email password start_url));
sub new {
my $class = shift;
my $plugin = shift;
my $mech = Plagger::Mechanize->new;
$mech->agent_alias( "Mac Mozilla" );
return bless {
mech => $mech,
email => $plugin->conf->{email},
password => $plugin->conf->{password},
start_url => 'https://www.google.com/accounts/ServiceLogin?service=sitemaps&passive=true&nui=1&continue=https%3A%2F%2Fwww.google.com%2Fwebmasters%2Ftools%2F&followup=https%3A%2F%2Fwww.google.com%2Fwebmasters%2Ftools%2F&hl=ja',
}, $class;
}
sub login {
my $self = shift;
my $mech = $self->mech;
my $res = $mech->get($self->start_url);
return unless $mech->success;
$mech->form_number(1);
$mech->set_fields('Email' => $self -> email,
'Passwd' => $self -> password);
$mech->submit;
return if ($mech->content =~ m! mech -> find_all_links(url_regex => qr/dashboard/i);
}
sub search_queries_html {
my $self = shift;
my $html;
$self->mech->follow_link(url_regex => qr/top-search-queries/i);
my $content = $self->mech->content;
if ($content =~ m!(
)!is) {
$html = "表示回数\n";
$html .= $1;
}
if ($content =~ m!(
)!is) {
$html .= "クリックスルー\n";
$html .= $1;
}
return $html;
}
sub external_links_html {
my $self = shift;
my $html;
$self->mech->follow_link(url_regex => qr/external-links/i);
my $content = $self->mech->content;
if ($content =~ m!(
)!is) {
$html = "サイトへのリンク\n";
$html .= $1;
}
return $html;
}
sub go_dashboard {
my ($self, $number) = @_;
$number += 0;
$self->mech->follow_link(url_regex => qr/dashboard/i, n => $number);
}
sub go_top {
my $self = shift;
$self -> mech -> follow_link(url_regex => qr/webmasters\/tools\/home\?hl=ja/i);
}
1;
__END__
それにしてもひどいコードだw
コメント