| 1 | package Net::Google::WebmasterTools; | ||
| 2 | { | ||
| 3 | $Net::Google::WebmasterTools::VERSION = '0.01'; | ||
| 4 | } | ||
| 5 | use strict; | ||
| 6 | |||
| 7 | # ABSTRACT: Simple interface to the Google WebmasterTools Core Reporting API | ||
| 8 | |||
| 9 | use JSON; | ||
| 10 | use LWP::UserAgent; | ||
| 11 | use Net::Google::WebmasterTools::Request; | ||
| 12 | use Net::Google::WebmasterTools::Response; | ||
| 13 | use Net::Google::WebmasterTools::Row; | ||
| 14 | use URI; | ||
| 15 | use Data::Dumper; | ||
| 16 | use Mojo::Log; | ||
| 17 | |||
| 18 | my $log = Mojo::Log->new; | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my $package = shift; | ||
| 22 | |||
| 23 | my $self = bless({}, $package); | ||
| 24 | |||
| 25 | return $self; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub auth_params { | ||
| 29 | my $self = shift; | ||
| 30 | |||
| 31 | my $auth_params = $self->{auth_params} || []; | ||
| 32 | |||
| 33 | if (@_) { | ||
| 34 | $self->{auth_params} = [ @_ ]; | ||
| 35 | } | ||
| 36 | |||
| 37 | return @$auth_params; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub token { | ||
| 41 | my ($self, $token) = @_; | ||
| 42 | |||
| 43 | $self->{auth_params} = [ | ||
| 44 | Authorization => "$token->{token_type} $token->{access_token}", | ||
| 45 | ]; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub user_agent { | ||
| 49 | my $self = $_[0]; | ||
| 50 | |||
| 51 | my $ua = $self->{user_agent}; | ||
| 52 | |||
| 53 | if (@_ > 1) { | ||
| 54 | $self->{user_agent} = $_[1]; | ||
| 55 | } | ||
| 56 | elsif (!defined($ua)) { | ||
| 57 | $ua = LWP::UserAgent->new(); | ||
| 58 | $self->{user_agent} = $ua; | ||
| 59 | } | ||
| 60 | |||
| 61 | return $ua; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub new_request { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | return Net::Google::WebmasterTools::Request->new(@_); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub _uri { | ||
| 71 | my ($self, $req, $start_index, $row_limit) = @_; | ||
| 72 | |||
| 73 | my $uri = URI->new('https://www.googleapis.com/webmasters/v3/sites/' . $req->site_url . '/' . $req->report_name . '/' . $req->method); | ||
| 74 | if (($req->report_name =~ m{^(?:sitemaps|urlCrawlErrorsSamples)$}) && ($req->method eq 'list')) { | ||
| 75 | $uri = URI->new('https://www.googleapis.com/webmasters/v3/sites/' . $req->site_url . '/' . $req->report_name); | ||
| 76 | } elsif (($req->report_name eq 'sites') && ($req->method eq 'list')) { | ||
| 77 | $uri = URI->new('https://www.googleapis.com/webmasters/v3/sites'); | ||
| 78 | } | ||
| 79 | print "$uri\n"; | ||
| 80 | my @params; | ||
| 81 | push(@params, 'start-index' => $start_index) if defined($start_index); | ||
| 82 | push(@params, 'max-results' => $row_limit) if defined($row_limit); | ||
| 83 | |||
| 84 | $uri->query_form( | ||
| 85 | $req->_params, | ||
| 86 | @params, | ||
| 87 | ); | ||
| 88 | |||
| 89 | return $uri; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub uri { | ||
| 93 | my ($self, $req) = @_; | ||
| 94 | |||
| 95 | return $self->_uri($req, $req->start_index, $req->row_limit); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _retrieve_http { | ||
| 99 | my ($self, $req, $start_index, $row_limit) = @_; | ||
| 100 | |||
| 101 | my $uri = $self->_uri($req, $start_index, $row_limit); | ||
| 102 | |||
| 103 | my $params = $req; | ||
| 104 | my @auth_params = $self->auth_params; | ||
| 105 | |||
| 106 | my %params = $req->_params; | ||
| 107 | my $json = to_json(\%params); | ||
| 108 | |||
| 109 | my $http_req = HTTP::Request->new( 'POST', $uri->as_string ); | ||
| 110 | $http_req->header( @auth_params ); | ||
| 111 | $http_req->header( 'Content-Type' => 'application/json' ); | ||
| 112 | $http_req->content( $json ); | ||
| 113 | |||
| 114 | if (($req->report_name eq 'sitemaps') && ($req->method eq 'list')) { | ||
| 115 | $http_req = HTTP::Request->new( 'GET', $uri->as_string ); | ||
| 116 | } elsif (($req->report_name eq 'sites') && ($req->method eq 'list')) { | ||
| 117 | $http_req = HTTP::Request->new( 'GET', $uri->as_string ); | ||
| 118 | } | ||
| 119 | |||
| 120 | return $self->user_agent->request($http_req); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub retrieve_http { | ||
| 124 | my ($self, $req) = @_; | ||
| 125 | |||
| 126 | return $self->_retrieve_http($req, $req->start_index, $req->row_limit); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _retrieve { | ||
| 130 | my ($self, $req, $start_index, $row_limit) = @_; | ||
| 131 | |||
| 132 | my $http_res = $self->_retrieve_http($req, $start_index, $row_limit); | ||
| 133 | my $res = Net::Google::WebmasterTools::Response->new; | ||
| 134 | $res->code($http_res->code); | ||
| 135 | $res->message($http_res->message); | ||
| 136 | |||
| 137 | if (!$http_res->is_success) { | ||
| 138 | $res->content($http_res->decoded_content); | ||
| 139 | return $res; | ||
| 140 | } | ||
| 141 | |||
| 142 | my $json = from_json($http_res->decoded_content); | ||
| 143 | $res->_parse_json($json); | ||
| 144 | |||
| 145 | $res->start_index($start_index); | ||
| 146 | $res->is_success(1); | ||
| 147 | |||
| 148 | return $res; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub retrieve { | ||
| 152 | my ($self, $req) = @_; | ||
| 153 | |||
| 154 | return $self->_retrieve($req, $req->start_index, $req->row_limit); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub retrieve_paged { | ||
| 158 | my ($self, $req) = @_; | ||
| 159 | |||
| 160 | my $start_index = $req->start_index; | ||
| 161 | $start_index = 1 if !defined($start_index); | ||
| 162 | my $remaining_items = $req->row_limit; | ||
| 163 | my $max_items_per_page = 10_000; | ||
| 164 | my $res; | ||
| 165 | |||
| 166 | while (!defined($remaining_items) || $remaining_items > 0) { | ||
| 167 | my $row_limit = | ||
| 168 | defined($remaining_items) && | ||
| 169 | $remaining_items < $max_items_per_page ? | ||
| 170 | $remaining_items : $max_items_per_page; | ||
| 171 | |||
| 172 | my $page = $self->_retrieve($req, $start_index, $row_limit); | ||
| 173 | return $page if !$page->is_success; | ||
| 174 | |||
| 175 | if (!defined($res)) { | ||
| 176 | $res = $page; | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | push(@{ $res->rows }, @{ $page->rows }); | ||
| 180 | } | ||
| 181 | |||
| 182 | my $num_items = @{ $page->rows }; | ||
| 183 | last if $num_items < $row_limit; | ||
| 184 | |||
| 185 | $remaining_items -= $num_items if defined($remaining_items); | ||
| 186 | $start_index += $num_items; | ||
| 187 | } | ||
| 188 | |||
| 189 | my $total_items = @{ $res->rows }; | ||
| 190 | $res->items_per_page($total_items); | ||
| 191 | # The total result count of the first page isn't always accurate | ||
| 192 | $res->total_results($total_items); | ||
| 193 | |||
| 194 | return $res; | ||
| 195 | } | ||
| 196 | |||
| 197 | 1; | ||
| 198 | |||
| 199 | __END__ |