perlgoogle-apigoogle-search-api

Perl: WWW::Google::CustomSearch: Missing required arguments?


(2024-03-01 update: following question was misleeding. This bug was already reported to the maintainer and I found a workaround. Please see the comments)

I'm writting a script to run the Google Custom Search API to obtain the search results. When I run the following script, it succeeds to get the result.

#! /bin/perl

# minisearch.pl

use strict;
use warnings;
use WWW::Google::CustomSearch;
use Data::Dumper;
use feature qw(say);

my $engine  = WWW::Google::CustomSearch->new(
    api_key => 'my api key',
    cx => 'my search engine ID',
    dateRestrict => 'm3',
    #siteSearch => 'accesswire.com',
    #siteSearchFilter => 'i'
    );

my $result  = $engine->search('mobile+pc+linux');

exit unless defined $result;

say "kind: ", $result->kind;
say "totalResults: ", $result->totalResults;
say "formattedTotalResults: ", $result->formattedTotalResults;
say "formattedSearchTime: ", $result->formattedSearchTime;
say "searchTime: ", $result->searchTime;
say "url_template: ", $result->url_template;
say "url_type: ", $result->url_type;
say "";
say "request:";
say "REQUEST:\n", Dumper $result->request;
say "";
say ">>> 10 pages >>>\n";
for my $n (1..2) {
    say "=== PAGE $n ";
    say "ITEMS:\n", Dumper $result->items;
    say "";

    sleep 10;
    $result = $result->nextPage->fetch;
}

But when I use siteSearch and siteSearchFilter parameters to restrict the target sites, it fails.

my $engine  = WWW::Google::CustomSearch->new(
    api_key => 'my api key',
    cx => 'my search engine ID',
    dateRestrict => 'm3',
    siteSearch => 'accesswire.com',
    siteSearchFilter => 'i'
    );

The following error message is displayed.

$ ./miniSearch.pl Missing required arguments: totalResults at /usr/local/share/perl/5.30.0/WWW/Google/CustomSearch/Request.pm line 30.

I have added totalResults hash entry to new as below.

my $engine  = WWW::Google::CustomSearch->new(
    api_key => 'my api key',
    cx => 'my search engine ID',
    dateRestrict => 'm3',
    siteSearch => 'accesswire.com',
    siteSearchFilter => 'i',
    totalResults => 1000,
    );

or below.

my $engine  = WWW::Google::CustomSearch->new(
    api_key => 'my api key',
    cx => 'my search engine ID',
    dateRestrict => 'm3',
    siteSearch => 'accesswire.com',
    siteSearchFilter => 'i',
    totalResults => '1000',
    );

But the error remains the same.

Does anyone know how to resolve this?


Solution

  • Being the author of WWW::Google::CustomSearch, I just pushed the proposed fix to the related issue.

    https://rt.cpan.org/Ticket/Display.html?id=141609

    The v0.40 has the fix.

    https://metacpan.org/release/MANWAR/WWW-Google-CustomSearch-0.40