2011-12-11 19:29:55 +01:00
|
|
|
#!/usr/bin/perl
|
2011-12-06 17:46:39 +01:00
|
|
|
|
2011-12-11 19:29:55 +01:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2016-08-01 10:46:17 +02:00
|
|
|
use WWW::Google::CustomSearch;
|
2012-01-05 12:04:55 +01:00
|
|
|
use HTML::Entities;
|
2011-12-06 17:46:39 +01:00
|
|
|
|
2016-08-01 10:46:17 +02:00
|
|
|
my $api_key = '';
|
|
|
|
my $cx = '';
|
|
|
|
|
2011-12-06 17:46:39 +01:00
|
|
|
my ($nick, $arguments, $matches);
|
|
|
|
|
|
|
|
$matches = 3;
|
|
|
|
$nick = shift @ARGV;
|
|
|
|
|
2016-08-01 10:46:17 +02:00
|
|
|
if ($#ARGV < 0) {
|
2011-12-06 17:46:39 +01:00
|
|
|
print "Usage: google [number of results] query\n";
|
2016-08-01 10:46:17 +02:00
|
|
|
exit;
|
2011-12-06 17:46:39 +01:00
|
|
|
}
|
|
|
|
|
2016-08-01 10:46:17 +02:00
|
|
|
$arguments = join ' ', @ARGV;
|
2011-12-06 17:46:39 +01:00
|
|
|
|
2016-08-01 10:46:17 +02:00
|
|
|
if($arguments =~ s/^([0-9]+)//) {
|
2011-12-06 17:46:39 +01:00
|
|
|
$matches = $1;
|
|
|
|
}
|
|
|
|
|
2016-08-01 10:46:17 +02:00
|
|
|
my $engine = WWW::Google::CustomSearch->new(api_key => $api_key, cx => $cx, quotaUser => $nick);
|
|
|
|
my $result = $engine->search($arguments);
|
2011-12-06 17:46:39 +01:00
|
|
|
|
|
|
|
print "$nick: ";
|
|
|
|
|
2016-08-01 10:46:17 +02:00
|
|
|
print '(', $result->formattedTotalResults, " results)\n";
|
|
|
|
|
|
|
|
if (not @{$result->items}) {
|
|
|
|
print "No results found\n";
|
|
|
|
exit;
|
2011-12-06 17:46:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $comma = "";
|
2016-08-01 10:46:17 +02:00
|
|
|
foreach my $item (@{$result->items}) {
|
|
|
|
print $comma, decode_entities $item->title, ': <', $item->link, ">\n";
|
|
|
|
$comma = " -- ";
|
|
|
|
last if --$matches <= 0;
|
2011-12-06 17:46:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
print "\n";
|