Thursday, 21 February 2008

Oodle API with Perl and Frontier::Client

Perl client code for using Oodle API



I've been disappointed that no-one has seemingly published a Perl implementation of the Oodle API. Here is one way to do it:



Requires Frontier::Client and Data::Dumper modules





#!/usr/bin/perl

# Tim Gibbon 2008
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.


use strict;
use warnings;
use diagnostics;
use Frontier::Client;
use Data::Dumper;

my $lowhigh={ low=>'1000',
high=>'22000' };
my %prices= ('params'=> $lowhigh);

my $type = {'type' => 'price',
%prices
};
my @filters_price=( $type );


my %calling_params;
$calling_params{'partner_id'}="YOURAPIKEY";
$calling_params{'q'}="toyota prius";
$calling_params{'category'}="vehicles";
$calling_params{'region'}="london";
$calling_params{'filters'}=\@filters_price;

#Useful for debugging input params
#$Data::Dumper::Purity=1;
#print Dumper(%calling_params);


#The following program shows how to call an XML-RPC server from Perl:

# Make an object to represent the XML-RPC server.
my $server_url = 'http://api.oodle.com/api/';
my $server = Frontier::Client->new(url => $server_url
);

# Call the remote server and get our result.
my $result = $server->call('get', \%calling_params);
my $sum = $result->{'total'};
my $items = $result->{'items'};
my $item_number=0;


foreach my $record (@$items) {
$item_number++;
print ("######ITEM NUMBER $item_number\n");
my $mpg=$record->{'milespergallon'} if $record->{'milespergallon'};
my $url=$record->{'url'} if $record->{'url'};
my $shortbody=$record->{'body'} if $record->{'body'};
my $longbody=$record->{'sbody'} if $record->{'sbody'};
my $price=$record->{'price'} if $record->{'price'};


print ("PRICE \t $price \n") if $price;
# print ("URL \t $url \n") if $url;
# print ("SHORT \t $shortbody \n") if $shortbody;
# print ("LONG \t $longbody \n") if $longbody;
}

###################USE FOR DEBUG################################
#$Data::Dumper::Purity=1;
##$Data::Dumper::Pad="####PAD####";
#$Data::Dumper::Sortkeys="1";
##$Data::Dumper::Varname="#VAR#";
#print Dumper($items);
###################USE FOR DEBUG################################

1 comment:

Anonymous said...
This comment has been removed by a blog administrator.