1 |
package Frey::DelIcioUs; |
2 |
use Moose; |
3 |
|
4 |
with 'Frey::Storage'; |
5 |
|
6 |
use LWP::UserAgent; |
7 |
use XML::Simple; |
8 |
use Data::Dump qw/dump/; |
9 |
|
10 |
has 'username' => ( |
11 |
is => 'rw', |
12 |
isa => 'Str', |
13 |
required => 1, |
14 |
); |
15 |
|
16 |
has 'password' => ( |
17 |
is => 'ro', |
18 |
isa => 'Str', |
19 |
required => 1, |
20 |
); |
21 |
|
22 |
sub path { |
23 |
my $self = shift; |
24 |
'var/delicious/' . $self->username; |
25 |
} |
26 |
|
27 |
sub data { |
28 |
my $self = shift; |
29 |
|
30 |
if ( my $posts = $self->load( $self->path ) ) { |
31 |
return $posts; |
32 |
} |
33 |
|
34 |
my $ua = LWP::UserAgent->new; |
35 |
my $req = HTTP::Request->new(GET => 'https://api.del.icio.us/v1/posts/all'); |
36 |
$req->authorization_basic( $self->username, $self->password ); |
37 |
my $content = $ua->request($req)->as_string; |
38 |
warn "# got ",length($content), " bytes"; |
39 |
|
40 |
# strip headers |
41 |
$content =~ s/^.+(<\?xml)/$1/s; |
42 |
warn substr($content,0,100), ' ... ', substr($content,-100); |
43 |
my $posts = XMLin( $content ); |
44 |
warn dump( $posts ); |
45 |
$self->store( $self->path . '.yml', $posts ); |
46 |
return $posts; |
47 |
} |
48 |
|
49 |
1; |