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

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

add variable interval time
add config for scraping big numbers

  • 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_users} = $delicious_users;
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 wlil
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 RSS feeds API to get the
80 tags 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://del.icio.us/help/>
92
93 =cut
Note: See TracBrowser for help on using the browser.