| blib/lib/WWW/Google/Notebook.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 30 | 125 | 24.0 |
| branch | 0 | 24 | 0.0 |
| condition | 0 | 6 | 0.0 |
| subroutine | 10 | 28 | 35.7 |
| pod | 5 | 5 | 100.0 |
| total | 45 | 188 | 23.9 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package WWW::Google::Notebook; | ||||||
| 2 | 2 | 2 | 165440 | use strict; | |||
| 2 | 5 | ||||||
| 2 | 1726 | ||||||
| 3 | 2 | 2 | 16 | use warnings; | |||
| 2 | 5 | ||||||
| 2 | 87 | ||||||
| 4 | 2 | 2 | 10 | use base qw(Class::Accessor::Fast); | |||
| 2 | 9 | ||||||
| 2 | 5353 | ||||||
| 5 | |||||||
| 6 | 2 | 2 | 8925 | use Carp; | |||
| 2 | 6 | ||||||
| 2 | 225 | ||||||
| 7 | 2 | 2 | 5273 | use URI; | |||
| 2 | 32016 | ||||||
| 2 | 77 | ||||||
| 8 | 2 | 2 | 22 | use URI::Escape (); | |||
| 2 | 4 | ||||||
| 2 | 42 | ||||||
| 9 | 2 | 2 | 41438 | use LWP::UserAgent; | |||
| 2 | 185212 | ||||||
| 2 | 97 | ||||||
| 10 | 2 | 2 | 1977 | use WWW::Google::Notebook::Note; | |||
| 2 | 6 | ||||||
| 2 | 18 | ||||||
| 11 | 2 | 2 | 1473 | use WWW::Google::Notebook::Notebook; | |||
| 2 | 5 | ||||||
| 2 | 15 | ||||||
| 12 | |||||||
| 13 | our $VERSION = '0.01'; | ||||||
| 14 | |||||||
| 15 | __PACKAGE__->mk_accessors(qw/username password/); | ||||||
| 16 | |||||||
| 17 | my $BaseURI = 'http://www.google.com/notebook/'; | ||||||
| 18 | |||||||
| 19 | sub new { | ||||||
| 20 | 0 | 0 | 1 | my ($class, %param) = @_; | |||
| 21 | 0 | 0 | bless { | ||||
| 0 | |||||||
| 22 | username => $param{username} || '', | ||||||
| 23 | password => $param{password} || '', | ||||||
| 24 | }, $class; | ||||||
| 25 | } | ||||||
| 26 | |||||||
| 27 | sub ua { | ||||||
| 28 | 0 | 0 | 1 | my $self = shift; | |||
| 29 | 0 | 0 | if (@_) { | ||||
| 30 | 0 | $self->{ua} = shift; | |||||
| 31 | } else { | ||||||
| 32 | 0 | 0 | $self->{ua} and return $self->{ua}; | ||||
| 33 | 0 | $self->{ua} = LWP::UserAgent->new; | |||||
| 34 | 0 | $self->{ua}->agent(__PACKAGE__."/$VERSION"); | |||||
| 35 | } | ||||||
| 36 | 0 | $self->{ua}; | |||||
| 37 | } | ||||||
| 38 | |||||||
| 39 | sub login { | ||||||
| 40 | 0 | 0 | 1 | my ($self, %param) = @_; | |||
| 41 | 0 | my $uri = URI->new('https://www.google.com/accounts/ServiceLoginAuth'); | |||||
| 42 | 0 | $uri->query_form( | |||||
| 43 | Email => $self->username, | ||||||
| 44 | Passwd => $self->password, | ||||||
| 45 | service => 'notebook', | ||||||
| 46 | continue => $BaseURI, | ||||||
| 47 | source => __PACKAGE__."/$VERSION", | ||||||
| 48 | ); | ||||||
| 49 | 0 | my $res = $self->ua->post($uri); | |||||
| 50 | 0 | 0 | croak($res->status_line) if $res->is_error; | ||||
| 51 | 0 | my $cookie = $res->header('Set-Cookie'); | |||||
| 52 | 0 | $self->ua->default_header(Cookie => $cookie); | |||||
| 53 | 0 | $res = $self->ua->post($BaseURI.'token?&pv=2'); | |||||
| 54 | 0 | 0 | croak($res->status_line) if $res->is_error; | ||||
| 55 | 0 | my ($token) = $res->content =~ m!/\*(.*)\*/!; | |||||
| 56 | 0 | $self->{_token} = $token; | |||||
| 57 | 0 | return 1; | |||||
| 58 | } | ||||||
| 59 | |||||||
| 60 | sub notebooks { | ||||||
| 61 | 0 | 0 | 1 | my $self = shift; | |||
| 62 | 0 | my $uri = sprintf( | |||||
| 63 | $BaseURI.'read?pv=2&ident=fp&tok=%s&cmd=u&zx=%d', | ||||||
| 64 | $self->{_token}, | ||||||
| 65 | time, | ||||||
| 66 | ); | ||||||
| 67 | 0 | my $res = $self->ua->get($uri); | |||||
| 68 | 0 | 0 | croak($res->status_line) if $res->is_error; | ||||
| 69 | 0 | my $notebooks = $self->_parse($res->content); | |||||
| 70 | 0 | $notebooks; | |||||
| 71 | } | ||||||
| 72 | |||||||
| 73 | sub add_notebook { | ||||||
| 74 | 0 | 0 | 1 | my ($self, $title) = @_; | |||
| 75 | 0 | my $uri = sprintf( | |||||
| 76 | $BaseURI.'write?pv=2&ident=fp&tok=%s&cmd=b&contents=%s', | ||||||
| 77 | $self->{_token}, | ||||||
| 78 | _uri_escape($title), | ||||||
| 79 | ); | ||||||
| 80 | 0 | my $res = $self->ua->post($uri); | |||||
| 81 | 0 | 0 | croak($res->status_line) if $res->is_error; | ||||
| 82 | 0 | my $notebook = $self->_parse($res->content); | |||||
| 83 | 0 | $notebook; | |||||
| 84 | } | ||||||
| 85 | |||||||
| 86 | sub _delete_notebook { | ||||||
| 87 | 0 | 0 | my ($self, $notebook) = @_; | ||||
| 88 | 0 | my $uri = sprintf( | |||||
| 89 | $BaseURI.'write?pv=2&ident=fp&tok=%s&cmd=trshn&nbid=%s', | ||||||
| 90 | $self->{_token}, | ||||||
| 91 | $notebook->id, | ||||||
| 92 | ); | ||||||
| 93 | 0 | my $res = $self->ua->post($uri); | |||||
| 94 | 0 | 0 | croak($res->status_line) if $res->is_error; | ||||
| 95 | 0 | undef %$notebook; | |||||
| 96 | 0 | bless $notebook, 'WWW::Google::Notebook::Object::Has::Been::Deleted'; | |||||
| 97 | 0 | 1; | |||||
| 98 | } | ||||||
| 99 | |||||||
| 100 | sub _update_notebook { | ||||||
| 101 | 0 | 0 | my ($self, $notebook) = @_; | ||||
| 102 | 0 | my $uri = sprintf( | |||||
| 103 | $BaseURI.'write?pv=2&ident=fp&tok=%s&cmd=b&nbid=%s&contents=%s', | ||||||
| 104 | $self->{_token}, | ||||||
| 105 | $notebook->id, | ||||||
| 106 | _uri_escape($notebook->title), | ||||||
| 107 | ); | ||||||
| 108 | 0 | my $res = $self->ua->post($uri); | |||||
| 109 | 0 | 0 | croak($res->status_line) if $res->is_error; | ||||
| 110 | 0 | $notebook = $self->_parse($res->content); | |||||
| 111 | 0 | 1; | |||||
| 112 | } | ||||||
| 113 | |||||||
| 114 | sub _notes { | ||||||
| 115 | 0 | 0 | my ($self, $notebook) = @_; | ||||
| 116 | 0 | my $uri = sprintf( | |||||
| 117 | $BaseURI.'read?pv=2&ident=fp&tok=%s&cmd=b&nbid=%s&zx=%d', | ||||||
| 118 | $self->{_token}, | ||||||
| 119 | $notebook->id, | ||||||
| 120 | time, | ||||||
| 121 | ); | ||||||
| 122 | 0 | my $res = $self->ua->get($uri); | |||||
| 123 | 0 | 0 | croak($res->status_line) if $res->is_error; | ||||
| 124 | 0 | print $res->content; | |||||
| 125 | 0 | $notebook = $self->_parse($res->content); | |||||
| 126 | 0 | my @notes; | |||||
| 127 | 0 | for my $note (@{$notebook->{_notes}}) { | |||||
| 0 | |||||||
| 128 | 0 | $note->notebook($notebook); | |||||
| 129 | 0 | push @notes, $note; | |||||
| 130 | } | ||||||
| 131 | 0 | undef $notebook->{_notes}; | |||||
| 132 | 0 | \@notes; | |||||
| 133 | } | ||||||
| 134 | |||||||
| 135 | sub _add_note { | ||||||
| 136 | 0 | 0 | my ($self, $notebook, $content) = @_; | ||||
| 137 | 0 | $content =~ s/\r?\n/ /g; |
|||||
| 138 | 0 | my $uri = sprintf( | |||||
| 139 | $BaseURI.'write?pv=2&ident=fp&tok=%s&cmd=n&nbid=%s&contents=%s&qurl=null&nmeth=fp', | ||||||
| 140 | $self->{_token}, | ||||||
| 141 | $notebook->id, | ||||||
| 142 | _uri_escape($content), | ||||||
| 143 | ); | ||||||
| 144 | 0 | my $res = $self->ua->post($uri); | |||||
| 145 | 0 | 0 | croak($res->status_line) if $res->is_error; | ||||
| 146 | 0 | my $note = $self->_parse($res->content); | |||||
| 147 | 0 | $note->notebook($notebook); | |||||
| 148 | 0 | $note; | |||||
| 149 | } | ||||||
| 150 | |||||||
| 151 | sub _delete_note { | ||||||
| 152 | 0 | 0 | my ($self, $note) = @_; | ||||
| 153 | 0 | my $uri = sprintf( | |||||
| 154 | $BaseURI.'write?pv=2&ident=fp&tok=%s&cmd=trsh&nid=%s&nbid=%s', | ||||||
| 155 | $self->{_token}, | ||||||
| 156 | $note->id, | ||||||
| 157 | $note->notebook->id, | ||||||
| 158 | ); | ||||||
| 159 | 0 | my $res = $self->ua->post($uri); | |||||
| 160 | 0 | 0 | croak($res->status_line) if $res->is_error; | ||||
| 161 | 0 | undef %$note; | |||||
| 162 | 0 | bless $note, 'WWW::Google::Notebook::Object::Has::Been::Deleted'; | |||||
| 163 | 0 | 1; | |||||
| 164 | } | ||||||
| 165 | |||||||
| 166 | sub _update_note { | ||||||
| 167 | 0 | 0 | my ($self, $note) = @_; | ||||
| 168 | 0 | my $uri = sprintf( | |||||
| 169 | $BaseURI.'write?pv=2&ident=fp&tok=%s&cmd=n&nbid=%s&nid=%s&contents=%s&qurl=null', | ||||||
| 170 | $self->{_token}, | ||||||
| 171 | $note->notebook->id, | ||||||
| 172 | $note->id, | ||||||
| 173 | _uri_escape($note->content), | ||||||
| 174 | ); | ||||||
| 175 | 0 | my $res = $self->ua->post($uri); | |||||
| 176 | 0 | 0 | croak($res->status_line) if $res->is_error; | ||||
| 177 | 0 | $note = $self->_parse($res->content); | |||||
| 178 | 0 | 1; | |||||
| 179 | } | ||||||
| 180 | |||||||
| 181 | sub _parse { | ||||||
| 182 | 0 | 0 | my ($self, $json) = @_; | ||||
| 183 | 2 | 2 | 4216 | no warnings 'once'; | |||
| 2 | 5 | ||||||
| 2 | 976 | ||||||
| 184 | 0 | 0 | local *F = sub {}; | ||||
| 0 | |||||||
| 185 | 0 | 0 | local *U = sub { $_[0] }; | ||||
| 0 | |||||||
| 186 | local *B = sub { | ||||||
| 187 | 0 | 0 | 0 | WWW::Google::Notebook::Notebook->new({ | |||
| 188 | id => $_[0], | ||||||
| 189 | title => $_[1], | ||||||
| 190 | api => $self, | ||||||
| 191 | _notes => $_[11]->[0] || [], | ||||||
| 192 | }); | ||||||
| 193 | 0 | }; | |||||
| 194 | local *N = sub { | ||||||
| 195 | 0 | 0 | WWW::Google::Notebook::Note->new({ | ||||
| 196 | id => $_[0], | ||||||
| 197 | content => $_[1], | ||||||
| 198 | created_on => $_[5], | ||||||
| 199 | }); | ||||||
| 200 | 0 | }; | |||||
| 201 | 0 | 0 | local *S = sub { $_[3] }; | ||||
| 0 | |||||||
| 202 | 0 | eval $json; | |||||
| 203 | } | ||||||
| 204 | |||||||
| 205 | sub _uri_escape { | ||||||
| 206 | 0 | 0 | my $val = shift; | ||||
| 207 | 0 | $val =~ s/\r?\n/ /g; |
|||||
| 208 | 0 | URI::Escape::uri_escape($val); | |||||
| 209 | } | ||||||
| 210 | |||||||
| 211 | 1; | ||||||
| 212 | __END__ |