| 1 | package Net::Google::Spreadsheets; |
|---|
| 2 | use Moose; |
|---|
| 3 | use Carp; |
|---|
| 4 | use Net::Google::AuthSub; |
|---|
| 5 | use Net::Google::Spreadsheets::Spreadsheet; |
|---|
| 6 | use LWP::UserAgent; |
|---|
| 7 | use XML::Atom; |
|---|
| 8 | use XML::Atom::Feed; |
|---|
| 9 | use URI; |
|---|
| 10 | use HTTP::Headers; |
|---|
| 11 | |
|---|
| 12 | our $VERSION = '0.01'; |
|---|
| 13 | |
|---|
| 14 | BEGIN { |
|---|
| 15 | $XML::Atom::DefaultVersion = 1; |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | has username => ( isa => 'Str', is => 'ro', required => 1 ); |
|---|
| 19 | has password => ( isa => 'Str', is => 'ro', required => 1 ); |
|---|
| 20 | |
|---|
| 21 | has source => ( |
|---|
| 22 | isa => 'Str', |
|---|
| 23 | is => 'ro', |
|---|
| 24 | required => 1, |
|---|
| 25 | default => sub { __PACKAGE__.'-'.$VERSION }, |
|---|
| 26 | ); |
|---|
| 27 | |
|---|
| 28 | has auth => ( |
|---|
| 29 | isa => 'Str', |
|---|
| 30 | is => 'rw', |
|---|
| 31 | required => 1, |
|---|
| 32 | lazy => 1, |
|---|
| 33 | default => sub { |
|---|
| 34 | my $self = shift; |
|---|
| 35 | my $authsub = Net::Google::AuthSub->new( |
|---|
| 36 | service => 'wise', |
|---|
| 37 | source => $self->source, |
|---|
| 38 | ); |
|---|
| 39 | my $res = $authsub->login( |
|---|
| 40 | $self->username, |
|---|
| 41 | $self->password, |
|---|
| 42 | ); |
|---|
| 43 | $res->is_success or return; |
|---|
| 44 | return $res->auth; |
|---|
| 45 | }, |
|---|
| 46 | ); |
|---|
| 47 | |
|---|
| 48 | has host => ( |
|---|
| 49 | isa => 'Str', |
|---|
| 50 | is => 'ro', |
|---|
| 51 | required => 1, |
|---|
| 52 | default => 'spreadsheets.google.com', |
|---|
| 53 | ); |
|---|
| 54 | |
|---|
| 55 | has ua => ( |
|---|
| 56 | isa => 'LWP::UserAgent', |
|---|
| 57 | is => 'ro', |
|---|
| 58 | required => 1, |
|---|
| 59 | lazy => 1, |
|---|
| 60 | default => sub { |
|---|
| 61 | my $self = shift; |
|---|
| 62 | my $ua = LWP::UserAgent->new( |
|---|
| 63 | agent => $self->source, |
|---|
| 64 | ); |
|---|
| 65 | $ua->default_headers( |
|---|
| 66 | HTTP::Headers->new( |
|---|
| 67 | Authorization => sprintf('GoogleLogin auth=%s', $self->auth), |
|---|
| 68 | GData_Version => 2, |
|---|
| 69 | ) |
|---|
| 70 | ); |
|---|
| 71 | return $ua; |
|---|
| 72 | } |
|---|
| 73 | ); |
|---|
| 74 | |
|---|
| 75 | sub spreadsheets { |
|---|
| 76 | my ($self, $cond) = @_; |
|---|
| 77 | my $feed = $self->feed( |
|---|
| 78 | sprintf ('http://%s/feeds/spreadsheets/private/full', $self->host), |
|---|
| 79 | $cond |
|---|
| 80 | ); |
|---|
| 81 | return [ map { |
|---|
| 82 | Net::Google::Spreadsheets::Spreadsheet->new( |
|---|
| 83 | atom => $_, |
|---|
| 84 | service => $self, |
|---|
| 85 | ) |
|---|
| 86 | } $feed->entries ]; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | sub spreadsheet { |
|---|
| 90 | my ($self, $args) = @_; |
|---|
| 91 | my $url = sprintf('http://%s/feeds/spreadsheets/', $self->host); |
|---|
| 92 | my $cond = $args->{title} ? |
|---|
| 93 | { |
|---|
| 94 | title => $args->{title}, |
|---|
| 95 | 'title-exact' => 'true' |
|---|
| 96 | } : {}; |
|---|
| 97 | my $feed = $self->feed( |
|---|
| 98 | $url."private/full", |
|---|
| 99 | $cond |
|---|
| 100 | ); |
|---|
| 101 | my $entry; |
|---|
| 102 | for ( $feed->entries ) { |
|---|
| 103 | my ($key) = $_->id =~ m{^$url(.+)$}; |
|---|
| 104 | $entry = $_ and last if $args->{title} && $_->title eq $args->{title}; |
|---|
| 105 | $entry = $_ and last if $args->{key} && $key eq $args->{key}; |
|---|
| 106 | } |
|---|
| 107 | $entry or return; |
|---|
| 108 | return Net::Google::Spreadsheets::Spreadsheet->new( |
|---|
| 109 | atom => $entry, |
|---|
| 110 | service => $self, |
|---|
| 111 | ); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | sub request { |
|---|
| 115 | my ($self, $args) = @_; |
|---|
| 116 | my $method = delete $args->{method}; |
|---|
| 117 | $method ||= $args->{content} ? 'POST' : 'GET'; |
|---|
| 118 | my $uri = URI->new($args->{'uri'}); |
|---|
| 119 | $uri->query_form($args->{query}) if $args->{query}; |
|---|
| 120 | my $req = HTTP::Request->new($method => "$uri"); |
|---|
| 121 | $req->content($args->{content}) if $args->{content}; |
|---|
| 122 | $req->header('Content-Type' => $args->{content_type}) if $args->{content_type}; |
|---|
| 123 | if ($args->{header}) { |
|---|
| 124 | while (my @pair = each %{$args->{header}}) { |
|---|
| 125 | $req->header(@pair); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | my $res = $self->ua->request($req); |
|---|
| 129 | # warn $res->request->as_string; |
|---|
| 130 | # warn $res->as_string; |
|---|
| 131 | unless ($res->is_success) { |
|---|
| 132 | warn $res->request->as_string; |
|---|
| 133 | warn $res->as_string; |
|---|
| 134 | croak "request failed: ",$res->code; |
|---|
| 135 | } |
|---|
| 136 | return $res; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | sub feed { |
|---|
| 140 | my ($self, $url, $query) = @_; |
|---|
| 141 | my $res = $self->request( |
|---|
| 142 | { |
|---|
| 143 | uri => $url, |
|---|
| 144 | query => $query || undef, |
|---|
| 145 | } |
|---|
| 146 | ); |
|---|
| 147 | return XML::Atom::Feed->new(\($res->content)); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | sub entry { |
|---|
| 151 | my ($self, $url, $query) = @_; |
|---|
| 152 | my $res = $self->request( |
|---|
| 153 | { |
|---|
| 154 | uri => $url, |
|---|
| 155 | query => $query || undef, |
|---|
| 156 | } |
|---|
| 157 | ); |
|---|
| 158 | return XML::Atom::Entry->new(\($res->content)); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | sub post { |
|---|
| 162 | my ($self, $url, $entry, $query) = @_; |
|---|
| 163 | my $res = $self->request( |
|---|
| 164 | { |
|---|
| 165 | uri => $url, |
|---|
| 166 | query => $query || undef, |
|---|
| 167 | content => $entry->as_xml, |
|---|
| 168 | content_type => 'application/atom+xml', |
|---|
| 169 | } |
|---|
| 170 | ); |
|---|
| 171 | return XML::Atom::Entry->new(\($res->content)); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | sub put { |
|---|
| 175 | my ($self, $args) = @_; |
|---|
| 176 | my $res = $self->request( |
|---|
| 177 | { |
|---|
| 178 | method => 'PUT', |
|---|
| 179 | uri => $args->{self}->editurl, |
|---|
| 180 | content => $args->{entry}->as_xml, |
|---|
| 181 | header => {'If-Match' => $args->{self}->etag}, |
|---|
| 182 | content_type => 'application/atom+xml', |
|---|
| 183 | } |
|---|
| 184 | ); |
|---|
| 185 | return XML::Atom::Entry->new(\($res->content)); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | 1; |
|---|
| 189 | __END__ |
|---|
| 190 | |
|---|
| 191 | =head1 NAME |
|---|
| 192 | |
|---|
| 193 | Net::Google::Spreadsheets - A Perl module for using Google Spreadsheets API. |
|---|
| 194 | |
|---|
| 195 | =head1 SYNOPSIS |
|---|
| 196 | |
|---|
| 197 | use Net::Google::Spreadsheets; |
|---|
| 198 | |
|---|
| 199 | my $api = Net::Google::Spreadsheets->new; |
|---|
| 200 | my $res = $api->login( |
|---|
| 201 | { |
|---|
| 202 | username => 'myname@gmail.com', |
|---|
| 203 | password => 'mypassword' |
|---|
| 204 | } |
|---|
| 205 | ); |
|---|
| 206 | |
|---|
| 207 | my @spreadsheets = $api->list(); |
|---|
| 208 | |
|---|
| 209 | my $spreadsheet = $api->spreadsheet('pZV-pns_sm9PtH2WowhU2Ew'); |
|---|
| 210 | my $worksheet = $spreadsheet->worksheet(1); |
|---|
| 211 | |
|---|
| 212 | my @fields = $worksheet->fields(); |
|---|
| 213 | |
|---|
| 214 | my $inserted_row = $worksheet->insert( |
|---|
| 215 | { |
|---|
| 216 | name => 'danjou', |
|---|
| 217 | } |
|---|
| 218 | ); |
|---|
| 219 | |
|---|
| 220 | my @rows = $worksheet->rows; |
|---|
| 221 | |
|---|
| 222 | my $row = $worksheet->row(1); |
|---|
| 223 | |
|---|
| 224 | $row->update( |
|---|
| 225 | { |
|---|
| 226 | nick => 'lopnor', |
|---|
| 227 | mail => 'nobuo.danjou@gmail.com', |
|---|
| 228 | } |
|---|
| 229 | ); |
|---|
| 230 | |
|---|
| 231 | =head1 DESCRIPTION |
|---|
| 232 | |
|---|
| 233 | Net::Google::Spreadsheets is a Perl module for using Google Spreadsheets API. |
|---|
| 234 | |
|---|
| 235 | =head1 AUTHOR |
|---|
| 236 | |
|---|
| 237 | Nobuo Danjou E<lt>nobuo.danjou@gmail.comE<gt> |
|---|
| 238 | |
|---|
| 239 | =head1 SEE ALSO |
|---|
| 240 | |
|---|
| 241 | =head1 LICENSE |
|---|
| 242 | |
|---|
| 243 | This library is free software; you can redistribute it and/or modify |
|---|
| 244 | it under the same terms as Perl itself. |
|---|
| 245 | |
|---|
| 246 | =cut |
|---|