diff options
author | Jesse Luehrs <doy@tozt.net> | 2009-11-27 03:15:02 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2009-11-27 03:15:02 -0600 |
commit | f37beb4ccf327d07420f0f521ef498602541d455 (patch) | |
tree | c4975d7301f7d03bcd398bef913f1ee9e97f7aea /lib | |
parent | aca3759d9008e9b9f4a3f703357c26b45e8cd713 (diff) | |
download | crawlbot-f37beb4ccf327d07420f0f521ef498602541d455.tar.gz crawlbot-f37beb4ccf327d07420f0f521ef498602541d455.zip |
factor out mantis-specific things into its own class
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Crawl/Bot.pm | 100 | ||||
-rw-r--r-- | lib/Crawl/Bot/Mantis.pm | 116 |
2 files changed, 132 insertions, 84 deletions
diff --git a/lib/Crawl/Bot.pm b/lib/Crawl/Bot.pm index 4fa7d62..9e1cd93 100644 --- a/lib/Crawl/Bot.pm +++ b/lib/Crawl/Bot.pm @@ -3,8 +3,7 @@ use Moose; use MooseX::NonMoose; extends 'Bot::BasicBot'; -use autodie; -use XML::RAI; +use File::Path; has [qw(username name)] => ( # don't need (or want) accessors, just want to initialize the hash slot @@ -14,18 +13,11 @@ has [qw(username name)] => ( default => sub { shift->nick }, ); -has rss_feed => ( +has data_dir => ( 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 => 'cache', + default => 'dat', ); has update_time => ( @@ -34,93 +26,33 @@ has update_time => ( default => 300, ); -has issues => ( - traits => ['Hash'], - isa => 'HashRef', +has mantis => ( + is => 'ro', + isa => 'Crawl::Bot::Mantis', + lazy => 1, default => sub { my $self = shift; - my $file = $self->cache_file; - my $issues = {}; - if (-r $file) { - warn "Updating seen issue list from the cache..."; - open my $fh, '<', $file; - while (<$fh>) { - chomp; - $issues->{$_} = 1; - 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/; - $issues->{$id} = 1; - warn " got issue $id"; - }); - } - return $issues; - }, - handles => { - has_issue => 'exists', - issues => 'keys', - _add_issue => 'set', + require Crawl::Bot::Mantis; + Crawl::Bot::Mantis->new(bot => $self); }, ); -sub add_issue { - my $self = shift; - $self->_add_issue($_[0], 1); -} - sub BUILD { my $self = shift; - $self->save_cache; -} - -sub each_issue { - my $self = shift; - my ($code) = @_; - my $rss = XML::RAI->parse_uri($self->rss_feed); - for my $issue (@{ $rss->items }) { - $code->($issue); - } + File::Path::make_path($self->data_dir); + $self->mantis; } -sub save_cache { +before say => sub { my $self = shift; - warn "Saving cache state to " . $self->cache_file; - open my $fh, '>', $self->cache_file; - $fh->print("$_\n") for $self->issues; -} + my %params = @_; + warn "sending '$params{body}' to $params{channel}"; +}; 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); - warn "New issue! ($id)"; - (my $title = $issue->title) =~ s/\d+: //; - my $link = $issue->link; - (my $user = $issue->creator) =~ s/ <.*?>$//; - $self->say( - channel => $_, - body => "$title ($link) by $user", - ) for $self->channels; - $self->add_issue($id); - }); - $self->save_cache; - + $self->mantis->tick; return $self->update_time; } -before say => sub { - my $self = shift; - my %params = @_; - warn "sending '$params{body}' to $params{channel}"; -}; - 1; diff --git a/lib/Crawl/Bot/Mantis.pm b/lib/Crawl/Bot/Mantis.pm new file mode 100644 index 0000000..e3c9c44 --- /dev/null +++ b/lib/Crawl/Bot/Mantis.pm @@ -0,0 +1,116 @@ +package Crawl::Bot::Mantis; +use Moose; + +use autodie; +use File::Spec; +use XML::RAI; + +has bot => ( + is => 'ro', + isa => 'Crawl::Bot', + required => 1, + weak_ref => 1, + handles => [qw(say channels data_dir)], +); + +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 { + my $self = shift; + my ($code) = @_; + my $rss = XML::RAI->parse_uri($self->rss_feed); + for my $issue (@{ $rss->items }) { + $code->($issue); + } +} + +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); + warn "New issue! ($id)"; + (my $title = $issue->title) =~ s/\d+: //; + my $link = $issue->link; + (my $user = $issue->creator) =~ s/ <.*?>$//; + $self->say( + channel => $_, + body => "$title ($link) by $user", + ) for $self->channels; + $self->add_issue($id); + }); + $self->save_cache; +} + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; |