root/trunk/plagger/lib/Plagger/Plugin/Subscription/Bloglines.pm

Revision 209 (checked in by miyagawa, 14 years ago)

Bloglines: fix method signature

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin::Subscription::Bloglines;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 our $VERSION = '0.10';
6 use WebService::Bloglines;
7
8 sub register {
9     my($self, $context) = @_;
10
11     $self->init_bloglines();
12
13     if ($self->conf->{no_sync_api}) {
14         $context->register_hook(
15             $self,
16             'subscription.load' => \&getsubs,
17         );
18     } else {
19         $context->register_hook(
20             $self,
21             'subscription.load' => \&notifier,
22             'aggregator.aggregate.bloglines' => \&sync,
23         );
24     }
25 }
26
27 sub getsubs {
28     my($self, $context) = @_;
29     my $subscription = $self->{bloglines}->listsubs();
30
31     for my $folder ($subscription->folders) {
32         $self->add_subscription($subscription, $folder->{BloglinesSubId}, $folder->{title});
33     }
34
35     $self->add_subscription($subscription, 0);
36 }
37
38 sub add_subscription {
39     my($self, $subscription, $subid, $title) = @_;
40
41     my @feeds = $subscription->feeds_in_folder($subid);
42     for my $source (@feeds) {
43         my $feed = Plagger::Feed->new;
44         $feed->title($source->{title});
45         $feed->link($source->{htmlUrl});
46         $feed->url($source->{xmlUrl} );
47         $feed->tags([ $title ]) if $title;
48         Plagger->context->subscription->add($feed);
49     }
50 }
51
52 sub init_bloglines {
53     my $self = shift;
54     $self->{bloglines} = WebService::Bloglines->new(
55         username => $self->conf->{username},
56         password => $self->conf->{password},
57     );
58 }
59
60 sub notifier {
61     my($self, $context) = @_;
62
63     my $count = $self->{bloglines}->notify();
64     $context->log(info => "You have $count unread item(s) on Bloglines.");
65     if ($count) {
66         my $feed = Plagger::Feed->new;
67         $feed->type('bloglines');
68         $context->subscription->add($feed);
69
70         if ($self->conf->{fetch_folders}) {
71             # TODO: cache it!
72             $self->fetch_folders($context);
73         }
74     }
75 }
76
77 sub fetch_folders {
78     my($self, $context) = @_;
79
80     $self->{folders} = {};
81     $context->log(info => "call Bloglines listsubs API to get folder structure");
82
83     my $subscription = $self->{bloglines}->listsubs();
84
85     for my $folder ($subscription->folders) {
86         my @feeds = $subscription->feeds_in_folder($folder->{BloglinesSubId});
87         for my $feed (@feeds) {
88             # BloglinesSubId is different from bloglines:siteid. Don't use it
89             $self->{folders}->{$feed->{htmlUrl}} = $folder->{title};
90             $context->log(debug => "$feed->{htmlUrl}: $folder->{title}");
91         }
92     }
93 }
94
95 sub sync {
96     my($self, $context, $args) = @_;
97
98     my $mark_read = $self->conf->{mark_read};
99        $mark_read = 1 unless defined $mark_read;
100
101     my @updates = $self->{bloglines}->getitems(0, $mark_read);
102     $context->log(info => scalar(@updates) . " feed(s) updated.");
103
104     for my $update (@updates) {
105         my $source = $update->feed;
106
107         my $feed = Plagger::Feed->new;
108         $feed->type('bloglines');
109         $feed->title($source->{title});
110         $feed->link($source->{link});
111         $feed->image($source->{image});
112         $feed->description($source->{description});
113         $feed->language($source->{language});
114         $feed->author($source->{webmaster});
115         $feed->meta->{bloglines_id} = $source->{bloglines}->{siteid};
116
117         # under fetch_folders option, set folder as tags to feeds
118         if (my $folder = $self->{folders}->{$feed->link}) {
119             $feed->tags([ $folder ]);
120         }
121
122         $feed->source_xml($update->{_xml});
123
124         for my $item ( $update->items ) {
125             my $entry = Plagger::Entry->new;
126
127             $entry->title($item->{title});
128             $entry->author($item->{dc}->{creator});
129             $entry->tags([ $item->{dc}->{subject} ])
130                 if $item->{dc}->{subject};
131             $entry->date( Plagger::Date->parse('Mail', $item->{pubDate}) );
132             $entry->link($item->{link});
133             $entry->id($item->{guid});
134
135             $entry->body($item->{description});
136
137             $feed->add_entry($entry);
138         }
139
140         $context->update->add($feed);
141     }
142 }
143
144 1;
145
146 __END__
147
148 =head1 NAME
149
150 Plagger::Plugin::Subscription::Bloglines - Bloglines Subscription
151
152 =head1 SYNOPSIS
153
154   - module: Subscription::Bloglines
155     config:
156       username: your-email@account
157       password: your-password
158       mark_read: 1
159
160 =head1 DESCRIPTION
161
162 This plugin allows you to synchronize your subscription using
163 Bloglines Web Services sync API.
164
165 =head1 CONFIGURATION
166
167 =over 4
168
169 =item username, password
170
171 Your username & password to use with Bloglines API.
172
173 =item mark_read
174
175 C<mark_read> specifies whether this plugin I<marks as read> the items
176 you synchronize. With this option set to 0, you will get the
177 duplicated updates everytime you run Plagger, until you mark them
178 unread using Bloglines browser interface. Defaults to 1.
179
180 For people who uses Bloglines browser interface regularly, and use
181 Plagger as a tool to synchronize feed updates to mobile devices (like
182 PSP or iPod), I'd recommend set this option to 0.
183
184 Otherwise, especially for Publish::Gmail plugin users, I recommend set
185 to 1, the default.
186
187 =item fetch_folders
188
189 C<fetch_folders> specifies whether this plugin fetches I<folder>
190 strucuture using listsubs API. With this option on, all feeds under
191 I<Plagger> folder will have I<Plagger> as its tag.
192
193 You can use this tags information using Rules in later phase.
194
195 =back
196
197 =head1 AUTHOR
198
199 Tatsuhiko Miyagawa
200
201 =head1 SEE ALSO
202
203 L<Plagger>, L<WebService::Bloglines>, L<http://www.bloglines.com/>
204
205 =cut
206
Note: See TracBrowser for help on using the browser.