package Plagger::Plugin::Filter::Delicious; use strict; use base qw( Plagger::Plugin ); use Digest::MD5 qw(md5_hex); use Plagger::UserAgent; use JSON::Syck; sub register { my($self, $context) = @_; $context->register_hook( $self, 'update.feed.fixup' => \&update, ); } sub update { my($self, $context, $args) = @_; my @permalink = map $_->permalink, $args->{feed}->entries; my $interval = $self->conf->{interval} || 1; my $json_url = "http://feeds.delicious.com/feeds/json/url/data?hash="; my $ua = Plagger::UserAgent->new; my %delicious = {}; while (my @links = splice(@permalink, 0, 15)) { $context->log( info => 'Requesting json call to delicious with ' . scalar(@links) . ' link(s)' ); my $url = $json_url . (join '&hash=', map { md5_hex($_) } @links); $self->log(info => "Going to fetch $url"); my $res = $ua->fetch($url); if ($res->is_error) { $context->log(warn => "Fetch error $url: " . $res->http_response->message); next; } my $data = JSON::Syck::Load($res->content); unless (ref $data eq 'ARRAY') { $context->log(warn => "json parse error: $data"); next; } $context->log( info => 'json request success.' ); for my $h ( @{$data} ) { next if !$h || !$h->{url}; $h->{url} =~ s{\\/}{/}mosixg; $delicious{ $h->{url} } = $h; } sleep $interval if (@permalink); } for my $entry ($args->{feed}->entries) { next if (not defined(my $info = $delicious{$entry->permalink})); my $delicious_users = $info->{total_posts} || 0; $entry->meta->{delicious_rate} = rate_of_color($delicious_users); $entry->meta->{delicious_users} = $delicious_users; $entry->meta->{delicious_top_tags} = $info->{top_tags}; $entry->meta->{delicious_hash} = $info->{hash}; next if (!$info->{top_tags} || !(ref $info->{top_tags} eq 'HASH')); for my $tag ( keys %{$info->{top_tags}} ) { $entry->add_tag($tag); $self->log(debug => "add tag $tag"); } } } sub rate_of_color { my $n = shift; return 100 unless $n; int(log($n) / log(0.82) + 100); } 1; __END__ =head1 NAME Plagger::Plugin::Filter::Delicious - Fetch tags and users count from del.icio.us =head1 SYNOPSIS - module: Filter::Delicious =head1 DESCRIPTION B This plugin queries del.icio.us using its RSS feeds API to get the tags people added to the entries, and how many people bookmarked them. Users count is stored in C metadata of Plagger::Entry, so that other plugins and smartfeeds can make use of. =head1 AUTHOR Tatsuhiko Miyagawa =head1 SEE ALSO L, L L, L L, L =cut