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

Revision 1945 (checked in by otsune, 13 years ago)

Change from RSS scrape to JSON.

  • 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://badges.del.icio.us/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     $self->log(info => "set delicious_users to $delicious_users");
52 }
53
54 sub rate_of_color {
55     my $n = shift;
56     return 100 unless $n;
57     int(log($n) / log(0.82) + 100);
58 }
59
60 1;
61
62 __END__
63
64 =head1 NAME
65
66 Plagger::Plugin::Filter::Delicious - Fetch tags and users count from del.icio.us
67
68 =head1 SYNOPSIS
69
70   - module: Filter::Delicious
71
72 =head1 DESCRIPTION
73
74 B<Note: this module is mostly untested and written just for a proof of
75 concept. If you run this on your box with real feeds, del.icio.us will
76 be likely to ban your IP. See http://del.icio.us/help/ for details.>
77
78 This plugin queries del.icio.us using its JSON API to get the tags
79 people added to the entries, and how many people bookmarked them.
80
81 Users count is stored in C<delicious_users> metadata of
82 Plagger::Entry, so that other plugins and smartfeeds can make use of.
83
84 =head1 AUTHOR
85
86 Tatsuhiko Miyagawa
87
88 =head1 SEE ALSO
89
90 L<Plagger>, L<http://del.icio.us/help/>, L<http://del.icio.us/help/json/url>
91
92 =cut
Note: See TracBrowser for help on using the browser.