blib/lib/WWW/Contact/BG/Abv.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 64 CC 7 SCAR 37.8
CC
7
Cov
0%
get_contacts
56.0
BEGIN
2.0
BEGIN
2.0
CRAP 44.0
1 package WWW::Contact::BG::Abv;
2  
3 use Moose;
4 extends 'WWW::Contact::Base';
5  
6 our $VERSION = '0.26';
7  
8 sub get_contacts {
9 my ($self, $email, $password) = @_;
10
11 # reset
12 $self->errstr(undef);
13 my @contacts;
14
15 my $ua = $self->ua;
16 $self->debug("start get_contacts from abv");
17
18 # to form
19 # with_fields is used, 'cause username and password are not ids
20 $self->get('http://www.abv.bg/') || return;
21 $self->submit_form(
22 form_number => 1,
23 with_fields => {
24 username => $email,
25 password => $password,
26 },
27 ) || return;
28  
29 my $content = $ua->content();
30 if ($content =~ /login_help/) {
31 $self->errstr('Wrong Username or Password');
32 return;
33 }
34
35 my $base;
36 if ( $content =~ m/replace\("(.*)"\)/g ) {
37 my $fw = $1;
38 my $headers = $ua->head( $fw );
39 my $c = $headers->previous()->header( 'set-cookie' );
40 if ( $c =~ m/uhost=([a-zA-Z0-9]+\.abv.bg)/i ) {
41 $base = $1;
42 } else {
43 $self->errstr( 'Could not match base' );
44 return;
45 }
46 } else {
47 $self->errstr( 'Could not match address' );
48 return;
49 }
50  
51 $self->debug('Login OK');
52
53 # FIXME: ssl connection is better
54 $ua->get( "http://$base/app/servlet/addrimpex?action=EXPORT&program=40" );
55
56 # The content is CSV file
57 $content = $ua->content();
58
59 while ( $content
60 # $1 = fname, $2=lname, $3 = "lname, fname', $4 = email
61 =~ /^(.*?)\,(.*?)\,\"(.*?\,.*?)\"\,.*?\,(.*?)\,\,{31}/mg
62 ) {
63 next unless $4;
64  
65 my $email = $4;
66 my $name = ( $1 or $2 ) ? "$1 $2" : $3;
67 push @contacts, {
68 name => $name,
69 email => $email,
70 };
71 }
72  
73 return wantarray ? @contacts : \@contacts;
74 }
75  
76 no Moose;
77 __PACKAGE__->meta->make_immutable;
78  
79 1;
80 __END__
81  
82 =head1 NAME
83  
84 WWW::Contact::BG::Abv - Get contacts/addressbook from Abv.bg
85  
86 =head1 SYNOPSIS
87  
88 use WWW::Contact;
89
90 my $wc = WWW::Contact->new();
91 my @contacts = $wc->get_contacts('itsa@abv.bg', 'password');
92 my $errstr = $wc->errstr;
93 if ($errstr) {
94 die $errstr;
95 } else {
96 print Dumper(\@contacts);
97 }
98  
99 =head1 DESCRIPTION
100  
101 get contacts from Abv.bg, gbg.bg, gyuvectch.bg
102  
103 =head1 SEE ALSO
104  
105 L<WWW::Contact>, L<WWW::Mechanize>
106  
107 =head1 AUTHOR
108  
109 Dimitar Petrov, C<< <mitko at datamax.bg> >>
110  
111 =head1 COPYRIGHT & LICENSE
112  
113 Copyright 2009 Dimitar Petrov, all rights reserved.
114  
115 This program is free software; you can redistribute it and/or modify it
116 under the same terms as Perl itself.
117  
118 =cut