From afeb7f327c88ab166f656ce59839ed18e61eb1c6 Mon Sep 17 00:00:00 2001 From: Brian West Date: Thu, 31 May 2012 14:18:58 -0500 Subject: [PATCH] OpenCNAM frontend so you can feed it 10 or 11 digits and cache the results. --- scripts/perl/cnam.cgi | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 scripts/perl/cnam.cgi diff --git a/scripts/perl/cnam.cgi b/scripts/perl/cnam.cgi new file mode 100755 index 0000000000..d7acaa9b49 --- /dev/null +++ b/scripts/perl/cnam.cgi @@ -0,0 +1,63 @@ +#!/usr/bin/perl +# +# OpenCNAM front end because they only take 10 digits and can't filter 11 on their side. +# + +use Data::Dumper; +use CGI qw/:standard/; +use LWP::UserAgent; +use SDBM_File; +use Fcntl; + +my %params = map { $_ => get_data( $_ ) } param; + +$ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 }); + +sub get_data { + my $name = shift; + my @values = param( $name ); + return @values > 1 + ? \@values + : $values[0]; +} + +print "Content-Type: text/plain\n\n"; + +tie (my %cache, 'SDBM_File', 'cnam.dbm', O_RDWR|O_CREAT, 0640) || die $!; + +my $number = $params{number}; + + +if($number =~ m/1?\d{10}/) { + + + if($number =~ m/^1(\d{10})$/) { + $number = $1; + } + + if($cache{"$number"}) { + print $cache{"$number"}; + untie %cache; + exit; + } + + my $url = "https://api.opencnam.com/v1/phone/$number?format=text"; + + + my $res = $ua->get( $url ); + + if ($res->is_success) { + my $content = $res->decoded_content; + if ($content =~ m/^Invalid/) { + # API shouldn't return this crap. + print "UNKNOWN"; + } else { + # Cache the entry. + $cache{"$number"} = $content; + # print the entry. + print $content; + } + } +} + +untie %cache;