Plagger::Plugin::CustomFeed::GoogleSearch 作ってみた


livedoor ブログ検索とかは検索結果のRSS吐いてるけど、Google の検索結果もFeed として扱いたいなぁ。

ということで Plagger::Plugin::CustomFeed::GoogleSearch 作ってみた。

ソースは以下。

package Plagger::Plugin::CustomFeed::GoogleSearch;
use strict;
use warnings;
use base qw(Plagger::Plugin);

use Net::Google;

sub register {
    my ($self, $context) = @_;
    $context->register_hook(
        $self,
        'subscription.load' => \&load,
    );
}

sub load {
    my ($self, $context) = @_;
    my $feed = Plagger::Feed->new;
    $feed->aggregator(sub { $self->aggregate(@_) });
    $context->subscription->add($feed);
}

sub aggregate {
    my ($self, $context, $args) = @_;
    my $google = Net::Google->new(
        key         => $self->conf->{key},
        starts_at   => $self->conf->{starts_at} || 0,
        max_results => $self->conf->{max_results} || 10,
        lr          => $self->conf->{lr} || '',
        http_proxy  => $self->conf->{http_proxy} || '',
    );
    my $search = $google->search;
    $search->query(split(' ', $self->conf->{query}));

    my $feed = Plagger::Feed->new;
    $feed->type('googlesearch');
    $feed->title($self->conf->{query}.' - Google Search');

    for my $result (@{$search->results}) {
        my $entry = Plagger::Entry->new;
        $entry->title($result->title);
        $entry->link($result->URL);
        $entry->body($result->snippet);
        $feed->add_entry($entry);
    }
    $context->update->add($feed);
}

1;
__END__

=head1 NAME

Plagger::Plugin::CustomFeed::GoogleSearch - CustomFeed for Google Search

=head1 SYNOPSIS

  - module: CustomFeed::GoogleSearch
    config:
      key: your_google_api_key
      starts_at: 0
      max_results: 10
      lr: ja
      http_proxy: your.http.proxy:8080
      query: Plagger


=head1 DESCRIPTION

This plugin fetches Google Search result

=head1 AUTHOR

Jiro Nishiguchi

=head1 SEE ALSO

L<Plagger>, L<Net::Google>

=cut

config は Pod のとおり。

Google Web APIs license key が必要。


ってこんなときになにやってんだ俺。

いろいろあって眠れないので気晴らしに作ってみました・・・