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

Revision 1345 (checked in by taro, 14 years ago)

store rate of background-color for Widget

  • 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 Digest::MD5 qw(md5_hex);
6 use URI;
7 use XML::Feed;
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     # xxx need cache & interval
21     my $interval = $self->conf->{interval} || 1;
22     sleep $interval;
23
24     my $md5  = md5_hex($args->{entry}->permalink);
25     my $url  = "http://del.icio.us/rss/url/$md5";
26     my $feed = XML::Feed->parse( URI->new($url) );
27
28     unless ($feed) {
29         $context->log(warn => "Feed error $url: " . XML::Feed->errstr);
30         return;
31     }
32
33     for my $entry ($feed->entries) {
34         my @tag = split / /, ($entry->category || '');
35            @tag or next;
36
37         for my $tag (@tag) {
38             $args->{entry}->add_tag($tag);
39         }
40     }
41
42     my $delicious_users = $feed->entries;
43     if ($delicious_users >= 30 && $self->conf->{scrape_big_numbers}) {
44         sleep $interval;
45
46         my $url = "http://del.icio.us/url/$md5";
47         my $ua  = Plagger::UserAgent->new;
48         my $res = $ua->fetch($url);
49
50         if ($res->is_error) {
51             $context->log(warn => "Fetch error $url: " . $res->http_response->message);
52             return;
53         }
54
55         $delicious_users =
56             ( $res->content =~ m#<h4[^>]*>[^<>]*this url has been saved by\D+(\d+)#s )[0];
57     }
58     $args->{entry}->meta->{delicious_rate} = rate_of_color($delicious_users);
59     $args->{entry}->meta->{delicious_users} = $delicious_users;
60 }
61
62 sub rate_of_color {
63     my $n = shift;
64     int(log($n) / log(0.82) + 100);
65 }
66
67 1;
68
69 __END__
70
71 =head1 NAME
72
73 Plagger::Plugin::Filter::Delicious - Fetch tags and users count from del.icio.us
74
75 =head1 SYNOPSIS
76
77   - module: Filter::Delicious
78
79 =head1 DESCRIPTION
80
81 B<Note: this module is mostly untested and written just for a proof of
82 concept. If you run this on your box with real feeds, del.icio.us wlil
83 be likely to ban your IP. See http://del.icio.us/help/ for details.>
84
85 This plugin queries del.icio.us using its RSS feeds API to get the
86 tags people added to the entries, and how many people bookmarked them.
87
88 Users count is stored in C<delicious_users> metadata of
89 Plagger::Entry, so that other plugins and smartfeeds can make use of.
90
91 =head1 AUTHOR
92
93 Tatsuhiko Miyagawa
94
95 =head1 SEE ALSO
96
97 L<Plagger>, L<http://del.icio.us/help/>
98
99 =cut
Note: See TracBrowser for help on using the browser.