Make rbtadd work outside of the LAN.
[utils.git] / tel2name
1 #!/usr/bin/perl
2
3 binmode(STDOUT, ":utf8");
4
5 use LWP::UserAgent;
6 use HTML::Entities;
7 use Encode;
8
9 sub yplookup {
10     my($tel);
11     ($tel) = @_;
12     
13     $ua = LWP::UserAgent->new;
14     $ua->agent("tel2name/1.0 ");
15     $res = $ua->request(HTTP::Request->new("GET", "http://www.eniro.se/query?what=yp&search_word=$tel"));
16
17     return undef unless $res->is_success;
18     
19     $html = $res->content;
20     
21     match: while($html =~ /<span\s+class\s*=\s*"org fn"\s*>\s*([^<]*[^\s<])\s*<\/span>/ig) {
22         $match = decode_entities($1);
23         for $prev (@matches) {
24             next match if $prev eq $match;
25         }
26         push @matches, $match;
27     }
28     for $match (@matches) {
29         print "$match\n";
30     }
31 }
32
33 sub wplookup {
34     my($tel);
35     ($tel) = @_;
36     
37     $ua = LWP::UserAgent->new;
38     $ua->agent("tel2name/1.0 ");
39     $res = $ua->request(HTTP::Request->new("GET", "http://www.eniro.se/query?what=wp&phone_number=$tel"));
40
41     return undef unless $res->is_success;
42     
43     $html = $res->content;
44     
45     match: while($html =~ /<a\s+class\s*=\s*"fn expand"[^>]*>\s*\<span\>\s*([^<]*[^\s<])\s*<\/span>/ig) {
46         $match = decode_entities($1);
47         for $prev (@matches) {
48             next match if $prev eq $match;
49         }
50         push @matches, $match;
51     }
52     for $match (@matches) {
53         print "$match\n";
54     }
55 }
56
57 $tel = $ARGV[0];
58
59 if(open NT, $ENV{"HOME"} . "/phone/nametab") {
60     while(<NT>) {
61         if(/$tel\s(.*)$/) {
62             print "$1\n";
63             exit 0;
64         }
65     }
66     close NT;
67 }
68
69 $yppid = open YP, "-|";
70 if($yppid == 0) {
71     yplookup $tel;
72     exit 0;
73 }
74 $wppid = open WP, "-|";
75 if($wppid == 0) {
76     wplookup $tel;
77     exit 0;
78 }
79 binmode(YP, ":utf8");
80 binmode(WP, ":utf8");
81
82 match: while(<YP>) {
83     chomp;
84     for $prev (@matches) {
85         next match if $prev eq $_;
86     }
87     push @matches, $_;
88 }
89 match: while(<WP>) {
90     chomp;
91     for $prev (@matches) {
92         next match if $prev eq $_;
93     }
94     push @matches, $_;
95 }
96
97 for $match (@matches) {
98     print "$match\n";
99 }