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