Initial import.
authorfredrik <fredrik@959494ce-11ee-0310-bf91-de5d638817bd>
Wed, 31 Jan 2007 21:12:54 +0000 (21:12 +0000)
committerfredrik <fredrik@959494ce-11ee-0310-bf91-de5d638817bd>
Wed, 31 Jan 2007 21:12:54 +0000 (21:12 +0000)
git-svn-id: svn+ssh://svn.dolda2000.com/srv/svn/repos/src/utils@842 959494ce-11ee-0310-bf91-de5d638817bd

tel2name [new file with mode: 0755]

diff --git a/tel2name b/tel2name
new file mode 100755 (executable)
index 0000000..562b133
--- /dev/null
+++ b/tel2name
@@ -0,0 +1,99 @@
+#!/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 =~ /<span\s+class\s*=\s*"org fn"\s*>\s*([^<]*[^\s<])\s*<\/span>/ig) {
+       $match = decode_entities($1);
+       for $prev (@matches) {
+           next match if $prev eq $match;
+       }
+       push @matches, $match;
+    }
+    for $match (@matches) {
+       print "$match\n";
+    }
+}
+
+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 =~ /<a\s+class\s*=\s*"fn expand"[^>]*>\s*\<span\>\s*([^<]*[^\s<])\s*<\/span>/ig) {
+       $match = decode_entities($1);
+       for $prev (@matches) {
+           next match if $prev eq $match;
+       }
+       push @matches, $match;
+    }
+    for $match (@matches) {
+       print "$match\n";
+    }
+}
+
+$tel = $ARGV[0];
+
+if(open NT, $ENV{"HOME"} . "/phone/nametab") {
+    while(<NT>) {
+       if(/$tel\s(.*)$/) {
+           print "$1\n";
+           exit 0;
+       }
+    }
+    close NT;
+}
+
+$yppid = open YP, "-|";
+if($yppid == 0) {
+    yplookup $tel;
+    exit 0;
+}
+$wppid = open WP, "-|";
+if($wppid == 0) {
+    wplookup $tel;
+    exit 0;
+}
+binmode(YP, ":utf8");
+binmode(WP, ":utf8");
+
+match: while(<YP>) {
+    chomp;
+    for $prev (@matches) {
+       next match if $prev eq $_;
+    }
+    push @matches, $_;
+}
+match: while(<WP>) {
+    chomp;
+    for $prev (@matches) {
+       next match if $prev eq $_;
+    }
+    push @matches, $_;
+}
+
+for $match (@matches) {
+    print "$match\n";
+}