diff options
author | Jesse Luehrs <doy@tozt.net> | 2009-11-30 19:46:19 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2009-11-30 19:46:19 -0600 |
commit | b64a7fc1d7c4c64db64a859812c10539f39d281d (patch) | |
tree | cb08df486122a197bb7be04f2ca1e9afd760bc18 /lib | |
parent | 07dd20c4443a2231bda204744bfa553c8055de00 (diff) | |
download | crawlbot-b64a7fc1d7c4c64db64a859812c10539f39d281d.tar.gz crawlbot-b64a7fc1d7c4c64db64a859812c10539f39d281d.zip |
Refactor the Mantis plugin into an RSS role
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Crawl/Bot/Plugin/Mantis.pm | 103 | ||||
-rw-r--r-- | lib/Crawl/Bot/Role/RSS.pm | 80 |
2 files changed, 96 insertions, 87 deletions
diff --git a/lib/Crawl/Bot/Plugin/Mantis.pm b/lib/Crawl/Bot/Plugin/Mantis.pm index 9312237..f59fb80 100644 --- a/lib/Crawl/Bot/Plugin/Mantis.pm +++ b/lib/Crawl/Bot/Plugin/Mantis.pm @@ -1,103 +1,32 @@ package Crawl::Bot::Plugin::Mantis; use Moose; extends 'Crawl::Bot::Plugin'; +with 'Crawl::Bot::Role::RSS'; -use autodie; -use File::Spec; -use XML::RAI; +sub rss_feed { 'http://crawl.develz.org/mantis/issues_rss.php' } -has rss_feed => ( - is => 'ro', - isa => 'Str', - lazy => 1, - default => 'http://crawl.develz.org/mantis/issues_rss.php', -); - -has _cache_file => ( - is => 'ro', - isa => 'Str', - lazy => 1, - default => 'mantis_issue_cache', -); - -sub cache_file { - my $self = shift; - return File::Spec->catfile($self->data_dir, $self->_cache_file); -} - -has issues => ( - traits => ['Hash'], - isa => 'HashRef', - lazy => 1, - default => sub { { } }, - handles => { - has_issue => 'exists', - issues => 'keys', - _add_issue => 'set', - }, -); - -sub BUILD { - my $self = shift; - my $file = $self->cache_file; - if (-r $file) { - warn "Updating seen issue list from the cache..."; - open my $fh, '<', $file; - while (<$fh>) { - chomp; - $self->add_issue($_); - warn " got issue $_"; - } - } - else { - warn "Updating seen issue list from a fresh copy of the feed..."; - $self->each_issue(sub { - my $issue = shift; - my $link = $issue->identifier; - (my $id = $link) =~ s/.*=(\d+)$/$1/; - $self->add_issue($id); - warn " got issue $id"; - }); - $self->save_cache; - } -} - -sub save_cache { - my $self = shift; - warn "Saving cache state to " . $self->cache_file; - open my $fh, '>', $self->cache_file; - $fh->print("$_\n") for $self->issues; -} - -sub add_issue { - my $self = shift; - $self->_add_issue($_[0], 1); -} - -sub each_issue { +sub rss_item_to_id { my $self = shift; - my ($code) = @_; - my $rss = XML::RAI->parse_uri($self->rss_feed); - for my $issue (@{ $rss->items }) { - $code->($issue); - } + my ($item) = @_; + my $link = $item->identifier; + (my $id = $link) =~ s/.*=(\d+)$/$1/; + return $id; } sub tick { my $self = shift; - warn "Checking for new issues..."; - $self->each_issue(sub { - my $issue = shift; - (my $id = $issue->identifier) =~ s/.*=(\d+)$/$1/; - return if $self->has_issue($id); + $self->each_rss_item(sub { + my $item = shift; + my $id = $self->rss_item_to_id($item); + return if $self->has_rss_item($id); warn "New issue! ($id)"; - (my $title = $issue->title) =~ s/\d+: //; - my $link = $issue->link; - (my $user = $issue->creator) =~ s/ <.*?>$//; + (my $title = $item->title) =~ s/\d+: //; + my $link = $item->link; + (my $user = $item->creator) =~ s/ <.*?>$//; $self->say_all("$title ($link) by $user"); - $self->add_issue($id); + $self->add_rss_item($id); }); - $self->save_cache; + $self->save_rss_cache; } __PACKAGE__->meta->make_immutable; diff --git a/lib/Crawl/Bot/Role/RSS.pm b/lib/Crawl/Bot/Role/RSS.pm new file mode 100644 index 0000000..c2a3f16 --- /dev/null +++ b/lib/Crawl/Bot/Role/RSS.pm @@ -0,0 +1,80 @@ +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'; + +has rss_items => ( + traits => ['Hash'], + isa => 'HashRef', + builder => '_build_rss_items', + handles => { + has_rss_item => 'exists', + rss_items => 'keys', + _add_rss_item => 'set', + }, +); + +sub _build_rss_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; +} + +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; |