| 1 | package WWW::Contact::GoogleContactsAPI; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | extends 'WWW::Contact::Base'; | ||
| 5 | |||
| 6 | our $VERSION = '0.44'; | ||
| 7 | our $AUTHORITY = 'cpan:FAYLAND'; | ||
| 8 | |||
| 9 | has 'skip_NoEmail' => ( is => 'ro', isa => 'Bool', default => 1 ); | ||
| 10 | has authsub => ( | ||
| 11 | is => 'ro', | ||
| 12 | isa => 'Net::Google::AuthSub', | ||
| 13 | lazy => 1, | ||
| 14 | default => sub { | ||
| 15 | require Net::Google::AuthSub; | ||
| 16 | Net::Google::AuthSub->new(service => 'cp'); | ||
| 17 | }, | ||
| 18 | ); | ||
| 19 | |||
| 20 | has 'json' => | ||
| 21 | ( | ||
| 22 | is => 'ro', | ||
| 23 | isa => 'JSON::XS', | ||
| 24 | lazy => 1, | ||
| 25 | default => sub | ||
| 26 | { | ||
| 27 | require JSON::XS; | ||
| 28 | return JSON::XS->new->utf8; | ||
| 29 | } | ||
| 30 | ); | ||
| 31 | |||
| 32 | # Authenticate with $email and $password and return either an array | ||
| 33 | # of contacts or a reference to an array, according to context | ||
| 34 | |||
| 35 | sub get_contacts { | ||
| 36 | my ($self, $email, $password) = @_; | ||
| 37 | |||
| 38 | $self->errstr(undef); # Reset | ||
| 39 | |||
| 40 | my $resp = $self->authsub->login($email, $password); | ||
| 41 | unless ($resp && $resp->is_success) | ||
| 42 | { | ||
| 43 | $self->errstr("Wrong Username or Password"); | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | my $url = "https://www.google.com/m8/feeds/contacts/default/full" | ||
| 47 | . "?max-results=9999&alt=json"; | ||
| 48 | $url .= "&v=3.0"; # Gives more fields | ||
| 49 | $self->get($url, $self->authsub->auth_params) | ||
| 50 | or return; | ||
| 51 | my $content = $self->ua->content; | ||
| 52 | my $data = $self->json->decode($content); | ||
| 53 | $data = $data->{feed} if ($data); | ||
| 54 | |||
| 55 | my @contacts; | ||
| 56 | @contacts = map $self->make_contact($_), @{$data->{entry}} | ||
| 57 | if ($data); | ||
| 58 | |||
| 59 | @contacts = grep { exists $_->{email} } @contacts | ||
| 60 | if $self->skip_NoEmail; | ||
| 61 | |||
| 62 | return wantarray ? @contacts : \@contacts; | ||
| 63 | } | ||
| 64 | |||
| 65 | # Return an entry from Google as a reference to a hash in our format. | ||
| 66 | # Feyland's 0.23 only returned "name" and "email" fields, we return | ||
| 67 | # everything. | ||
| 68 | |||
| 69 | sub make_contact { | ||
| 70 | my ($self, $in) = @_; | ||
| 71 | |||
| 72 | my %orig = %$in; | ||
| 73 | my $out = {}; | ||
| 74 | my $name; | ||
| 75 | if (my $n = delete $in->{'gd$name'}) { | ||
| 76 | $name = $n->{'gd$fullName'}->{'$t'}; | ||
| 77 | $out->{name} = $name; # Backward compatible | ||
| 78 | for ($n->{'gd$familyName'}->{'$t'}) | ||
| 79 | { | ||
| 80 | $out->{family_name} = $_ if ($_); | ||
| 81 | } | ||
| 82 | for ($n->{'gd$givenName'}->{'$t'}) | ||
| 83 | { | ||
| 84 | $out->{given_name} = $_ if ($_); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | my @links; | ||
| 89 | local $_ = delete $in->{link} | ||
| 90 | and @links = @$_; | ||
| 91 | |||
| 92 | foreach (@links) | ||
| 93 | { | ||
| 94 | if ($_->{type} eq 'image/*' && exists $_->{'gd$etag'}) { | ||
| 95 | (my $href = $_->{href}) =~ s/\?v=3\.0//; | ||
| 96 | $out->{photo}{href} = $href; | ||
| 97 | $out->{photo}{content} = sub { $self->ua->get($href, $self->authsub->auth_params)->decoded_content() }; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | # $out->{emails} = [[ADDRESS, TYPE], ...] | ||
| 102 | my @emails; | ||
| 103 | local $_ = delete $in->{'gd$email'} | ||
| 104 | and @emails = @$_; | ||
| 105 | foreach (@emails) | ||
| 106 | { | ||
| 107 | ($_->{rel} || "") =~ /#(.*)/; | ||
| 108 | $_ = [$_->{address}, $1]; | ||
| 109 | } | ||
| 110 | if (@emails) | ||
| 111 | { | ||
| 112 | $out->{emails} = \@emails; | ||
| 113 | # Derive single-value field for backward compatibility | ||
| 114 | ($out->{email}) = $emails[0][0]; | ||
| 115 | } | ||
| 116 | |||
| 117 | $_ = delete $in->{'gContact$nickname'} | ||
| 118 | and $_ = $_->{'$t'} | ||
| 119 | and $out->{nickname} = $_; | ||
| 120 | |||
| 121 | # $out->{addresses} = [[TYPE => ADDRESS], ...] | ||
| 122 | my @ads; | ||
| 123 | my @postal_ads; | ||
| 124 | $_ = delete $in->{'gd$structuredPostalAddress'} | ||
| 125 | and @ads = @$_; | ||
| 126 | foreach (@ads) | ||
| 127 | { | ||
| 128 | my $address; | ||
| 129 | ($address->{street1}, $address->{street2}) = split /\n/, delete $_->{'gd$street'}{'$t'}; | ||
| 130 | $address->{postal_code} = delete $_->{'gd$postcode'}{'$t'}; | ||
| 131 | $address->{city} = delete $_->{'gd$city'}{'$t'}; | ||
| 132 | $address->{state} = delete $_->{'gd$region'}{'$t'}; | ||
| 133 | $address->{country} = delete $_->{'gd$country'}{'$t'}; | ||
| 134 | |||
| 135 | my ($type) = (delete $_->{rel}) =~ /#(.*)/; | ||
| 136 | $_ = (delete $_->{'gd$formattedAddress'})->{'$t'}; | ||
| 137 | s/\s+$//g; s/\n/, /g; | ||
| 138 | $_ = [$_, $type]; | ||
| 139 | push @postal_ads, $address; | ||
| 140 | } | ||
| 141 | $out->{addresses} = \@ads if (@ads); | ||
| 142 | $out->{postal_addresses} = \@postal_ads if (@postal_ads); | ||
| 143 | |||
| 144 | my @events; | ||
| 145 | $_ = delete $in->{'gContact$event'} | ||
| 146 | and @events = @$_; | ||
| 147 | foreach (@events) | ||
| 148 | { | ||
| 149 | my $type = $_->{rel}; | ||
| 150 | $_ = $_->{'gd$when'} | ||
| 151 | and $_ = $_->{startTime} | ||
| 152 | and $_ = [$_, $type]; | ||
| 153 | } | ||
| 154 | @events = grep $_, @events; | ||
| 155 | $out->{events} = \@events if (@events); | ||
| 156 | |||
| 157 | $_ = delete $in->{'gContact$birthday'} | ||
| 158 | and $_ = $_->{when} | ||
| 159 | and $out->{birthday} = $_ | ||
| 160 | and $_ = _age($_) | ||
| 161 | and $out->{age} = $_; | ||
| 162 | |||
| 163 | my @ims; | ||
| 164 | $_ = delete $in->{'gd$im'} | ||
| 165 | and @ims = @$_; | ||
| 166 | foreach (@ims) | ||
| 167 | { | ||
| 168 | delete $_->{label}; # Always "None" | ||
| 169 | delete $_->{type}; # Nearly always "other" | ||
| 170 | my $p = delete $_->{protocol}; | ||
| 171 | if ($p &&= $p ne "None" && lc $p) | ||
| 172 | { | ||
| 173 | $p =~ s/.*#//; $p =~ s/google_/g/; | ||
| 174 | } | ||
| 175 | $_ = [$p || "?" => $_->{address}]; | ||
| 176 | } | ||
| 177 | $out->{instant_messengers} = \@ims if (@ims); | ||
| 178 | |||
| 179 | my @phones; | ||
| 180 | $_ = delete $in->{'gd$phoneNumber'} | ||
| 181 | and @phones = @$_; | ||
| 182 | foreach (@phones) | ||
| 183 | { | ||
| 184 | my ($type) = (delete $_->{rel} || "") =~ /#(.*)/; | ||
| 185 | my $t = delete $_->{'$t'}; | ||
| 186 | $t =~ s/(^\s+|\s+$)//isg; | ||
| 187 | $_ = [$t, $type]; | ||
| 188 | } | ||
| 189 | $out->{phones} = \@phones | ||
| 190 | if (@phones); | ||
| 191 | |||
| 192 | # Google doesn't seem to keep a creation date | ||
| 193 | $out->{updated} = (delete $in->{updated})->{'$t'}; | ||
| 194 | |||
| 195 | $_ = delete $in->{content} | ||
| 196 | and $_ = $_->{'$t'} | ||
| 197 | and $out->{notes} = $_; | ||
| 198 | |||
| 199 | # Map "Custom" fields. "Label" = key, "Custom value" = value | ||
| 200 | my @u; | ||
| 201 | $_ = delete $in->{'gContact$userDefinedField'} | ||
| 202 | # List of hash refs, {key => ..., value => ...} | ||
| 203 | and @u = @$_; | ||
| 204 | foreach (@u) { $out->{$_->{key}} = $_->{value} } | ||
| 205 | |||
| 206 | $_ = delete $in->{'gContact$relation'}; | ||
| 207 | if ($_) | ||
| 208 | { | ||
| 209 | foreach (@$_) | ||
| 210 | { | ||
| 211 | my $k = $_->{rel}; | ||
| 212 | push @{$out->{$k}}, $_->{'$t'}; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | my @orgs; | ||
| 217 | $_ = delete $in->{'gd$organization'} | ||
| 218 | and @orgs = @$_; | ||
| 219 | foreach (@orgs) | ||
| 220 | { | ||
| 221 | my ($type) = (delete $_->{rel} || "") =~ /#(.*)/; | ||
| 222 | $_ = [$_->{'gd$orgName'}->{'$t'}, $type]; | ||
| 223 | } | ||
| 224 | @orgs = grep $_->[0], @orgs; # Zap null orgs | ||
| 225 | $out->{organisations} = \@orgs if (@orgs); | ||
| 226 | |||
| 227 | my @urls; | ||
| 228 | $_ = delete $in->{'gContact$website'} | ||
| 229 | and @urls = @$_; | ||
| 230 | foreach (@urls) | ||
| 231 | { | ||
| 232 | $_ = {$_->{rel} => $_->{href}}; | ||
| 233 | } | ||
| 234 | $out->{urls} = \@urls if (@urls); | ||
| 235 | |||
| 236 | my @groups; | ||
| 237 | $_ = delete $in->{'gContact$groupMembershipInfo'} | ||
| 238 | and @groups = @$_; | ||
| 239 | @groups = map $self->group_name($_), @groups; | ||
| 240 | $out->{groups} = \@groups if (@groups); | ||
| 241 | |||
| 242 | # Ignore id link app$edited gd$etag category title | ||
| 243 | |||
| 244 | return $out; | ||
| 245 | } | ||
| 246 | |||
| 247 | # Return a list containing the name of a group given | ||
| 248 | # its URL or () if unknown, e.g. "My Contacts" | ||
| 249 | |||
| 250 | { # statics | ||
| 251 | my %name; # Map known URLs to group names | ||
| 252 | sub group_name { | ||
| 253 | my ($self, $g) = @_; | ||
| 254 | |||
| 255 | my $url = $g->{href}; | ||
| 256 | |||
| 257 | unless (exists $name{$url}) | ||
| 258 | { | ||
| 259 | $_ = eval { $self->get("$url?alt=json", $self->authsub->auth_params) }; | ||
| 260 | $self->errstr(undef); | ||
| 261 | if ($_) | ||
| 262 | { | ||
| 263 | $_ = $self->ua->content; | ||
| 264 | $_ = $self->json->decode($_); | ||
| 265 | $_ = $_->{entry}{content}{'$t'}; | ||
| 266 | } | ||
| 267 | $name{$url} = $_; | ||
| 268 | } | ||
| 269 | return $name{$url} || (); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | # Given birth date YYYY-MM-DD, return the age in years | ||
| 274 | sub _age { | ||
| 275 | my ($dob) = @_; | ||
| 276 | |||
| 277 | my ($by, $bm, $bd) = split /-/, $dob; | ||
| 278 | return undef unless ($by && $bm && $bd); | ||
| 279 | |||
| 280 | my @now = localtime; | ||
| 281 | my ($y, $m, $d) = (1900 + $now[5], 1 + $now[4], $now[3]); | ||
| 282 | # Decrement age if hasn't had birthday yet this year | ||
| 283 | my $age = $y - $by - ($m < $bm || $m == $bm && $d < $bd ? 1 : 0); | ||
| 284 | |||
| 285 | return $age; | ||
| 286 | } | ||
| 287 | |||
| 288 | no Moose; | ||
| 289 | __PACKAGE__->meta->make_immutable; | ||
| 290 | |||
| 291 | 1; | ||
| 292 | __END__ | ||
| 293 | |||
| 294 | =head1 NAME | ||
| 295 | |||
| 296 | WWW::Contact::GoogleContactsAPI - Get contacts via Google Contacts Data API | ||
| 297 | |||
| 298 | =head1 SYNOPSIS | ||
| 299 | |||
| 300 | use WWW::Contact; | ||
| 301 | use Data::Dumper; | ||
| 302 | |||
| 303 | my $wc = WWW::Contact->new(); | ||
| 304 | my @contacts = $wc->get_contacts('itsa@gmail.com', 'password'); | ||
| 305 | my $errstr = $wc->errstr; | ||
| 306 | die $errstr if ($errstr); | ||
| 307 | print Dumper(\@contacts); | ||
| 308 | |||
| 309 | =head1 DESCRIPTION | ||
| 310 | |||
| 311 | WWW::Contact::GoogleContactsAPI uses the Google Contacts Data API | ||
| 312 | (L<http://code.google.com/apis/contacts/docs/3.0/reference.html>) | ||
| 313 | to retrieve all a user's contacts. | ||
| 314 | |||
| 315 | =head1 METHODS | ||
| 316 | |||
| 317 | =over 4 | ||
| 318 | |||
| 319 | =item C<< $wc->get_contacts($email, $password) >> | ||
| 320 | |||
| 321 | Login to Google using C<email> and C<password>. Fetch all the given | ||
| 322 | user's contacts. Return them as a list or, in a scalar context, as a | ||
| 323 | reference to a list. Each element of the result represents one | ||
| 324 | contact as a reference to a hash with one or more of the following | ||
| 325 | keys (all fields are optional): | ||
| 326 | |||
| 327 | =over 4 | ||
| 328 | |||
| 329 | =item C<name> | ||
| 330 | |||
| 331 | The contact's main name as a single string. This may | ||
| 332 | be a person's full name or the name of an organisation. | ||
| 333 | |||
| 334 | =item C<family_name> | ||
| 335 | |||
| 336 | A person's family name (surname or last name), | ||
| 337 | usually shared with one or both parents. | ||
| 338 | |||
| 339 | =item C<given_name> | ||
| 340 | |||
| 341 | A person's given name (christian name or first name). | ||
| 342 | |||
| 343 | =item C<nickname> | ||
| 344 | |||
| 345 | An alternative name or alias for the contact. | ||
| 346 | |||
| 347 | =item C<emails> | ||
| 348 | |||
| 349 | An ordered list of references to pairs, C<< [$address, $type] >>, | ||
| 350 | where C<$type> is C<"home">, C<"work">, C<"other"> or some custom | ||
| 351 | label and C<$address> is the corresponding e-mail address. | ||
| 352 | |||
| 353 | =item C<email> | ||
| 354 | |||
| 355 | The first address in C<emails>, if any. | ||
| 356 | Deprecated: use C<< $contact->{emails}->[0][0] >>. | ||
| 357 | |||
| 358 | =item C<addresses> | ||
| 359 | |||
| 360 | As for C<emails> but C<$address> is a | ||
| 361 | postal addresses, given as a single string. | ||
| 362 | |||
| 363 | =item C<birthday> | ||
| 364 | |||
| 365 | The date of birth in the form C<YYYY-MM-DD> or C<--MM-DD>. | ||
| 366 | |||
| 367 | =item C<age> | ||
| 368 | |||
| 369 | The age in years today, calculated from the date of birth. | ||
| 370 | |||
| 371 | =item C<events> | ||
| 372 | |||
| 373 | An ordered list of references to pairs, C<[$date, $type]>, where | ||
| 374 | C<$date> is the date of the event in the form C<YYYY-MM-DD> and | ||
| 375 | C<$type> is the type of event, e.g. "anniversary". | ||
| 376 | |||
| 377 | =item C<instant_messengers> | ||
| 378 | |||
| 379 | An ordered list of references to pairs, C<[$protocol, | ||
| 380 | $address]>, where C<$protocol> is one of C<gtalk>, | ||
| 381 | C<jabber>, C<msn>, C<skype>, C<yahoo> and possibly others. | ||
| 382 | |||
| 383 | =item C<phones> | ||
| 384 | |||
| 385 | An ordered list of references to pairs, C<[$number, $type]>, where | ||
| 386 | C<$type> is one of C<home>, C<work>, C<mobile> and possibly other | ||
| 387 | values. C<$number> may include punctuation and comments. | ||
| 388 | |||
| 389 | =item C<updated> | ||
| 390 | |||
| 391 | The time when this contact was last modified in the | ||
| 392 | form C<YYYY-MM-DDTHH:MM:SS.SSSZ> where C<T> is a | ||
| 393 | literal "T", C<SS.SSS> is seconds and milliseconds | ||
| 394 | as a decimal and C<Z> indicates the UTC time-zone. | ||
| 395 | |||
| 396 | =item C<notes> | ||
| 397 | |||
| 398 | Arbitrary text. | ||
| 399 | |||
| 400 | =item C<child>, C<spouse>, etc. | ||
| 401 | |||
| 402 | Relations are returned as references to | ||
| 403 | lists of names (even mother and father). | ||
| 404 | |||
| 405 | =item C<organisations> | ||
| 406 | |||
| 407 | An ordered list of reference to pairs, C<[$organisation, | ||
| 408 | $type]>, where C<$organisation> is a body this contact is | ||
| 409 | associated with and C<$type> is the type of relationship. | ||
| 410 | |||
| 411 | =item C<groups> | ||
| 412 | |||
| 413 | A reference to a list of group names | ||
| 414 | |||
| 415 | =item C<> | ||
| 416 | |||
| 417 | =item Custom fields | ||
| 418 | |||
| 419 | Any custom fields not recognised by the module are | ||
| 420 | returned in the output hash with a simple string value. | ||
| 421 | |||
| 422 | =back | ||
| 423 | |||
| 424 | =back | ||
| 425 | |||
| 426 | =head1 SEE ALSO | ||
| 427 | |||
| 428 | L<WWW::Contact>, L<WWW::Mechanize>, L<Net::Google::AuthSub> | ||
| 429 | |||
| 430 | =head1 AUTHORS | ||
| 431 | |||
| 432 | Fayland Lam, C<< <fayland at gmail.com> >> | ||
| 433 | Denis Howe, C<< denis.howe=gc@gmail.com >> | ||
| 434 | |||
| 435 | =head1 COPYRIGHT & LICENSE | ||
| 436 | |||
| 437 | Copyright 2008 Fayland Lam, all rights reserved. | ||
| 438 | |||
| 439 | This program is free software; you can redistribute it and/or modify it | ||
| 440 | under the same terms as Perl itself. | ||
| 441 | |||
| 442 | =cut |