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 |
has 'path' => ( |
23 |
is => 'ro', |
24 |
isa => 'Str', |
25 |
default => sub { |
26 |
my $self = shift; |
27 |
'var/delicious/' . $self->username; |
28 |
} |
29 |
); |
30 |
|
31 |
sub data { |
32 |
my $self = shift; |
33 |
|
34 |
if ( my $posts = $self->load( $self->path ) ) { |
35 |
return $posts; |
36 |
} |
37 |
|
38 |
my $ua = LWP::UserAgent->new; |
39 |
my $req = HTTP::Request->new(GET => 'https://api.del.icio.us/v1/posts/all'); |
40 |
$req->authorization_basic( $self->username, $self->password ); |
41 |
my $content = $ua->request($req)->as_string; |
42 |
warn "# got ",length($content), " bytes"; |
43 |
|
44 |
# strip headers |
45 |
$content =~ s/^.+(<\?xml)/$1/s; |
46 |
warn substr($content,0,100), ' ... ', substr($content,-100); |
47 |
my $posts = XMLin( $content ); |
48 |
warn dump( $posts ); |
49 |
$self->store( $self->path . '.yml', $posts ); |
50 |
return $posts; |
51 |
} |
52 |
|
53 |
1; |