spoken news

marcel on 2002-07-23T12:29:19

Hear your favourite RSS newsfeeds: Take some LWP, a little XML::RSS and season with command-line driven AppleScript.

#!/usr/bin/perl

use warnings; use strict; use XML::RSS; use LWP::Simple; $|++;

my %news = ( slashdot => "http://slashdot.org/slashdot.rdf", useperl => "http://use.perl.org/useperl.rss", );

my $key = shift || die "usage: $0 key\n"; die "unknown key\n" unless exists $news{$key};

my $rss = XML::RSS->new or die "can't create XML::RSS?"; eval { $rss->parse(get( $news{$key} )) } and not $@ or die "cannot get or parse content: $@";

for my $item (@{$rss->{items}}) { my $title = $item->{title}; my $spoken = prepare_spoken($title);;

print "title: <$title>\n"; print "spoken: <$spoken>\n\n"; system('/usr/bin/osascript', '-e', sprintf q!say "%s"!, $spoken) == 0 or die "can't speak '$spoken': $?"; sleep(1); }

sub prepare_spoken { local $_ = shift; $_ = lc;

s/\.NET/ dot net /gi; s/U\.S\./ U S /gi; s/mozilla/ mo zilla /gi; s/AMD's/ AMDs /gi; s/yapc/ yapsee /gi; s/bsd/ BSD /gi;

s/(\d+)-/ $1 /gi; s/(?<=\S)\.(?=\S)/ dot /gi; s/(?<=\.)/ /g; y/"()//d; $_ }

__END__

Note that prepare_spoken is rather primitive; some more general rules would be appreciated. Send suggestions to marcel@cpan.org.