Make rbtadd work outside of the LAN.
[utils.git] / tel2name
CommitLineData
178e2421 1#!/usr/bin/perl
2
3binmode(STDOUT, ":utf8");
4
5use LWP::UserAgent;
6use HTML::Entities;
7use Encode;
8
9sub 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
33sub 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
59if(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, "-|";
70if($yppid == 0) {
71 yplookup $tel;
72 exit 0;
73}
74$wppid = open WP, "-|";
75if($wppid == 0) {
76 wplookup $tel;
77 exit 0;
78}
79binmode(YP, ":utf8");
80binmode(WP, ":utf8");
81
82match: while(<YP>) {
83 chomp;
84 for $prev (@matches) {
85 next match if $prev eq $_;
86 }
87 push @matches, $_;
88}
89match: while(<WP>) {
90 chomp;
91 for $prev (@matches) {
92 next match if $prev eq $_;
93 }
94 push @matches, $_;
95}
96
97for $match (@matches) {
98 print "$match\n";
99}