root/trunk/plagger/lib/Plagger/Plugin/Filter/Delicious.pm

Revision 2056 (checked in by otsune, 11 years ago)

* Update API domain
* Add meta hash (via http://0-9.sakura.ne.jp/blog/archives/2008/09/15180112.html )

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin::Filter::Delicious;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use JSON::Syck;
6 use Digest::MD5 qw(md5_hex);
7 use Plagger::UserAgent;
8
9 sub register {
10     my($self, $context) = @_;
11     $context->register_hook(
12         $self,
13         'update.entry.fixup' => \&update,
14     );
15 }
16
17 sub update {
18     my($self, $context, $args) = @_;
19
20     my $interval = $self->conf->{interval} || 1;
21     sleep $interval;
22
23     my $md5  = md5_hex($args->{entry}->permalink);
24     my $url = "http://feeds.delicious.com/feeds/json/url/data?hash=$md5";
25
26     $self->log(info => "Going to fetch $url");
27
28     my $ua  = Plagger::UserAgent->new;
29     my $res = $ua->fetch($url);
30
31     if ($res->is_error) {
32         $self->log(error => "Fetch URL $url failed.");
33         return;
34     }
35    
36     my $data = JSON::Syck::Load($res->content);
37     unless (ref $data eq 'ARRAY') {
38         $self->log(error => "json parse error: $data");
39         return;
40     }
41     my $h = @{$data}[0];
42
43     for my $tag (keys %{$h->{top_tags}}) {
44         $args->{entry}->add_tag($tag);
45         $self->log(debug => "add tag $tag");
46     }
47
48     my $delicious_users = $h->{total_posts} || 0;
49     $args->{entry}->meta->{delicious_rate} = rate_of_color($delicious_users);
50     $args->{entry}->meta->{delicious_users} = $delicious_users;
51     $args->{entry}->meta->{delicious_hash} = $h->{hash};
52     $self->log(info => "set delicious_users to $delicious_users");
53 }
54
55 sub rate_of_color {
56     my $n = shift;
57     return 100 unless $n;
58     int(log($n) / log(0.82) + 100);
59 }
60
61 1;
62
63 __END__
64
65 =head1 NAME
66
67 Plagger::Plugin::Filter::Delicious - Fetch tags and users count from del.icio.us
68
69 =head1 SYNOPSIS
70
71   - module: Filter::Delicious
72
73 =head1 DESCRIPTION
74
75 B<Note: this module is mostly untested and written just for a proof of
76 concept. If you run this on your box with real feeds, del.icio.us will
77 be likely to ban your IP. See http://del.icio.us/help/ for details.>
78
79 This plugin queries del.icio.us using its JSON API to get the tags
80 people added to the entries, and how many people bookmarked them.
81
82 Users count is stored in C<delicious_users> metadata of
83 Plagger::Entry, so that other plugins and smartfeeds can make use of.
84
85 =head1 AUTHOR
86
87 Tatsuhiko Miyagawa
88
89 =head1 SEE ALSO
90
91 L<Plagger>, L<http://delicious.com/help/api>
92 L<http://delicious.com/help/feeds/>, L<http://delicious.com/help/html>
93
94 =cut
Note: See TracBrowser for help on using the browser.