| 1 | package WWW::Contact::CN::163; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | extends 'WWW::Contact::Base'; | ||
| 5 | |||
| 6 | our $VERSION = '0.28'; | ||
| 7 | our $AUTHORITY = 'cpan:FAYLAND'; | ||
| 8 | |||
| 9 | sub get_contacts { | ||
| 10 | my ($self, $email, $password) = @_; | ||
| 11 | |||
| 12 | # reset | ||
| 13 | $self->errstr(undef); | ||
| 14 | my @contacts; | ||
| 15 | |||
| 16 | my $ua = $self->ua; | ||
| 17 | $self->debug("start get_contacts from CN::163"); | ||
| 18 | |||
| 19 | # to form | ||
| 20 | $self->get('https://reg.163.com/logins.jsp?type=1&product=mail163&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D') || return; | ||
| 21 | $self->submit_form( | ||
| 22 | form_name => 'fLogin', | ||
| 23 | fields => { | ||
| 24 | username => $email, | ||
| 25 | password => $password, | ||
| 26 | }, | ||
| 27 | ) || return; | ||
| 28 | my $content = $ua->content(); | ||
| 29 | if ($content =~ /fLogin/) { | ||
| 30 | $self->errstr('Wrong Username or Password'); | ||
| 31 | return; | ||
| 32 | } | ||
| 33 | |||
| 34 | $self->debug('Login OK'); | ||
| 35 | |||
| 36 | while ( $ua->content() =~ /URL=(.*?)\"/ ) { | ||
| 37 | my $url = $1; | ||
| 38 | if ( $self->{ua}->content() =~ /window.location.replace\(\"(.*?)\"/ ) { | ||
| 39 | $self->get($1) || return; | ||
| 40 | } else { | ||
| 41 | $self->get($url) || return; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | my ($sid) = ( $ua->content() =~ /sid\=(\w+)(\"|\&)/ ); | ||
| 46 | unless ( $sid ) { | ||
| 47 | $self->errstr('Unknown Error'); | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | $self->get("/jy3/address/addrprint.jsp?sid=$sid") || return; | ||
| 52 | $content = $ua->content(); | ||
| 53 | @contacts = $self->get_contacts_from_html($content); | ||
| 54 | |||
| 55 | return wantarray ? @contacts : \@contacts; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub get_contacts_from_html { | ||
| 59 | my ($self, $content) = @_; | ||
| 60 | |||
| 61 | my @contents = split('class="gTitleSub', $content); | ||
| 62 | shift @contents; | ||
| 63 | |||
| 64 | my @contacts; | ||
| 65 | foreach my $con (@contents) { | ||
| 66 | my ( $name, $email ); | ||
| 67 | if ( $con =~ /mTT\"\>(.*?)\</ ) { | ||
| 68 | $name = $1; | ||
| 69 | } | ||
| 70 | if ( $con =~ /td\>\s*((.*?)\@(.*?))\</ ) { | ||
| 71 | $email = $1; | ||
| 72 | } | ||
| 73 | next unless $email; | ||
| 74 | |||
| 75 | my $c = { name => $name || $email, email => $email }; | ||
| 76 | push @contacts, $c; | ||
| 77 | } | ||
| 78 | |||
| 79 | return @contacts; | ||
| 80 | } | ||
| 81 | |||
| 82 | no Moose; | ||
| 83 | __PACKAGE__->meta->make_immutable; | ||
| 84 | |||
| 85 | 1; | ||
| 86 | __END__ | ||
| 87 | |||
| 88 | =head1 NAME | ||
| 89 | |||
| 90 | WWW::Contact::CN::163 - Get contacts/addressbook from mail.163.com | ||
| 91 | |||
| 92 | =head1 SYNOPSIS | ||
| 93 | |||
| 94 | use WWW::Contact; | ||
| 95 | |||
| 96 | my $wc = WWW::Contact->new(); | ||
| 97 | my @contacts = $wc->get_contacts('itsa@163.com', 'password'); | ||
| 98 | my $errstr = $wc->errstr; | ||
| 99 | if ($errstr) { | ||
| 100 | die $errstr; | ||
| 101 | } else { | ||
| 102 | print Dumper(\@contacts); | ||
| 103 | } | ||
| 104 | |||
| 105 | =head1 DESCRIPTION | ||
| 106 | |||
| 107 | Get contacts from mail.163.com. Extends L<WWW::Contact::Base> | ||
| 108 | |||
| 109 | =head1 SEE ALSO | ||
| 110 | |||
| 111 | L<WWW::Contact>, L<WWW::Mechanize>, L<HTML::TokeParser::Simple> | ||
| 112 | |||
| 113 | =head1 AUTHOR | ||
| 114 | |||
| 115 | Fayland Lam, C<< <fayland at gmail.com> >> | ||
| 116 | |||
| 117 | =head1 COPYRIGHT & LICENSE | ||
| 118 | |||
| 119 | Copyright 2008 Fayland Lam, all rights reserved. | ||
| 120 | |||
| 121 | This program is free software; you can redistribute it and/or modify it | ||
| 122 | under the same terms as Perl itself. | ||
| 123 | |||
| 124 | =cut |