blib/lib/Business/UTV.pm

filter: Statementstmt 64.7% 68 / 105 Branchbran 44.4% 16 / 36 Conditioncond 33.3% 3 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 88.8% 8 / 9 Podpod 75.0% 3 / 4 totaltotal 59.5% 100 / 168 CC 22 SCAR 41.7
CC
22
Cov
58%
current_statement
156.0
usage
6.0
login
4.0
CRAP 64.6
↑ Summary
1 package Business::UTV;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 our $VERSION = 0.05;
11 6 our $errstr = undef;
12  
13 sub login
14 5 ▶{
Subroutine login: called CC=4 SCAR=13.9
Pod documented
15 5 my ( $class , $username , $password , $atts ) = @_;
16  
17 5 errstr( "" );
18
19 5 my $name = $atts->{"name"};
20 5 my $login_url = "https://ssl2.u.tv/clicksilveraccountie/gologin.asp";
21 5 my $usage_url = "https://ssl2.u.tv/clicksilveraccountie/onlineusage.asp?id=$username";
22 5 my $newindex_url = "https://ssl2.u.tv/clicksilveraccountie/newindex.asp";
23
24 5 my $statement_url = "https://ssl2.u.tv/clicksilveraccountie/statementbeta.asp";
25
26 5 my $ua = LWP::UserAgent->new();
27  
28 5 my $login_request = $ua->post( $login_url ,
29 { "id" => $username , "password" => $password } ,
30 "Referer" => $newindex_url );
31  
32 5 ▶ if( $login_request->is_error() )
Branch: 1 branch
BranchTrueFalseTotal
if ($login_request->is_error) 1 4 5
33 {
34 1 errstr( "Login failed : http problem" );
35 1 return undef;
36 }
37  
38 4 my $login = $login_request->content();
39 4 my %data = ();
40  
41 4 ▶ unless( $login =~ /\Q$name\E/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($login =~ /\Q$name\E/) 2 2 4
42 {
43 2 errstr( "Login failed : your name '$name' not matched" );
44 2 return undef;
45 }
46  
47 2 ▶ while( $login =~ /<input\s+type\s*=\s*hidden\s+name\s*=\s*([^ ]+)\s+value\s*=\s*['"]?([^'">]+)['"]?>/ig )
Branch: 1 branch
BranchTrueFalseTotal
if ($login =~ /<input\s+type\s*=\s*hidden\s+name\s*=\s*([^ ]+)\s+value\s*=\s*['"]?([^'">]+)['"]?>/gi) 0 2 2
48 {
49 0 $data{$1}=$2;
50 }
51
52 2 my %self = %$atts;
53
54 2 $self{"ua"} = $ua;
55 2 $self{"login_url"} = $login_url;
56 2 $self{"usage_url"} = $usage_url;
57 2 $self{"statement_url"} = $statement_url;
58
59 2 $self{"username"} = $username;
60 2 $self{"password"} = $password;
61 2 $self{"_data"} = \%data;
62
63 2 bless( \%self , $class );
64 }
65  
66  
67 sub usage
68 8 ▶{
Subroutine usage: called CC=6 SCAR=17.9
Pod documented
69 8 my ( $self ) = @_;
70  
71 8 errstr( "" );
72
73 8 my $upload;
74 my $download;
75  
76 8 my $usage_request = $self->{"ua"}->get( $self->{"usage_url"} );
77  
78 8 ▶ if( $usage_request->is_error() )
Branch: 1 branch
BranchTrueFalseTotal
if ($usage_request->is_error) 1 7 8
79 {
80 1 errstr( "Usage failed : http problem" );
81 1 return undef;
82 }
83  
84 7 my $usage = $usage_request->content();
85
86 7 ▶ if( $usage =~ /Incoming:\s*(\d+(\.\d+)?)MB/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($usage =~ /Incoming:\s*(\d+(\.\d+)?)MB/) 5 2 7
87 {
88 5 $upload = $1;
89 }
90 7 ▶ if( $usage =~ /Outgoing:\s*(\d+(\.\d+)?)MB/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($usage =~ /Outgoing:\s*(\d+(\.\d+)?)MB/) 5 2 7
91 {
92 5 $download = $1;
93 }
94  
95 7 ▶ if( defined($upload) && defined($download) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $upload and defined $download) { } 4 3 7
Condition100%3/3
defined $upload and defined $download
!ll&&!rl&&r
214
MC/DC100%2/2
defined $upload and defined $download
defined $upload defined $download
Truth table100%3/3
A and B
A= defined $uploadB= defined $download
ABresult
0X0
100
111
96 {
97 4 return { "upload" => $upload , "download" => $download };
98 }
99 else
100 {
101 3 errstr( "Could not retrieve upload and download usage" );
102 3 return undef;
103 }
104 }
105  
106  
107 sub current_statement
108 0 ▶{
Subroutine current_statement: not called CC=12 SCAR=50.5
Pod documented
109 0 my ( $self ) = @_;
110  
111 0 errstr( "" );
112
113 0 my $total = undef;
114 0 my $mytotal = 0;
115 0 my @calls = ();
116
117 my $referer = "https://ssl2.u.tv/clicksilveraccountie/menu.asp" .
118 "?usersname=" . uri_escape( $self->{"_data"}->{"usersname"} ) .
119 0 "&tariffcode=" . uri_escape( $self->{"_data"}->{"tariffcode"} );
120  
121 my $statement_request = $self->{"ua"}->post(
122 $self->{"statement_url"} ,
123 0 { "custid" => $self->{"username"} },
124 "Referer" => $referer
125 );
126
127 0 my $statement = $statement_request->content();
128 0 $statement =~ s/<font[^>]+>//ig;
129 0 $statement =~ s/<\/font>//ig;
130 0 $statement =~ s/&nbsp;//g;
131  
132 0 ▶ while( $statement =~ /<tr[^>]*>(.*?)<\/tr>/isg )
Branch: 1 branch
BranchTrueFalseTotal
if ($statement =~ m[<tr[^>]*>(.*?)</tr>]sig) 0 0 0
133 {
134 0 my $line = $1;
135 0 ▶ if( $line =~ /total/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /total/i) 0 0 0
136 {
137 0 ▶ if( $line =~ /&euro;(.+)\s/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /&euro;(.+)\s/) 0 0 0
138 {
139 0 $total = $1;
140 }
141 }
142
143 0 my $count = 0;
144 0 my @fields = ();
145 0 my $call = {};
146 0 ▶ while( $line =~ /<td[^>]+>(.*?)<\/td>/g )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[<td[^>]+>(.*?)</td>]g) 0 0 0
147 {
148 0 ▶ if( length($1) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $1 > 0) 0 0 0
149 {
150 0 push( @fields , $1 );
151 }
152 0 $count++;
153 }
154  
155 0 ▶ if( $count == 8 && scalar( @fields ) == 6 && $fields[0] ne "<b><u>Date</u></b>" )
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 8 and scalar @fields == 6 and $fields[0] ne "<b><u>Date</u></b>") 0 0 0
Condition0%0/6
$count == 8 and scalar @fields == 6 and $fields[0] ne "<b><u>Date</u></b>"
!ll&&!rl&&r
000
$count == 8 and scalar @fields == 6
!ll&&!rl&&r
000
MC/DC0%0/3
$count == 8 and scalar @fields == 6 and $fields[0] ne "<b><u>Date</u></b>"
$count == 8 scalar @fields == 6 $fields[0] ne "<b><u>Date</u></b>"
Truth table0%0/4
A and B and C
A= $count == 8B= scalar @fields == 6C= $fields[0] ne "<b><u>Date</u></b>"
ABCresult
0XX0
10X0
1100
1111
156 {
157 0 @$call{ ( "date" , "time" , "phone_number" , "type" , "length" , "cost" ) } = @fields;
158 0 ▶ if( $call->{"cost"} eq "FREE!" )
Branch: 1 branch
BranchTrueFalseTotal
if ($call->{"cost"} eq "FREE!") 0 0 0
159 {
160 0 $call->{"cost"} = 0;
161 }
162 0 $mytotal = $mytotal + $call->{"cost"};
163 0 push( @calls , $call );
164 }
165 }
166  
167 0 ▶ if( !defined( $total ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $total) 0 0 0
168 {
169 0 errstr( "Could not find their total" );
170 0 return undef;
171 }
172 0 ▶ if( abs($total-$mytotal) > 0.1 )
Branch: 1 branch
BranchTrueFalseTotal
if (abs $total - $mytotal > "0.1") 0 0 0
173 {
174 0 errstr( "I calculated total of $mytotal but they said total is $total" );
175 0 return undef;
176 }
177 0 return ( $total , \@calls );
178 }
179  
180 sub errstr
181 23 ▶{
Subroutine errstr: called CC=3 SCAR=11.0
Pod undocumented
182 23 my ( $error ) = @_;
183
184 23 ▶ if( defined( $error ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 23 0 23
185 {
186 23 $errstr = $error;
187 23 ▶ if( length( $errstr ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $Business::UTV::errstr) { } 10 13 23
188 {
189 10 warn( $errstr );
190 }
191 else
192 {
193 13 $errstr = undef;
194 }
195 }
196 23 return $errstr;
197 }
198  
199 =head1 NAME
200  
201 Business::UTV - Module for retrieiving UTV internet account information
202  
203 =head1 SYNOPSIS
204  
205 use Business::UTV;
206 my $utv = Business::UTV->login( $id , $password , { "name" => "me" } );
207 my $usage = $utv->usage();
208 print "Upload = " . $usage->{"upload"} . "MB\n";
209  
210 =head1 DESCRIPTION
211  
212 This module enables you to access your UTV account information using perl.
213  
214 Currently the only supported data is your current monthly upload/download
215 usage and call data from your latest phone bill.
216  
217 This module provides the following methods
218  
219 =head2 login
220  
221 $utv = Business::UTV->login( $id , $password , { "name" => $name }
222  
223 The constructor takes your utv id , password and a hash reference
224 and logs into the utv website. Login is verified be checking the
225 name of the account holder is correctly returned.
226  
227 On failure undef is returned and an error message stored in $Business::UTV::errstr
228  
229  
230 =head2 usage
231  
232 my $usage = $utv->usage();
233 print "Upload - " . $usage->{"upload"} . "\n";
234 print "Download - " . $usage->{"download"} . "\n";
235  
236 This method retrieves the accounts current upload and download in
237 megabytes as a hash reference.
238  
239 On failure undef is returned and an error message is stored in $Business::UTV::errstr
240  
241 =head2 current_statement
242  
243 my ( $total , $calls ) = $utv->current_statement();
244  
245 This method returns the total of the latest bill and details of any phone calls.
246  
247 Calls are returned as an array reference with each call a hash ref with the following
248 fields
249  
250 date
251 time
252 phone_number
253 type
254 length
255 cost
256  
257 On failure undef is returned and an error message is stored in $Business::UTV::errstr
258  
259 =head1 LIMITATIONS
260  
261 By definition I am limited to my own account when writting this module.
262 If some features do not work as expected or at all contact me and I'll
263 do my best to add support for different account configurations.
264  
265 =head1 WARNING
266  
267 This warning is (mostly) from Simon Cozens' Finance::Bank::LloydsTSB, and seems almost as apt here.
268
269 This is code for pretending to be you online, and that could mean your money, and that means BE CAREFUL.
270 You are encouraged, nay, expected, to audit the source of this module yourself to reassure yourself
271 that I am not doing anything untoward with your account data. This software is useful to me, but is
272 provided under NO GUARANTEE, explicit or implied.
273  
274 =head1 SEE ALSO
275  
276 utv_usage_applet.pl
277 utv_usage_tray.pl
278  
279 =cut
280  
281 6 1;
↑ Summary