Added CPU loading utility.
[utils.git] / tel2name
... / ...
CommitLineData
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 return @matches;
29}
30
31sub wplookup {
32 my($tel);
33 ($tel) = @_;
34
35 $ua = LWP::UserAgent->new;
36 $ua->agent("tel2name/1.0 ");
37 $res = $ua->request(HTTP::Request->new("GET", "http://www.eniro.se/query?what=wp&phone_number=$tel"));
38
39 return undef unless $res->is_success;
40
41 $html = $res->content;
42
43 match: while($html =~ /<a\s+class\s*=\s*"fn expand"[^>]*>\s*\<span\>\s*([^<]*[^\s<])\s*<\/span>/ig) {
44 $match = decode_entities($1);
45 for $prev (@matches) {
46 next match if $prev eq $match;
47 }
48 push @matches, $match;
49 }
50 return @matches;
51}
52
53$tel = $ARGV[0];
54
55if(open NT, $ENV{"HOME"} . "/phone/nametab") {
56 while(<NT>) {
57 if(/$tel\s(.*)$/) {
58 print "$1\n";
59 exit 0;
60 }
61 }
62 close NT;
63}
64
65$yppid = open YP, "-|";
66if($yppid == 0) {
67 $mod = 0;
68 bt: while(1) {
69 if(@matches = yplookup $tel) {
70 for $match (@matches) {
71 print $match;
72 print " ($tel)" if $mod;
73 print "\n";
74 }
75 last bt;
76 }
77 last bt if !($tel =~ /(.+)[^0](0*)$/);
78 $tel = $1 . "0" . $2;
79 $mod = 1;
80 }
81 exit 0;
82}
83$wppid = open WP, "-|";
84if($wppid == 0) {
85 for $match (wplookup $tel) {
86 print "$match\n";
87 }
88 exit 0;
89}
90binmode(YP, ":utf8");
91binmode(WP, ":utf8");
92
93match: while(<WP>) {
94 chomp;
95 for $prev (@matches) {
96 next match if (lc $prev) eq (lc $_);
97 }
98 print "$_\n";
99 flush;
100 push @matches, $_;
101}
102match: while(<YP>) {
103 chomp;
104 for $prev (@matches) {
105 next match if (lc $prev) eq (lc $_);
106 }
107 print "$_\n";
108 flush;
109 push @matches, $_;
110}