Publish::Gmailで送信してみると、メールでこんなようなレポートを受信できる。
.yamlファイルで設定したGoogleアカウントで管理しているサイトについて、全部一括でレポートを作成できるようになったので、実現したいことは大体できるようになった。
文言の表示とか、テーブルそのまま取ってくるとかどうよ?みたいな、細かい部分はそのうち直す。
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 on ' . Plagger::Date->now());
$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;
$mech->go_top();
$queries_entry->title("上位の検索クエリ site #$i");
$queries_entry->date( Plagger::Date->now() );
$mech->go_dashboard($i);
$queries_entry->body($mech->search_queries_html);
$feed->add_entry($queries_entry);
my $external_links_entry = Plagger::Entry->new;
$mech->go_top();
$external_links_entry->title("外部リンク site #$i");
$external_links_entry->date( Plagger::Date->now() );
$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!
return 1;
}
sub find_targets {
my $self = shift;
return $self -> 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/webmasters\/tools\/dashboard/i,
n => $number);
}
sub go_top {
my $self = shift;
$self -> mech -> follow_link(url_regex => qr/webmasters\/tools\/home/i);
}
1;
__END__
設定の.yamlファイルはこんな感じ。
global:
assets_path: /home/yz/local/lib/perl5/site_perl/5.8.8/Plagger/assets
plugin_path: /home/yz/local/lib/perl5/site_perl/5.8.8/Plagger/Plugin
timezone: Asia/Tokyo
log:
level: info
plugins:
- module: CustomFeed::GoogleWebmasterToolReport
config:
email: YOUR_GOOGLE_ACCOUNT
password: GOOGLE_PASSWORD
- module: Publish::Gmail
config:
mailto: you@example.com
mailfrom: me@example.com
メールで送るよりも、CSVとかに吐き出してファイルに保存しておく方が便利かもしれない。ま、メールで送ったのを後でエクセル上で加工すればいいような気もする。どうせ毎日気にしなきゃいけないようなデータではないのだし。
Posted by yz at 2009年09月08日 12:34
| コメント (0)
| トラックバック (0)

| .
|
|
|
|
この記事に対するコメントはまだありません。
コメントを投稿する
この記事のトラックバックURL
この記事に対するトラックバック
この記事に対するトラックバックはまだありません。


この記事に対するコメント