2009-12-09 02:08:12 +01:00
|
|
|
#!/usr/bin/perl -w -I /home/msmud/lib/lib/perl5/site_perl/5.10.0/
|
2007-05-20 22:44:44 +02: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/.
|
|
|
|
|
2007-05-20 22:44:44 +02:00
|
|
|
# Quick and dirty by :pragma
|
|
|
|
|
|
|
|
use LWP::UserAgent;
|
|
|
|
|
|
|
|
my ($text, $arguments, $header, $footer, $t, $matches);
|
|
|
|
|
|
|
|
$header = "";
|
|
|
|
$footer = undef;
|
|
|
|
$matches = 1;
|
|
|
|
|
|
|
|
if ($#ARGV < 0)
|
|
|
|
{
|
2012-08-24 00:50:07 +02:00
|
|
|
print "Usage: google [number of results] query\n";
|
2007-05-20 22:44:44 +02:00
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
$arguments = join("+", @ARGV);
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($arguments =~ m/([0-9]+)\+/)
|
2007-05-20 22:44:44 +02:00
|
|
|
{
|
|
|
|
$matches = $1;
|
|
|
|
$arguments =~ s/$1//;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new;
|
|
|
|
$ua->agent("Mozilla/5.0");
|
|
|
|
|
|
|
|
my $response = $ua->get("http://www.google.com/search?q=$arguments");
|
|
|
|
|
|
|
|
if (not $response->is_success)
|
|
|
|
{
|
|
|
|
print "Couldn't get google information.\n";
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = $response->content;
|
|
|
|
|
|
|
|
$arguments =~ s/\+/ /g;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($text =~ m/No pages were found/)
|
2007-05-20 22:44:44 +02:00
|
|
|
{
|
|
|
|
print "No results found for '$arguments'.\n";
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($text =~ m/Results/g)
|
2007-05-20 22:44:44 +02:00
|
|
|
{
|
|
|
|
$text =~ m/1<\/b> - .*?<\/b> of (about )?<b>(.*?)<\/b>/g;
|
|
|
|
$header = $2;
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($text =~ m/Did you mean\:/g)
|
2007-05-20 22:44:44 +02:00
|
|
|
{
|
|
|
|
$text =~ m/<i>(.*?)<\/i>/g;
|
|
|
|
$footer = "Alternatively, try '$1' for more results.";
|
|
|
|
}
|
|
|
|
|
|
|
|
print "$arguments ($header): ";
|
|
|
|
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($text =~ m/Showing web page information/g)
|
2007-05-20 22:44:44 +02:00
|
|
|
{
|
|
|
|
$text =~ m/<p class=g>(.*?)<br>/g;
|
|
|
|
$header = $1;
|
|
|
|
$header =~ s/<.*?>//g;
|
|
|
|
print "$header";
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($text =~ m/Description:(.*?)<br>/)
|
2007-05-20 22:44:44 +02:00
|
|
|
{
|
|
|
|
$header = $1;
|
|
|
|
$header =~ s/<.*?>//g;
|
|
|
|
print " - $header\n";
|
|
|
|
}
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
$matches = 5 if ($matches > 5);
|
2007-05-20 22:44:44 +02:00
|
|
|
|
|
|
|
my $i = 0;
|
|
|
|
|
|
|
|
my $quote = chr(226) . chr(128) . chr(156);
|
|
|
|
my $quote2 = chr(226) . chr(128) . chr(157);
|
|
|
|
my $dash = chr(226) . chr(128) . chr(147);
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
while ($text =~ m/<li class=g><h3 class=r><a href=\"(.*?)\".*?>(.*?)<\/a>/g && $i < $matches)
|
2007-05-20 22:44:44 +02:00
|
|
|
{
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($i > 0)
|
2007-05-20 22:44:44 +02:00
|
|
|
{
|
|
|
|
$t = ", $2: [$1]";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$t = "$2: [$1]";
|
|
|
|
}
|
2009-12-09 02:08:12 +01:00
|
|
|
$t =~ s/<[^>]+>//g;
|
|
|
|
$t =~ s/<\/[^>]+>//g;
|
2007-05-20 22:44:44 +02:00
|
|
|
$t =~ s/$quote/"/g;
|
|
|
|
$t =~ s/$quote2/"/g;
|
|
|
|
$t =~ s/$dash/-/g;
|
2009-12-09 02:08:12 +01:00
|
|
|
$t =~ s/"/"/g;
|
|
|
|
$t =~ s/&/&/g;
|
|
|
|
$t =~ s/&nsb;/ /g;
|
2007-05-20 22:44:44 +02:00
|
|
|
$t =~ s/'/'/g;
|
|
|
|
$t =~ s/</</g;
|
|
|
|
$t =~ s/>/>/g;
|
2009-12-09 02:08:12 +01:00
|
|
|
$t =~ s/<em>//g;
|
|
|
|
$t =~ s/<\/em>//g;
|
2007-05-20 22:44:44 +02:00
|
|
|
print $t;
|
|
|
|
$i++;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
#while ($t =~ m/(.)/g)
|
2007-05-20 22:44:44 +02:00
|
|
|
#{
|
|
|
|
# print "($1) = " . ord($1). "\n";
|
|
|
|
#}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
print " - $footer\n" if defined $footer;
|