3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-08 13:09:34 +01:00
pbot/modules/g.pl
2016-08-01 01:46:17 -07:00

48 lines
849 B
Perl
Executable File

#!/usr/bin/perl
use warnings;
use strict;
use WWW::Google::CustomSearch;
use HTML::Entities;
my $api_key = '';
my $cx = '';
my ($nick, $arguments, $matches);
$matches = 3;
$nick = shift @ARGV;
if ($#ARGV < 0) {
print "Usage: google [number of results] query\n";
exit;
}
$arguments = join ' ', @ARGV;
if($arguments =~ s/^([0-9]+)//) {
$matches = $1;
}
my $engine = WWW::Google::CustomSearch->new(api_key => $api_key, cx => $cx, quotaUser => $nick);
my $result = $engine->search($arguments);
print "$nick: ";
print '(', $result->formattedTotalResults, " results)\n";
if (not @{$result->items}) {
print "No results found\n";
exit;
}
my $comma = "";
foreach my $item (@{$result->items}) {
print $comma, decode_entities $item->title, ': <', $item->link, ">\n";
$comma = " -- ";
last if --$matches <= 0;
}
print "\n";