Acme-24
view release on metacpan or search on metacpan
lib/Acme/24.pm view on Meta::CPAN
use LWP::Simple ();
use XML::RSSLite ();
use constant URL => 'http://www.notrly.com/jackbauer';
# Returns one random fact
sub random_jackbauer_fact
{
my $url = URL;
my $page = LWP::Simple::get($url);
my $fact = '';
if($page =~ m(<p class="fact">([^<]+)</p>))
{
$fact = $1;
$fact =~ s/^\s+//;
$fact =~ s/\s+$//;
if(eval('use HTML::Entities'))
{
HTML::Entities::decode_entities($fact);
}
$fact .= "\n";
}
return($fact);
}
# Returns an array of 24 random facts
sub random_jackbauer_facts
{
my @facts = ();
my $url = URL . '/rss.php';
my $tries = 5;
my %seen;
while ($tries-- > 0 && @facts < 24) {
my %result;
my $feed = LWP::Simple::get($url);
XML::RSSLite::parseRSS(\%result, \$feed);
if (exists $result{item} && UNIVERSAL::isa($result{item}, 'ARRAY'))
{
for my $fact (@{ $result{item} }) {
next if exists $seen{$fact->{title}};
push @facts, $fact->{title};
$seen{$fact->{title}} = undef;
}
}
sleep 1;
}
if (@facts && scalar(@facts) > 24) {
splice(@facts, 24);
}
return(\@facts);
}
# Build a database of Jack Bauer facts
sub collect_facts
{
my($self, $file) = @_;
$file ||= './jackbauer.txt';
my $new_facts = $self->random_jackbauer_facts();
return unless $new_facts;
open(my $fh, '>>' . $file) or return;
for(@$new_facts)
{
print $fh $_, "\n%\n";
}
close($fh);
}
unless (caller) {
print random_jackbauer_fact();
# Collect random Jack Bauer facts
#$| = 1;
#while(1)
#{
# Acme::24->collect_facts() and print '.';
#}
}
1;
( run in 0.554 second using v1.01-cache-2.11-cpan-39bf76dae61 )