Skip to content

Instantly share code, notes, and snippets.

@dpk
Created October 10, 2010 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpk/619192 to your computer and use it in GitHub Desktop.
Save dpk/619192 to your computer and use it in GitHub Desktop.
#!/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