2011-12-11 19:29:55 +01:00
|
|
|
#!/usr/bin/perl
|
2011-12-06 17:46:39 +01:00
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
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
|
|
|
|
2019-07-11 03:46:34 +02:00
|
|
|
my $api_key = '';
|
|
|
|
my $cx = '';
|
2016-08-01 10:46:17 +02:00
|
|
|
|
2011-12-06 17:46:39 +01:00
|
|
|
my ($nick, $arguments, $matches);
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
$matches = 1;
|
2011-12-06 17:46:39 +01:00
|
|
|
$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
|
|
|
|
2019-05-28 18:19:42 +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);
|
2011-12-06 17:46:39 +01:00
|
|
|
|
|
|
|
print "$nick: ";
|
|
|
|
|
2017-09-28 07:44:44 +02:00
|
|
|
if ($arguments =~ m/(.*)\svs\s(.*)/i) {
|
|
|
|
my ($a, $b) = ($1, $2);
|
2018-04-24 21:42:53 +02:00
|
|
|
my $result1 = $engine->search("\"$a\" -\"$b\"");
|
|
|
|
my $result2 = $engine->search("\"$b\" -\"$a\"");
|
2017-09-28 07:44:44 +02:00
|
|
|
|
2018-01-06 04:45:28 +01:00
|
|
|
if (not defined $result1 or not defined $result1->items or not @{$result1->items}) {
|
2017-09-28 07:44:44 +02:00
|
|
|
print "No results for $a\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-01-06 04:45:28 +01:00
|
|
|
if (not defined $result2 or not defined $result2->items or not @{$result2->items}) {
|
2017-09-28 07:44:44 +02:00
|
|
|
print "No results for $b\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
print "$a: (", $result1->formattedTotalResults, ") ", decode_entities $result1->items->[0]->title, " <", $result1->items->[0]->link, "> VS $b: (", $result2->formattedTotalResults, ") ", decode_entities $result2->items->[0]->title, " <", $result2->items->[0]->link, ">\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $result = $engine->search($arguments);
|
2016-08-01 10:46:17 +02:00
|
|
|
|
2018-01-06 04:41:01 +01:00
|
|
|
if (not defined $result or not defined $result->items or not @{$result->items}) {
|
2016-08-01 10:46:17 +02:00
|
|
|
print "No results found\n";
|
|
|
|
exit;
|
2011-12-06 17:46:39 +01:00
|
|
|
}
|
|
|
|
|
2017-09-28 07:44:44 +02:00
|
|
|
print '(', $result->formattedTotalResults, " results)\n";
|
|
|
|
|
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";
|