#!/usr/bin/perl -w

# property search toy
# Waider, January 2001

use LWP::UserAgent;
use URI::Escape;

$| = 1;
my $debug = 0;

$ua = new LWP::UserAgent;
$ua->agent( "GeekToy/0.1 " . $ua->agent );
$ua->env_proxy();

# Need an EstateAgent object, supporting Search( location, type, price
# ).  Should return an object which contains a short description
# (location, type, price) plus a hook back to the original site/more
# detail page.

$price = $ARGV[0] || 150000;
sherry_fitz( $price );
#propertypartners( 150000 );

# Sherry Fitzgerald
sub sherry_fitz {
  my ( $pr ) = shift;
  # Bits: [must get the rest of these]
  # bcode=0, for 'any branch'
  # price=150000 for up to 150,000
  # tcode=1 for House
  # bedrooms=0 for any number of bedrooms
  # n2show=36 for number of properties to show
  # disact=show
  # acode=9
  # rcode=1
  # frow=1
  # submit=submit

  # if disact = next, then that's for the next bunch of pages.

  my $base = "http://www.sherryfitz.ie/htm/res_prop/";
  my $req = new HTTP::Request
	GET => $base . "display_results.whtm?rcode=1&acode=9&bcode=0&price=$ {pr}&tcode=1&bedrooms=0&n2show=36&disact=show&frow=1&submit=submit";

  my $res = $ua->request( $req );

  # Check the outcome of the response
  $res->is_success || die $res->as_string;
  print "Got initial list\n" if $debug;

  # Yay!
  my $page = $res->content;
  print $page if $debug == 2;
  $page =~ s/^.*?(\<form)/$1/si;
  @forms = split( /\<\/form\>/, $page );

  pop @forms;					# lose page trailer

  my %seen;
  my $props = $#forms;
  for my $house ( @forms ) {
	# Fetch the house details
	#<form action='display_details.whtm' method=post>
	#<input type=hidden name='price' value='150000'>
	#<input type=hidden name='bedrooms' value='0'>
	#<input type=hidden name='lcode' value=''>
	#<input type=hidden name='tcode' value='1'>
	#<input type=hidden name='n2show' value='36'>
	#<input type=hidden name='disact' value='show'>
	#<input type=hidden name='frow' value='1'>
	#<input type=hidden name='prop' value='SRP01007'>
	#<input type=hidden name='acode' value='9'><input type=hidden name='rcode' value='1'><input type=hidden name='plist' value='x:SRP01007:SRP00959:SEP00548:SWP00376:SWP00386:SWP00382:SWP00392:SLP00656:SLP00685:SCP00150:SCP00113:SCP00151:SLP00652:SBP00646:SLP00626:SLP00678:SLP00688:SSP00605:x'>
	#<input type=hidden name='not_from_mail' value='1'><input type=image name=submit src='../../himgs/sr001007_t.jpg' width='120' height='90' border=0>
	#<br>

	@inputs = split(/\<[iI][nN][pP][uU][tT]\s+/, $house);
	my $propid;

	# get form name
	my $frm = shift @inputs;
	if ( $frm =~ /display_details/ ) {
	  $frm = "display_details.whtm";
	} else {
	  $frm = "display_results.whtm";
	}

	# fixme could snag "method" too
	$req = new HTTP::Request POST => "$ {base}$ {frm}";
	$req->content_type( 'application/x-www-form-urlencoded' );
	print "Using $base$frm\n" if $debug;

	for my $i ( @inputs ) {
	  my ( %bits );
	  chomp( $i );
	  $i =~ s/\>\s*//;

	  # parse out the bits. I should use HTML::Parse, or something, sure.
	  while ( $i =~ m/^(\w+)(=(['"])?(.*?)\3?)(\s+(.*))?$/ ) {
		my $val = $4; $val ||= $1; $val =~ s/\"/\\\"/;
		eval '$bits{\'' . $1 . "'}=\"$4\";";
		$i = $6 || "";
	  }

	  if ( $bits{'type'} eq "image" ) {
		$bits{'name'} = $bits{'name'} . ".x";
		$bits{'value'} = 5;
	  }

	  $bits{'value'} = uri_escape( $bits{'value'});

	  if ( $frm =~ 'display_results' ) {
		$req->add_content( $bits{'name'} . "=" . $bits{'value'} . "&" );
		$propid = "next page";
	  } else {
		if ( $bits{ 'name' } eq "prop" ) { # after all that...
		  $req->content( $bits{'name'} . "=" . $bits{'value'} );
		  $propid = $bits{'value'}; # should use unescaped value?
		}
	  }
	}

	next if $req->content =~ /disact=prev/;
	next if defined( $seen{$propid});

	print "Requesting $propid..." if $debug;
	$res = $ua->request( $req );
	$res->is_success || die $res->as_string;

	# Parse the result
	print "got it.\n" if $debug;
	$page = $res->content;
	if ( $propid eq "next page" ) {
	  print $req->as_string if $debug == 2;
	  print $page if $debug == 2;
	  $page =~ s/^.*?(\<form)/$1/si;
	  my @newforms = split( /\<\/form\>/, $page );

	  pop @newforms;					# lose page trailer
	  push @forms, @newforms;
	} else {
	  $seen{ $propid } = 1;
	  $page =~ s/^.*?\<table//;
	  $page =~ s/^.*?\<table[^>]+\>//; # we want the second table.
	  $page =~ s|\</table\>.*$||; # and discard the remainder.

	  # from what's left:
	  $page =~ s/(\<[^>]+\>\s*)+/\n/g;
	  my @lines = split( /\n/, $page );
	  while ( $lines[ 0 ] =~ /^\s*$/ ) {
		shift @lines;
	  }

	  my $address = $lines[ 0 ];
	  my $price = "";
	  for my $l ( @lines ) {		# argh. this could break so easily.
	  $price = $l if $l =~ "^IRL";
	}

	  if ( !$price ) {
		$price =  "[can't parse out price]";
	  }

	  print $price, " $address ($propid)\n";
	}
  }
}

# propertypartners.ie: http://www.propertypartners.ie/results.asp?PropClass=&location=10&PriceRange=150000&PropertyType=House

sub propertypartners {
  my $pr = shift;

  my $base = "http://www.propertypartners.ie/";
  my $req = new HTTP::Request
	GET => $base . "results.asp?PropClass=&location=10&PriceRange=$pr&PropertyTpe=House";

  my $res = $ua->request( $req );
  $res->is_success || die $res->as_string;
  print "Got initial list\n" if $debug;

  my $page = $res->content;
  print $page, "\n";
}

# Gunne: http://www.gunne.ie/residential/search_purchase.cfm
sub gunne {
  my $pr = shift;
}
