#!/usr/bin/perl
use strict;
use warnings;
use Festival::Client;
use LWP::Simple;
use XML::Simple;
$| = 1;
# Information for Festival server to connect to
# (undef as port value to use default port)
my %festival_server = (
host => q{localhost},
port => undef,
);
# The following values are in seconds.
# $delay - approximate delay between CB XML Ticker retrievals
# $speech_delay - approximate delay between spoken lines
my $delay = 90;
my $speech_delay = 7;
# $seen - holds the message_id of last message seen
# $max_retrievals - number of times to retrieve CB content
# before exiting (as I did not want to allow the process
# to run forever). This is computed as time to run divided
# by the retrieval delay (both in seconds).
my $seen = 0;
my ($max_retrievals);
{
# Time period to run for. (0d 0h 30m 0s, for testing)
my %to_run = (
days => 0,
hours => 0,
minutes => 30,
seconds => 0,
);
$max_retrievals = int(
(
(
( $to_run{days} * 24 + $to_run{hours} ) * 60 +
$to_run{minutes}
) * 60 + $to_run{seconds}
) / $delay
);
}
# Display CB content and number of messages spoken during
# retrieval period (set true to display)
my $display_messages = 0;
# Setup
my $xs = new XML::Simple;
my $cb_xml_url =
q{http://www.perlmonks.org/index.pl?node_id=207304};
my $fs =
Festival::Client->new( $festival_server{host},
$festival_server{port} )
or die (
qq{Could not connect to Festival server at }
. join (
':',
$festival_server{host},
(
defined(
$festival_server{port}
? $festival_server{port}
: 1314
)
)
)
. qq{: $!\n}
);
my $format = sprintf( qq{(%%0%dd) %%s - %%s: %%s\n},
length($max_retrievals) );
print length($max_retrievals), " ", $max_retrievals, "\n";
while ($max_retrievals) {
my $said = 0;
my $cb_xml = get($cb_xml_url)
or die (qq{Could not retrieve CB XML ticker: $!\n});
my $ref = $xs->XMLin( $cb_xml, ForceArray => [q{message}] )
or die (qq{Could not parse CB XML: $!\n});
# print Data::Dumper->Dump( [ \$ref ], [qw(*ref)] ), "\n";
# Check to see if there are message entries
if ( exists( $ref->{message} ) ) {
# print ref( $ref->{message} ), "\n";
foreach my $message ( @{ $ref->{message} } ) {
if ( $message->{message_id} > $seen ) {
$seen = $message->{message_id};
$fs->say(
join ( qq{\t},
$message->{author}, $message->{text} )
. qq{\n}
);
$said++;
printf $format, $max_retrievals,
scalar localtime $message->{epoch},
$message->{author}, $message->{text}
if ($display_messages);
# I experienced a problem with the content being
# spoken being "lost" if I did not delay between
# sending lines of content. Thus, the delay here.
sleep($speech_delay);
}
}
print qq{Said: $said\n} if ($display_messages);
}
$max_retrievals--;
sleep(
$speech_delay * $said < $delay
? $delay - ( $speech_delay * $said )
: 1
);
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.