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/Crawl/Bot/Plugin/Mantis.pm | |
parent | 07dd20c4443a2231bda204744bfa553c8055de00 (diff) | |
download | crawlbot-b64a7fc1d7c4c64db64a859812c10539f39d281d.tar.gz crawlbot-b64a7fc1d7c4c64db64a859812c10539f39d281d.zip |
Refactor the Mantis plugin into an RSS role
Diffstat (limited to 'lib/Crawl/Bot/Plugin/Mantis.pm')
-rw-r--r-- | lib/Crawl/Bot/Plugin/Mantis.pm | 103 |
1 files changed, 16 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; |