blib/lib/WWW/WhoCallsMe.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 9 | 42 | 21.4 |
branch | 0 | 8 | 0.0 |
condition | n/a | ||
subroutine | 3 | 5 | 60.0 |
pod | 2 | 2 | 100.0 |
total | 14 | 57 | 24.5 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package WWW::WhoCallsMe; | ||||||
2 | |||||||
3 | 1 | 1 | 27990 | use strict; | |||
1 | 2 | ||||||
1 | 41 | ||||||
4 | 1 | 1 | 5 | use warnings; | |||
1 | 1 | ||||||
1 | 27 | ||||||
5 | 1 | 1 | 1113 | use LWP::UserAgent; | |||
1 | 50233 | ||||||
1 | 395 | ||||||
6 | |||||||
7 | our $VERSION = '0.02'; | ||||||
8 | |||||||
9 | =head1 NAME | ||||||
10 | |||||||
11 | WWW::WhoCallsMe - Query WhoCallsMe.com for details about a caller's phone number | ||||||
12 | |||||||
13 | =head1 SYNOPSIS | ||||||
14 | |||||||
15 | use WWW::WhoCallsMe; | ||||||
16 | my $who = WWW::WhoCallsMe->new; | ||||||
17 | |||||||
18 | my $number = '6305053008'; | ||||||
19 | my $calledme = $who->fetch($number); | ||||||
20 | if ($calledme->{listed}) | ||||||
21 | { | ||||||
22 | my $name = $calledme->{name}; | ||||||
23 | print "The number $number is listed. "; | ||||||
24 | print "It seems that $name was calling." if $name; | ||||||
25 | print "I don't know who was calling, though." unless $name; | ||||||
26 | print "\n"; | ||||||
27 | } | ||||||
28 | else | ||||||
29 | { | ||||||
30 | print "This number is not listed.\n"; | ||||||
31 | } | ||||||
32 | |||||||
33 | =head1 DESCRIPTION | ||||||
34 | |||||||
35 | WhoCallsMe.com is a website that compiles reports from users about | ||||||
36 | companies that call people. These callers might be telemarketers, | ||||||
37 | bill collectors, legit companies, or otherwise. These reports are | ||||||
38 | filed by the person that received the call. In some cases, the | ||||||
39 | report includes the name of the company that called. This module | ||||||
40 | attempts to grab this information and report it back to your program. | ||||||
41 | |||||||
42 | You supply the phone number and it tells you if the number is listed, | ||||||
43 | what names have been reported for this number, and a guess at the | ||||||
44 | company name of the caller. | ||||||
45 | |||||||
46 | =head2 new | ||||||
47 | |||||||
48 | my $who = WWW::WhoCallsMe->new; | ||||||
49 | |||||||
50 | Accepts no parameters. | ||||||
51 | |||||||
52 | Returns a new WWW::WhoCallsMe object for your enjoyment. | ||||||
53 | |||||||
54 | =cut | ||||||
55 | |||||||
56 | sub new | ||||||
57 | { | ||||||
58 | 0 | 0 | 1 | my $class = shift; | |||
59 | 0 | my $self = shift; | |||||
60 | |||||||
61 | 0 | $self->{ua} = LWP::UserAgent->new; | |||||
62 | 0 | $self->{ua}->agent("WWW::WhoCallsMe/$VERSION"); | |||||
63 | |||||||
64 | 0 | return bless($self, $class); | |||||
65 | } | ||||||
66 | |||||||
67 | =head2 fetch | ||||||
68 | |||||||
69 | my $hashref = $who->fetch($number); | ||||||
70 | |||||||
71 | Accepts one I |
||||||
72 | |||||||
73 | Returns a hashref containing this information: | ||||||
74 | |||||||
75 | =over | ||||||
76 | |||||||
77 | =item * | ||||||
78 | |||||||
79 | number - scalar: the number that was queried | ||||||
80 | |||||||
81 | =item * | ||||||
82 | |||||||
83 | success - scalar: whether or not the HTTP query succeeded | ||||||
84 | |||||||
85 | =item * | ||||||
86 | |||||||
87 | listed - scalar: determines if the number is listed | ||||||
88 | |||||||
89 | =item * | ||||||
90 | |||||||
91 | name - scalar: the guessed name of the caller (based on frequency of occurrences in the callername array) | ||||||
92 | |||||||
93 | =item * | ||||||
94 | |||||||
95 | callername - array: list of reported caller names (the "Caller:" field) | ||||||
96 | |||||||
97 | =item * | ||||||
98 | |||||||
99 | callerid - array: list of reported caller id values (the "Caller ID:" field) | ||||||
100 | |||||||
101 | =back | ||||||
102 | |||||||
103 | =cut | ||||||
104 | |||||||
105 | sub fetch | ||||||
106 | { | ||||||
107 | 0 | 0 | 1 | my $self = shift; | |||
108 | 0 | my $number = shift; | |||||
109 | 0 | $number =~ s/[^0-9]//g; | |||||
110 | |||||||
111 | 0 | my $req = HTTP::Request->new(GET => 'http://whocallsme.com/Phone-Number.aspx/'.$number); | |||||
112 | 0 | my $res = $self->{ua}->request($req); | |||||
113 | |||||||
114 | 0 | my $return = { | |||||
115 | number => $number, | ||||||
116 | }; | ||||||
117 | |||||||
118 | 0 | my $content = $res->content; | |||||
119 | 0 | 0 | $return->{success} = ($res->is_success ? 1 : 0); | ||||
120 | 0 | 0 | $return->{listed} = (($content =~ m#\s+phone\s+number\s+comments:#i) ? 1 : 0); # no comments means no listing | ||||
121 | 0 | @{$return->{callername}} = $content =~ m# Caller:\s*(.*?)\s* #ig; |
|||||
0 | |||||||
122 | 0 | @{$return->{callerid}} = $content =~ m# Caller\s+ID:\s*(.*?)\s* #ig; |
|||||
0 | |||||||
123 | |||||||
124 | 0 | my $callernames = {}; | |||||
125 | 0 | my $maxcallercount = 0; | |||||
126 | 0 | my $maxcallername = 'unknown'; | |||||
127 | 0 | foreach my $callername (@{$return->{callername}}) | |||||
0 | |||||||
128 | { | ||||||
129 | 0 | $callername =~ s/[\?\'\s]+/ /g; | |||||
130 | 0 | $callername =~ s/^\s+//; | |||||
131 | 0 | $callername =~ s/\s+$//; | |||||
132 | 0 | 0 | next unless $callername; | ||||
133 | |||||||
134 | 0 | $callernames->{uc($callername)}++; | |||||
135 | 0 | 0 | if ($callernames->{uc($callername)} > $maxcallercount) | ||||
136 | { | ||||||
137 | 0 | $maxcallercount = $callernames->{uc($callername)}; | |||||
138 | 0 | $maxcallername = uc($callername); | |||||
139 | } | ||||||
140 | } | ||||||
141 | |||||||
142 | 0 | $return->{name} = $maxcallername; | |||||
143 | |||||||
144 | 0 | return $return; | |||||
145 | } | ||||||
146 | |||||||
147 | =head1 DEPENDENCIES | ||||||
148 | |||||||
149 | L |
||||||
150 | |||||||
151 | =head1 SEE ALSO | ||||||
152 | |||||||
153 | L |
||||||
154 | |||||||
155 | =head1 TODO | ||||||
156 | |||||||
157 | I have no plans to expand this module, but I welcome any wishlist | ||||||
158 | requests. If you can think of something reasonable to add to this | ||||||
159 | module, I'll consider doing it. I also accept patches from others. | ||||||
160 | |||||||
161 | =head1 BUGS | ||||||
162 | |||||||
163 | Report all bugs through CPAN's bug reporting tool. Feel free to | ||||||
164 | file wishlist requests there as well. | ||||||
165 | |||||||
166 | =head1 COPYRIGHT / LICENSE | ||||||
167 | |||||||
168 | All data that is provided by this module is provided by | ||||||
169 | WhoCallsMe.com. They probably own the copyright to all of the data. | ||||||
170 | Their site doesn't appear to specify any kind of copyright or licensing | ||||||
171 | information. Be reasonable with it. I'll leave it up to you to | ||||||
172 | interpret what they think is okay for you to do with their data. | ||||||
173 | |||||||
174 | The (short amount of) code in this module is (C) Dusty Wilson, but | ||||||
175 | no real rights are reserved. Feel free to use it as you see fit, | ||||||
176 | as long as it doesn't get me in trouble. ;-) | ||||||
177 | |||||||
178 | =cut |