Skip to content

Instantly share code, notes, and snippets.

@dpk dpk/gist:619192
Created Oct 10, 2010

Embed
What would you like to do?
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use JSON;
use Getopt::Long;
use URI::Escape qw( uri_escape_utf8 );
# options
my $query;
my $titles;
my $count = 8;
GetOptions("query|q=s" => \$query,
"title|t" => \$titles,
"count|c=i" => \$count);
if (!$query) {
print STDERR $0.": No query given. Use the --query (-q) option.\n";
exit 0;
}
my $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".uri_escape_utf8($query)."&rsz=".$count;
my $ua = LWP::UserAgent->new();
my $body = $ua->get($url);
# process the json string
my $json = from_json($body->decoded_content);
# have some fun with the results
my $i = 0;
foreach my $result (@{$json->{responseData}->{results}}){
$i++;
print $result->{url};
if ($titles) {
print "\t".$result->{titleNoFormatting};
}
print "\n";
}
if (!$i) {
print STDERR $0.": no results for query ".$query."\n";
exit 1;
}
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.