blib/lib/WWW/Contact/Yahoo.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 20 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 120 CC 12 SCAR 47.1
CC
12
Cov
0%
get_contacts
156.0
BEGIN
2.0
BEGIN
2.0
CRAP 110.7
1 package WWW::Contact::Yahoo;
2  
3 use Encode qw( decode );
4 use Moose;
5 extends 'WWW::Contact::Base';
6 use Encode;
7  
8 our $VERSION = '0.46';
9 our $AUTHORITY = 'cpan:FAYLAND';
10  
11 has '+ua_class' => ( default => 'WWW::Mechanize::GZip' );
12  
13 use HTML::TreeBuilder;
14  
15 sub get_contacts {
16 my ($self, $email, $password) = @_;
17  
18 # reset
19 $self->errstr(undef);
20 my @contacts;
21  
22 my $ua = $self->ua;
23 $self->debug("start get_contacts from Yahoo!");
24  
25 # to form
26 $self->get('https://login.yahoo.com/config/login_verify2?&.src=ym') || return;
27 $self->submit_form(
28 form_name => 'login_form',
29 fields => {
30 login => $email,
31 passwd => $password,
32 },
33 ) || return;
34 my $content = $ua->content();
35 if ($content =~ /=[\'\"]yregertxt/) {
36 $self->errstr('Wrong Username or Password');
37 return;
38 }
39  
40 # https://edit.yahoo.com/recovery/update
41 if ( $ua->base =~ /recovery\/update/ ) {
42 $self->errstr("Account Recovery Issue");
43 return;
44 }
45  
46 $self->debug('Login OK');
47  
48 $self->get('http://address.mail.yahoo.com/?_src=&VPC=tools_print') || return;
49  
50 $self->submit_form(
51 with_fields => {
52 'field[allc]' => 1,
53 'field[style]' => 'quick',
54 },
55 button => 'submit[action_display]',
56 ) || return;
57  
58 my $tree = HTML::TreeBuilder->new_from_content( decode('utf8', $ua->content) );
59 my @tables = $tree->look_down( '_tag', 'table', 'class', 'qprintable2' );
60 while (my $table = shift @tables) {
61 # two tr, one has class phead
62 my @trs = $table->look_down( '_tag', 'tr' );
63 my ($phead_tr, @other_tr);
64 while (my $tr = shift @trs) {
65 if ( $tr->attr('class') and $tr->attr('class') eq 'phead' ) {
66 $phead_tr = $tr;
67 } else {
68 push @other_tr, $tr;
69 }
70 }
71 my $name = $phead_tr->look_down( '_tag', 'b' )->as_text;
72 $name ||= $phead_tr->look_down( '_tag', 'i' ) ? $phead_tr->look_down( '_tag', 'i' )->as_text : '';
73 my $yahoo_id = $phead_tr->look_down( '_tag', 'small' ) ? $phead_tr->look_down( '_tag', 'small' )->as_text : '';
74 my $email;
75 OTR: while (my $tr = shift @other_tr) {
76 my @divs = $tr->look_down( '_tag', 'div' );
77 foreach my $div (@divs) {
78 my $text = $div->as_text;
79 next unless $text;
80 if ( $text =~ /\@/ ) {
81 $email = $text;
82 last OTR;
83 }
84 }
85 }
86 if (not $email and $yahoo_id) {
87 # treat as '@yahoo.com' by default
88 $email = ($yahoo_id =~ /\@/) ? $yahoo_id : $yahoo_id . '@yahoo.com';
89 }
90 next unless $email;
91 $name =~ s/(^\s+|\s+$)//g;
92 $name ||= $yahoo_id;
93 push @contacts, {
94 name => $name,
95 email => $email,
96 };
97 }
98 $tree = $tree->delete;
99  
100 return wantarray ? @contacts : \@contacts;
101 }
102  
103 no Moose;
104 __PACKAGE__->meta->make_immutable;
105  
106 1;
107 __END__
108  
109 =head1 NAME
110  
111 WWW::Contact::Yahoo - Get contacts/addressbook from Yahoo! Mail
112  
113 =head1 SYNOPSIS
114  
115 use WWW::Contact;
116 use Data::Dumper;
117  
118 my $wc = WWW::Contact->new();
119 my @contacts = $wc->get_contacts('itsa@yahoo.com', 'password');
120 my $errstr = $wc->errstr;
121 if ($errstr) {
122 die $errstr;
123 } else {
124 print Dumper(\@contacts);
125 }
126  
127 =head1 DESCRIPTION
128  
129 get addressbook from Yahoo! Mail. Extends L<WWW::Contact::Base>
130  
131 =head1 SEE ALSO
132  
133 L<WWW::Contact>, L<WWW::Mechanize::GZip>
134  
135 =head1 AUTHOR
136  
137 Fayland Lam, C<< <fayland at gmail.com> >>
138  
139 =head1 COPYRIGHT & LICENSE
140  
141 Copyright 2008 Fayland Lam, all rights reserved.
142  
143 This program is free software; you can redistribute it and/or modify it
144 under the same terms as Perl itself.
145  
146 =cut