3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-20 02:49:49 +01:00

GoogleSearch: fix usage and improve stuff

This commit is contained in:
Pragmatic Software 2021-08-28 09:06:33 -07:00
parent 4096510d05
commit aaad36f99c
2 changed files with 18 additions and 15 deletions

View File

@ -36,11 +36,13 @@ sub unload {
sub cmd_googlesearch { sub cmd_googlesearch {
my ($self, $context) = @_; my ($self, $context) = @_;
return "Usage: google [number of results] query\n" if not length $context->{arguments}; return "Usage: google [-n <number of results>] query\n" if not length $context->{arguments};
my $matches = 1; my $matches = 1;
$matches = $1 if $context->{arguments} =~ s/^-n\s+([0-9]+)\s*//; $matches = $1 if $context->{arguments} =~ s/^-n\s+([0-9]+)\s*//;
$matches = 10 if $matches > 10;
my $api_key = $self->{pbot}->{registry}->get_value('googlesearch', 'api_key'); # https://developers.google.com/custom-search/v1/overview my $api_key = $self->{pbot}->{registry}->get_value('googlesearch', 'api_key'); # https://developers.google.com/custom-search/v1/overview
my $cx = $self->{pbot}->{registry}->get_value('googlesearch', 'context'); # https://cse.google.com/all my $cx = $self->{pbot}->{registry}->get_value('googlesearch', 'context'); # https://cse.google.com/all
@ -52,6 +54,7 @@ sub cmd_googlesearch {
my $engine = WWW::Google::CustomSearch->new(api_key => $api_key, cx => $cx, quotaUser => $context->{hostmask}); my $engine = WWW::Google::CustomSearch->new(api_key => $api_key, cx => $cx, quotaUser => $context->{hostmask});
# versus/fight mode: !google banana vs apple -- returns number of results for both terms.
if ($context->{arguments} =~ m/(.*)\s+vs\s+(.*)/i) { if ($context->{arguments} =~ m/(.*)\s+vs\s+(.*)/i) {
my ($a, $b) = ($1, $2); my ($a, $b) = ($1, $2);
my $result1 = $engine->search("\"$a\" -\"$b\""); my $result1 = $engine->search("\"$a\" -\"$b\"");
@ -67,15 +70,12 @@ sub cmd_googlesearch {
utf8::decode $title1; utf8::decode $title1;
utf8::decode $title2; utf8::decode $title2;
return return "$context->{nick}: "
"$context->{nick}: $a: (" . "$a: (" . $result1->formattedTotalResults . ') '
. $result1->formattedTotalResults . ") " . decode_entities($title1) . ' <' . $result1->items->[0]->link . '> '
. decode_entities($title1) . " <" . 'VS '
. $result1->items->[0]->link . "$b: (" . $result2->formattedTotalResults . ') '
. "> VS $b: (" . decode_entities($title2) . ' <' . $result2->items->[0]->link . '>';
. $result2->formattedTotalResults . ") "
. decode_entities($title2) . " <"
. $result2->items->[0]->link . ">";
} }
my $result = eval { $engine->search($context->{arguments}) }; my $result = eval { $engine->search($context->{arguments}) };
@ -92,14 +92,17 @@ sub cmd_googlesearch {
my $output = "$context->{nick}: (" . $result->formattedTotalResults . " results) "; my $output = "$context->{nick}: (" . $result->formattedTotalResults . " results) ";
my $comma = ""; my @results;
foreach my $item (@{$result->items}) { foreach my $item (@{$result->items}) {
my $title = $item->title; my $title = $item->title;
utf8::decode $title; utf8::decode $title;
$output .= $comma . decode_entities($title) . ': <' . $item->link . ">"; push @results, decode_entities($title) . ': <' . $item->link . '>';
$comma = " -- ";
last if --$matches <= 0; last if --$matches <= 0;
} }
$output = join "\n-- ", @results;
return $output; return $output;
} }

View File

@ -25,8 +25,8 @@ use PBot::Imports;
# These are set by the /misc/update_version script # These are set by the /misc/update_version script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 4372, BUILD_REVISION => 4373,
BUILD_DATE => "2021-08-27", BUILD_DATE => "2021-08-28",
}; };
sub initialize {} sub initialize {}