root/lang/perl/plagger/lib/Plagger/Plugin/Subscription/GoogleReader.pm

Revision 18472, 3.5 kB (checked in by hashy1126, 3 months ago)

AUTHORを本名に変更(@LL Future会場)

Line 
1package Plagger::Plugin::Subscription::GoogleReader;
2use strict;
3use base qw( Plagger::Plugin );
4
5use WebService::Google::Reader;
6
7sub plugin_id {
8    my $self = shift;
9    $self->class_id . '-' . $self->conf->{username};
10}
11
12sub register {
13    my($self, $context) = @_;
14
15    $self->init_reader;
16    $context->register_hook(
17        $self,
18        'subscription.load' => \&notifier,
19    );
20}
21
22sub init_reader {
23    my $self = shift;
24
25    unless (defined($self->conf->{username}) && defined($self->conf->{password})) {
26        Plagger->context->error("username and/or password is missing");
27    }
28}
29
30sub notifier {
31    my($self, $context) = @_;
32
33    $context->log(debug => "notifier");
34
35    my $feed = Plagger::Feed->new;
36    $feed->aggregator(sub { $self->sync(@_) });
37    $context->subscription->add($feed);
38}
39
40sub sync {
41    my($self, $context, $args) = @_;
42
43    $context->log(debug => "sync");
44
45    my $mark_read = $self->conf->{mark_read};
46       $mark_read = 1 unless defined $mark_read;
47
48    my $read_count = $self->conf->{read_count};
49       $read_count = 10 unless defined $read_count;
50
51    my $reader = WebService::Google::Reader->new(
52        username => $self->conf->{username},
53        password => $self->conf->{password},
54    );
55        unless ($reader) {
56            $context->log(error => "cannot login google reader: $!");
57            return;
58        }
59
60    my $unread = $reader->unread( count => $read_count );
61   
62    my $feed = Plagger::Feed->new;
63    $feed->type('googleReader');
64    $feed->title('googleReader');
65    $feed->url('http://google.com/reader/');
66
67    my @items = $unread->entries;
68    for my $item (@items) {
69        my $entry = Plagger::Entry->new;
70        $context->log(debug => "title:" . $item->title);
71        $context->log(debug => "href:" . $item->link()->href);
72
73        $entry->title($item->title);
74        $entry->author(defined $item->author ? $item->author : 'nobody');
75        $entry->link($item->link()->href);
76        # TODO support enclosure
77        $entry->tags([ $item->{category} ]) if $item->{category};
78        $entry->date( Plagger::Date->from_epoch($item->{modified_on}) )
79            if $item->{modified_on};
80        $entry->feed_link($feed->link);
81        my $content = $item->content();
82        if (defined $content) {
83            $entry->body($content->body);
84        } elsif (defined $item->summary) {
85            $entry->body($item->summary);
86        }
87        $reader->state_entry($item, 'read') if ($mark_read);
88        $feed->add_entry($entry);
89    }
90    $context->update->add($feed);
91}
92
931;
94__END__
95
96=head1 NAME
97
98Plagger::Plugin::Subscription::GoogleReader - Synchronize Google Reader with WebService::Google::Reader
99
100=head1 SYNOPSIS
101
102  - module: Subscription::GoogleReader
103    config:
104      username: your-google-id
105      password: your-password
106      read_count: 30
107      mark_read: 1
108
109=head1 DESCRIPTION
110
111This plugin allows you to synchronize your subscription using Google Reader
112
113=head1 CONFIGURATION
114
115=over 4
116
117=item username, password
118
119Your username & password to use with Google Reader.
120
121=item read_count
122
123=item mark_read
124
125C<mark_read> specifies whether this plugin I<marks as read> the items
126you synchronize. With this option set to 0, you will get the
127duplicated updates everytime you run Plagger, until you mark them
128unread using Google Reader web interface.
129
130=back
131
132=head1 AUTHOR
133
134Yasuyuki Hashimoto
135
136=head1 SEE ALSO
137
138L<Plagger>, L<Plagger::Plugin::Subscription::LivedoorReader>, L<WebService::Google::Reader>
139
140=cut
141
Note: See TracBrowser for help on using the browser.