From 7986f17cdffbafa62209796de502e20832be3afb Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 30 Nov 2009 20:03:13 -0600 Subject: further factor out the caching from the RSS role --- lib/Crawl/Bot/Plugin/Mantis.pm | 12 +++--- lib/Crawl/Bot/Role/CachedItems.pm | 78 +++++++++++++++++++++++++++++++++++++++ lib/Crawl/Bot/Role/RSS.pm | 70 ++--------------------------------- 3 files changed, 88 insertions(+), 72 deletions(-) create mode 100644 lib/Crawl/Bot/Role/CachedItems.pm diff --git a/lib/Crawl/Bot/Plugin/Mantis.pm b/lib/Crawl/Bot/Plugin/Mantis.pm index f59fb80..5617193 100644 --- a/lib/Crawl/Bot/Plugin/Mantis.pm +++ b/lib/Crawl/Bot/Plugin/Mantis.pm @@ -5,7 +5,7 @@ with 'Crawl::Bot::Role::RSS'; sub rss_feed { 'http://crawl.develz.org/mantis/issues_rss.php' } -sub rss_item_to_id { +sub item_to_id { my $self = shift; my ($item) = @_; my $link = $item->identifier; @@ -15,18 +15,18 @@ sub rss_item_to_id { sub tick { my $self = shift; - $self->each_rss_item(sub { + $self->each_current_item(sub { my $item = shift; - my $id = $self->rss_item_to_id($item); - return if $self->has_rss_item($id); + my $id = $self->item_to_id($item); + return if $self->has_item($id); warn "New issue! ($id)"; (my $title = $item->title) =~ s/\d+: //; my $link = $item->link; (my $user = $item->creator) =~ s/ <.*?>$//; $self->say_all("$title ($link) by $user"); - $self->add_rss_item($id); + $self->add_item($id); }); - $self->save_rss_cache; + $self->save_item_cache; } __PACKAGE__->meta->make_immutable; diff --git a/lib/Crawl/Bot/Role/CachedItems.pm b/lib/Crawl/Bot/Role/CachedItems.pm new file mode 100644 index 0000000..b2db856 --- /dev/null +++ b/lib/Crawl/Bot/Role/CachedItems.pm @@ -0,0 +1,78 @@ +package Crawl::Bot::Role::CachedItems; +use Moose::Role; + +use autodie; +use File::Spec; + +requires 'data_dir', 'current_items', 'item_to_id'; + +has items => ( + traits => ['Hash'], + isa => 'HashRef', + builder => '_build_items', + handles => { + has_item => 'exists', + items => 'keys', + _add_item => 'set', + }, +); + +sub _build_items { + my $self = shift; + warn "Loading item cache for " . blessed($self); + my $file = $self->item_cache_file; + my %items; + if (-r $file) { + open my $fh, '<', $file; + while (<$fh>) { + chomp; + $items{$_} = 1; + } + } + else { + $self->each_current_item(sub { + my $item = shift; + my $id = $self->item_to_id($item); + $items{$id} = 1; + }); + } + warn "Done loading"; + \%items; +} + +sub add_item { + my $self = shift; + $self->_add_item($_[0], 1); +} + +sub each_current_item { + my $self = shift; + my ($code) = @_; + for my $item ($self->current_items) { + $code->($item); + } +} + +sub item_cache_file { + my $self = shift; + my $class = blessed($self); + (my $plugin = $class) =~ s/^Crawl::Bot::Plugin:://; + $plugin = lc($plugin); + my $file = "${plugin}_item_cache"; + return File::Spec->catfile($self->data_dir, $file); +} + +sub save_item_cache { + my $self = shift; + open my $fh, '>', $self->item_cache_file; + $fh->print("$_\n") for $self->items; +} + +after BUILDALL => sub { + my $self = shift; + $self->save_item_cache; +}; + +no Moose::Role; + +1; diff --git a/lib/Crawl/Bot/Role/RSS.pm b/lib/Crawl/Bot/Role/RSS.pm index c2a3f16..8470eee 100644 --- a/lib/Crawl/Bot/Role/RSS.pm +++ b/lib/Crawl/Bot/Role/RSS.pm @@ -1,80 +1,18 @@ package Crawl::Bot::Role::RSS; use Moose::Role; -use autodie; -use File::Spec; use XML::RAI; -requires 'data_dir', 'rss_item_to_id', 'rss_feed'; +with 'Crawl::Bot::Role::CachedItems'; -has rss_items => ( - traits => ['Hash'], - isa => 'HashRef', - builder => '_build_rss_items', - handles => { - has_rss_item => 'exists', - rss_items => 'keys', - _add_rss_item => 'set', - }, -); +requires 'rss_feed'; -sub _build_rss_items { +sub current_items { my $self = shift; - warn "Loading RSS cache for " . blessed($self); - my $file = $self->rss_cache_file; - my %items; - if (-r $file) { - open my $fh, '<', $file; - while (<$fh>) { - chomp; - $items{$_} = 1; - } - } - else { - $self->each_rss_item(sub { - my $item = shift; - my $id = $self->rss_item_to_id($item); - $items{$id} = 1; - }); - } - warn "Done loading"; - \%items; -} - -sub add_rss_item { - my $self = shift; - $self->_add_rss_item($_[0], 1); -} - -sub each_rss_item { - my $self = shift; - my ($code) = @_; my $rss = XML::RAI->parse_uri($self->rss_feed); - for my $item (@{ $rss->items }) { - $code->($item); - } -} - -sub save_rss_cache { - my $self = shift; - open my $fh, '>', $self->rss_cache_file; - $fh->print("$_\n") for $self->rss_items; + return @{ $rss->items }; } -sub rss_cache_file { - my $self = shift; - my $class = blessed($self); - (my $plugin = $class) =~ s/^Crawl::Bot::Plugin:://; - $plugin = lc($plugin); - my $file = "${plugin}_rss_cache"; - return File::Spec->catfile($self->data_dir, $file); -} - -after BUILDALL => sub { - my $self = shift; - $self->save_rss_cache; -}; - no Moose::Role; 1; -- cgit v1.2.3-54-g00ecf