For a class I’m taking in technology entrepreneurship, I’m building a product that requires a large database of restaurants. Factual.com has one of the best restaurant databases I’ve found. The best part? 10,000 API calls per day for free. That’s pretty cool. You can also request downloads of their data for a reasonable fee.
Unfortunately, there’s no official Perl driver yet. And the code example they link to doesn’t seem to work.
The program below queries the Factual database given your public OAuth key, your secret OAuth key, a URL to query, and an output directory. I *think* this is the first complete program online that actually works for Factual – the rest seem to be focused on three-legged OAuth systems (which Factual is not). Factual also wants OAuth information sent through the authorization header, not encoded into the URL like many other services require.
use strict;
use Net::OAuth;
use HTTP::Request::Common;
use LWP::UserAgent;
$Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
die "Couldn't open or create output file\n" unless open (OUTPUTF, "+>", "/PATH/TO/OUTPUT/FILE.txt");
print OUTPUTF factual_query();
print "Successfully printed query\n";
close OUTPUTF;
sub factual_query {
my $url = "http://api.v3.factual.com/t/restaurants-us/"; #Or whatever you're querying
my $request = Net::OAuth->request('consumer')->new(
consumer_key => 'YOUR_FACTUAL_OAUTH_KEY',
consumer_secret => 'YOUR_FACTUAL_OAUTH_SECRET',
request_url => $url,
request_method => "GET",
signature_method => 'HMAC-SHA1',
timestamp => time(),
nonce => nonce(),
);
$request->sign;
my $req = HTTP::Request->new(GET => $url);
$req->header('Authorization' => $request->to_authorization_header);
my $ua = LWP::UserAgent->new;
my $response = $ua->simple_request($req);
return $response->as_string;
}
# Generates random numerical sequence
sub nonce {
my @a = (0..9);
my $nonce = '';
for(0..9) {
$nonce .= $a[rand(scalar(@a))];
}
return $nonce;
}
Very nicely written Perl code. Thank you very much, this will work nicely in my Moose package I’m working on.