| 1 | package WWW::Contact::Gmail; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | extends 'WWW::Contact::Base'; | ||
| 5 | |||
| 6 | our $VERSION = '0.24'; | ||
| 7 | our $AUTHORITY = 'cpan:FAYLAND'; | ||
| 8 | |||
| 9 | has '+ua_class' => ( default => 'WWW::Mechanize::GZip' ); | ||
| 10 | |||
| 11 | use HTML::TokeParser::Simple; | ||
| 12 | |||
| 13 | sub get_contacts { | ||
| 14 | my ($self, $email, $password) = @_; | ||
| 15 | |||
| 16 | # reset | ||
| 17 | $self->errstr(undef); | ||
| 18 | my @contacts; | ||
| 19 | |||
| 20 | my $ua = $self->ua; | ||
| 21 | $self->debug("start get_contacts from gmail"); | ||
| 22 | |||
| 23 | # to form | ||
| 24 | $self->get('https://mail.google.com/mail/') || return; | ||
| 25 | $self->submit_form( | ||
| 26 | form_number => 1, | ||
| 27 | fields => { | ||
| 28 | Email => $email, | ||
| 29 | Passwd => $password, | ||
| 30 | } | ||
| 31 | ) || return; | ||
| 32 | my $content = $ua->content(); | ||
| 33 | if ($content =~ /=[\'\"]errormsg/) { | ||
| 34 | $self->errstr('Wrong Username or Password'); | ||
| 35 | return; | ||
| 36 | } | ||
| 37 | |||
| 38 | $self->debug('Login OK'); | ||
| 39 | # use basic HTML | ||
| 40 | $self->get('https://mail.google.com/mail/?ui=html&zy=a') || return; | ||
| 41 | $ua->follow_link( url => '?v=cl' ); | ||
| 42 | $ua->follow_link( url => '?v=cl&pnl=a' ); | ||
| 43 | |||
| 44 | $content = $ua->content(); | ||
| 45 | @contacts = $self->get_contacts_from_html($content); | ||
| 46 | |||
| 47 | return wantarray ? @contacts : \@contacts; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub get_contacts_from_html { | ||
| 51 | my ($self, $content) = @_; | ||
| 52 | |||
| 53 | my (@names, @emails); | ||
| 54 | |||
| 55 | my $start = 0; | ||
| 56 | my $p = HTML::TokeParser::Simple->new( string => $content ); | ||
| 57 | while ( my $token = $p->get_token ) { | ||
| 58 | if ( my $tag = $token->get_tag ) { | ||
| 59 | # start with input checbox and <input type="checkbox" name="c" | ||
| 60 | # end with /table | ||
| 61 | if ($tag eq 'input') { | ||
| 62 | my $type = $token->get_attr('type'); | ||
| 63 | my $name = $token->get_attr('name'); | ||
| 64 | if ( $type and $type eq 'checkbox' and $name and $name eq 'c' ) { | ||
| 65 | $start = 1; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | $start = 0 if ($tag eq 'table'); | ||
| 69 | if ($start and $tag eq 'a') { # <a href="?v=ct&ct_id=0">fayland</a> | ||
| 70 | my $href = $token->get_attr('href'); | ||
| 71 | if ( $href and $href =~ /v\=ct\&ct_id\=/ ) { | ||
| 72 | my $name = $p->peek(1); | ||
| 73 | push @names, $name; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | if ($start) { | ||
| 78 | if( scalar @names > scalar @emails and $token->is_start_tag('td') ) { | ||
| 79 | my $text = $p->peek(2); | ||
| 80 | if ($text =~ /(\S+\@\S+)/) { | ||
| 81 | push @emails, $1; | ||
| 82 | } else { | ||
| 83 | pop @names; # remove last name for NoEmail | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | my @contacts; | ||
| 90 | foreach my $i (0 .. $#emails) { | ||
| 91 | push @contacts, { | ||
| 92 | name => $names[$i], | ||
| 93 | email => $emails[$i] | ||
| 94 | }; | ||
| 95 | } | ||
| 96 | |||
| 97 | return @contacts; | ||
| 98 | } | ||
| 99 | |||
| 100 | no Moose; | ||
| 101 | __PACKAGE__->meta->make_immutable; | ||
| 102 | |||
| 103 | 1; | ||
| 104 | __END__ | ||
| 105 | |||
| 106 | =head1 NAME | ||
| 107 | |||
| 108 | WWW::Contact::Gmail - Get contacts/addressbook from Gmail (DEPRECATED for GoogleContactsAPI) | ||
| 109 | |||
| 110 | =head1 SYNOPSIS | ||
| 111 | |||
| 112 | use WWW::Contact; | ||
| 113 | use Data::Dumper; | ||
| 114 | |||
| 115 | my $wc = WWW::Contact->new(); | ||
| 116 | $wc->known_supplier->{'gmail.com'} = 'Gmail'; | ||
| 117 | my @contacts = $wc->get_contacts('itsa@gmail.com', 'password'); | ||
| 118 | my $errstr = $wc->errstr; | ||
| 119 | if ($errstr) { | ||
| 120 | die $errstr; | ||
| 121 | } else { | ||
| 122 | print Dumper(\@contacts); | ||
| 123 | } | ||
| 124 | |||
| 125 | =head1 DESCRIPTION | ||
| 126 | |||
| 127 | Get contacts from Gmail. Extends L<WWW::Contact::Base>, replaced by L<WWW::Contact::GoogleContactsAPI> | ||
| 128 | |||
| 129 | =head1 SEE ALSO | ||
| 130 | |||
| 131 | L<WWW::Contact>, L<WWW::Mechanize>, L<HTML::TokeParser::Simple> | ||
| 132 | |||
| 133 | =head1 AUTHOR | ||
| 134 | |||
| 135 | Fayland Lam, C<< <fayland at gmail.com> >> | ||
| 136 | |||
| 137 | =head1 COPYRIGHT & LICENSE | ||
| 138 | |||
| 139 | Copyright 2008 Fayland Lam, all rights reserved. | ||
| 140 | |||
| 141 | This program is free software; you can redistribute it and/or modify it | ||
| 142 | under the same terms as Perl itself. | ||
| 143 | |||
| 144 | =cut |