diff -u Filter_org/Delicious.pm Filter/Delicious.pm --- Filter_org/Delicious.pm 2006-12-05 16:01:24.000000000 +0900 +++ Filter/Delicious.pm 2008-10-29 21:11:42.000000000 +0900 @@ -4,70 +4,68 @@ use Digest::MD5 qw(md5_hex); use Plagger::UserAgent; -use XML::Feed; +use JSON::Syck; sub register { my($self, $context) = @_; $context->register_hook( $self, - 'update.entry.fixup' => \&update, + 'update.feed.fixup' => \&update, ); } sub update { my($self, $context, $args) = @_; + my @permalink = map $_->permalink, $args->{feed}->entries; my $interval = $self->conf->{interval} || 1; - sleep $interval; - - my $md5 = md5_hex($args->{entry}->permalink); - my $url = "http://del.icio.us/rss/url/$md5"; - - $self->log(info => "Going to fetch $url"); - + my $json_url = "http://feeds.delicious.com/feeds/json/url/data?hash="; my $ua = Plagger::UserAgent->new; - my $res = $ua->fetch($url); - - if ($res->is_error) { - $self->log(error => "Fetch URL $url failed."); - return; - } - my $feed = XML::Feed->parse(\$res->content); + 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"); - unless ($feed) { - $context->log(warn => "Feed error $url: " . XML::Feed->errstr); - return; - } + my $res = $ua->fetch($url); + if ($res->is_error) { + $context->log(warn => "Fetch error $url: " . $res->http_response->message); + next; + } - for my $entry ($feed->entries) { - my @tag = split / /, ($entry->category || ''); - @tag or next; + my $data = JSON::Syck::Load($res->content); + unless (ref $data eq 'ARRAY') { + $context->log(warn => "json parse error: $data"); + next; + } - for my $tag (@tag) { - $args->{entry}->add_tag($tag); + $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 = $feed->entries; - if ($delicious_users >= 30 && $self->conf->{scrape_big_numbers}) { - my $url = "http://del.icio.us/url/$md5"; - $self->log(info => "users count is more than 30. Trying to scrape from $url."); - sleep $interval; - - my $res = $ua->fetch($url); + my $delicious_users = $info->{total_posts} || 0; - if ($res->is_error) { - $context->log(warn => "Fetch error $url: " . $res->http_response->message); - return; + $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"); } - - $delicious_users = - ( $res->content =~ m#