3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 22:09:26 +01:00
pbot/modules/g.pl

48 lines
849 B
Perl
Raw Normal View History

#!/usr/bin/perl
2011-12-06 17:46:39 +01:00
use warnings;
use strict;
2016-08-01 10:46:17 +02:00
use WWW::Google::CustomSearch;
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";