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