#!/usr/bin/perl binmode(STDOUT, ":utf8"); use LWP::UserAgent; use HTML::Entities; use Encode; sub yplookup { my($tel); ($tel) = @_; $ua = LWP::UserAgent->new; $ua->agent("tel2name/1.0 "); $res = $ua->request(HTTP::Request->new("GET", "http://www.eniro.se/query?what=yp&search_word=$tel")); return undef unless $res->is_success; $html = $res->content; match: while($html =~ /\s*([^<]*[^\s<])\s*<\/span>/ig) { $match = decode_entities($1); for $prev (@matches) { next match if $prev eq $match; } push @matches, $match; } return @matches; } sub wplookup { my($tel); ($tel) = @_; $ua = LWP::UserAgent->new; $ua->agent("tel2name/1.0 "); $res = $ua->request(HTTP::Request->new("GET", "http://www.eniro.se/query?what=wp&phone_number=$tel")); return undef unless $res->is_success; $html = $res->content; match: while($html =~ /]*>\s*\\s*([^<]*[^\s<])\s*<\/span>/ig) { $match = decode_entities($1); for $prev (@matches) { next match if $prev eq $match; } push @matches, $match; } return @matches; } $tel = $ARGV[0]; if(open NT, $ENV{"HOME"} . "/phone/nametab") { while() { if(/$tel\s(.*)$/) { print "$1\n"; exit 0; } } close NT; } $yppid = open YP, "-|"; if($yppid == 0) { $mod = 0; bt: while(1) { if(@matches = yplookup $tel) { for $match (@matches) { print $match; print " ($tel)" if $mod; print "\n"; } last bt; } last bt if !($tel =~ /(.+)[^0](0*)$/); $tel = $1 . "0" . $2; $mod = 1; } exit 0; } $wppid = open WP, "-|"; if($wppid == 0) { for $match (wplookup $tel) { print "$match\n"; } exit 0; } binmode(YP, ":utf8"); binmode(WP, ":utf8"); match: while() { chomp; for $prev (@matches) { next match if (lc $prev) eq (lc $_); } print "$_\n"; flush; push @matches, $_; } match: while() { chomp; for $prev (@matches) { next match if (lc $prev) eq (lc $_); } print "$_\n"; flush; push @matches, $_; }