Friday, 30 May 2008

Oodle API, Perl and Postcode searches

Thanks to Steve Baker on the Oodle API discussion group, I can now perform Oodle API searches against UK postcodes.



Here is one way to do it




#!/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 $location="uk";


##################################LOW/HIGH FILTER####################
my $lowhigh={ low=>'100000',
high=>'300000' };
my %prices= ('params'=> $lowhigh);

my $price_type = {'type' => 'price',
%prices
};
my @filters_price=( $price_type );
##################################LOW/HIGH FILTER#######################
######################BEDROOMS DIMENSION ISN'T GREAT - HASHED OUT BELOW#
########################################################################


##################################DISTANCE FILTER####################
# Postcode search DOES work for UK Postcodes
my $distance_params= {
value => 3,
units => 'mi',
zip => 'NG7 2RD',
country_code => 'GBR'
};
my %distances=('params'=>$distance_params);
my $distance_type = {'type' => 'distance',
%distances
};
my @filters_distance = ( $distance_type );
#
#
##################################DISTANCE FILTER####################
my @allfilters=(@filters_price, @filters_distance);


my %calling_params;
$calling_params{'partner_id'}="YOURAPIKEY";
$calling_params{'q'}="toyota prius";
$calling_params{'category'}="vehicles";
$calling_params{'region'}="uk";
$calling_params{'from'}=0;
$calling_params{'to'}=10;
# $calling_params{'dimensions'}=\@dimensions;
# $calling_params{'filters'}=\@filters_price;
$calling_params{'filters'}=\@allfilters;

$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;




my @rows;
foreach my $record (@$items) {

my $item_row;

$item_number++;
print ("######ITEM NUMBER $item_number\n");
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 ("URL: $url DESC: $shortbody LONGDESC: $longbody \n PRICE $price\n\n");
print ("#"x80,"\n");
}

print ("OK we found $sum records\n");

###################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:

Steve said...

Hey, tng -- Steve Baker here from the Engineering team at Oodle!

While doing some Google searches, I came across your post here about doing UK postcode searches on the Oodle API.

Just a small heads up that we've recently realeased a new version of the Oodle API (think of it as "Oodle API Version 2").

This new version does not use XML-RPC. The XML-RPC API is not going away anytime soon, but the new version is much much easier to use.

In the example in your blog post here about UK postcodes, you show how to construct the filters and use the country code to make sure Oodle understands that you're sending a UK postal code, not a Canadian or American postal code.

Fantastic work on demonstrating the example. But in the new API, it's *so* much more simple. It's all specified on a URL, so users like you don't have to fight to get it right. :-)

For example, say you want to query the Oodle API for the following:

* Cars for Sale
* Between 25000 & 50000 GBP
* Within 3 miles ...
* Of NG7 2RD

In the new API, simply visit this URL:

http://api.oodle.com/api/v2/listings?key=TEST&region=uk&location=NG7+2RD&radius=3&attributes=price_25000_50000&category=vehicle/car

That's it, you're done! Give it a shot! Heck, try visiting the URL right now. :-)

The XML data that comes back to you from the new API is structured a bit differently than the XML-RPC data you're used to. But all of the elemental pieces -- category, source, price, etc. -- are all still there.

Most importantly, though, all of the filters, the price setting, all of that old complex XML-RPC Oodle API stuff that you talked about in your blog post can now done on a URL.

For more info, visit the Oodle API version 2 documentation here:

http://developer.oodle.com/listings

Thanks for your fantastic support of the Oodle API!