our
$VERSION
=
'0.46'
;
our
$AUTHORITY
=
'cpan:FAYLAND'
;
has
'+ua_class'
=> (
default
=>
'WWW::Mechanize::GZip'
);
sub
get_contacts {
my
(
$self
,
$email
,
$password
) =
@_
;
$self
->errstr(
undef
);
my
@contacts
;
my
$ua
=
$self
->ua;
$self
->debug(
"start get_contacts from Yahoo!"
);
$self
->submit_form(
form_name
=>
'login_form'
,
fields
=> {
login
=>
$email
,
passwd
=>
$password
,
},
) ||
return
;
my
$content
=
$ua
->content();
if
(
$content
=~ /=[\'\"]yregertxt/) {
$self
->errstr(
'Wrong Username or Password'
);
return
;
}
if
(
$ua
->base =~ /recovery\/update/ ) {
$self
->errstr(
"Account Recovery Issue"
);
return
;
}
$self
->debug(
'Login OK'
);
$self
->submit_form(
with_fields
=> {
'field[allc]'
=> 1,
'field[style]'
=>
'quick'
,
},
button
=>
'submit[action_display]'
,
) ||
return
;
my
$tree
= HTML::TreeBuilder->new_from_content( decode(
'utf8'
,
$ua
->content) );
my
@tables
=
$tree
->look_down(
'_tag'
,
'table'
,
'class'
,
'qprintable2'
);
while
(
my
$table
=
shift
@tables
) {
my
@trs
=
$table
->look_down(
'_tag'
,
'tr'
);
my
(
$phead_tr
,
@other_tr
);
while
(
my
$tr
=
shift
@trs
) {
if
(
$tr
->attr(
'class'
) and
$tr
->attr(
'class'
) eq
'phead'
) {
$phead_tr
=
$tr
;
}
else
{
push
@other_tr
,
$tr
;
}
}
my
$name
=
$phead_tr
->look_down(
'_tag'
,
'b'
)->as_text;
$name
||=
$phead_tr
->look_down(
'_tag'
,
'i'
) ?
$phead_tr
->look_down(
'_tag'
,
'i'
)->as_text :
''
;
my
$yahoo_id
=
$phead_tr
->look_down(
'_tag'
,
'small'
) ?
$phead_tr
->look_down(
'_tag'
,
'small'
)->as_text :
''
;
my
$email
;
OTR:
while
(
my
$tr
=
shift
@other_tr
) {
my
@divs
=
$tr
->look_down(
'_tag'
,
'div'
);
foreach
my
$div
(
@divs
) {
my
$text
=
$div
->as_text;
next
unless
$text
;
if
(
$text
=~ /\@/ ) {
$email
=
$text
;
last
OTR;
}
}
}
if
(not
$email
and
$yahoo_id
) {
$email
= (
$yahoo_id
=~ /\@/) ?
$yahoo_id
:
$yahoo_id
.
'@yahoo.com'
;
}
next
unless
$email
;
$name
=~ s/(^\s+|\s+$)//g;
$name
||=
$yahoo_id
;
push
@contacts
, {
name
=>
$name
,
email
=>
$email
,
};
}
$tree
=
$tree
->
delete
;
return
wantarray
?
@contacts
: \
@contacts
;
}
no
Moose;
__PACKAGE__->meta->make_immutable;
1;