-
Notifications
You must be signed in to change notification settings - Fork 4
/
google2abook.pl
executable file
·34 lines (28 loc) · 1.09 KB
/
google2abook.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env perl
use local::lib;
use Modern::Perl;
use WWW::Google::Contacts;
use Net::Netrc;
use Class::CSV;
use System::Command;
use File::Slurp qw(write_file overwrite_file);
use File::Temp;
my $addressbook = qq[$ENV{HOME}/Dropbox/dot-files/abook/addressbook];
my $abook_cmd = q{abook --convert --outformat abook --informat csv --infile };
my @accounts = (qw(google.com umich.google.com));
my $temp_fh = File::Temp->new();
my $csv = Class::CSV->new(fields => [qw(name emails)]);
foreach my $account (@accounts) {
my $mach = Net::Netrc->lookup($account);
my $google = WWW::Google::Contacts->new(username => $mach->login, password => $mach->password);
my $contacts = $google->contacts();
while (my $contact = $contacts->next) {
next if not $contact->email;
#next if not $contact->full_name;
$csv->add_line([$contact->full_name||" ", join(q{,}, (map {$_->value} @{$contact->email}))]);
}
}
write_file($temp_fh->filename, $csv->string);
my $abook_fh = System::Command->new($abook_cmd . $temp_fh->filename)->stdout;
overwrite_file($addressbook, <$abook_fh>);
$abook_fh->close;