#!/usr/bin/perl # Reverse lookup US phone numbers using whitepages.com # Takes numbers on STDIN or from file # You will need to 'touch rlookup.cache' to get things started. # WHOS YOUR DADDY?? - MadCamel use LWP::Simple; $|++; load_rcache("rlookup.cache"); while () { chomp; $num = $_; $foo = rlookup($_); print("$num|$foo\n"); } sub rlookup { my $number = $_[0]; foreach (keys %rcache) { return("$rcache{$_} (c)") if ($_ == $number); } my $info = rlookup_remote($number); $rcache{$number} = $info; return($info); } sub rlookup_remote { my $content = get("http://www.whitepages.com/search/Reverse_Phone?phone=$_[0]"); return("Error") unless defined $content; $content =~ s/[\r\n\|]//g; $content =~ s/\s+/ /g; $_ = $content; # Case 1: Info fully available if (/City=(.+?)&State=(.+?)&/i) { my ($city, $state) = ($1, $2); my $first = $1 if (/_RM_HTML_FIRST_ESC_=([\w\d\%\s\,\"]+?)&/); my $last = $1 if (/_RM_HTML_LAST_ESC_=([\w\d\%\s\,\"]+?)&/); foreach(($city, $state, $first, $last)) { $_ = urldecode($_); } return("$first $last, $city, $state") if ($first); return("$last, $city, $state") if ($last); return("$city, $state"); } # Case 2: Location available if (/quot; is a (.+?) based/) { my $location = $1; my $carrier = $1 if (/carrier is (.+) However/); return("$location ($carrier)") if ($carrier); return("$location"); } # Case 3: Unknown return("Unknown"); } sub urldecode { $_[0] =~ s/%(..)/pack("c",hex($1))/eg; return($_[0]); } sub load_rcache { open(CACHE, $_[0]) or die("Can't load $_[0] $!\n"); while() { chomp; my ($num, $info) = split(/\|/, $_, 2); $rcache{$num} = $info; } close(CACHE); } sub save_rcache { open(CACHE, ">$_[0]") or die("Can't save $_[0]: $!\n"); foreach (keys %rcache) { print CACHE ("$_|$rcache{$_}\n"); } close(CACHE); } END { save_rcache("rlookup.cache"); }