April 2005
Intermediate to advanced
270 pages
7h 13m
English
In most of the examples so
far, we have written the parsed feed to the screen of a computer.
This is just the start. You can use the same basic structure to
output to just about anything that handles text. Example 10-2 is a script that sends the top headline of a
feed to a mobile phone via the Short Message Service (SMS). It uses
the
WWW::SMS module,
outputting to the first web-based free SMS service it can find that
works.
#!/usr/local/bin/perl
use strict;
use warnings;
use LWP::Simple;
use XML::Simple;
use WWW::SMS;
# Take the command line arguments, URL first, then complete number of mobile
my $url=$ARGV[0];
my $number=$ARGV[1];
# Retrieve the feed, or die disgracefully
my $feed_to_parse = get ($url) or die "I can't get the feed you want";
# Parse the XML
my $parser = XML::Simple->new( );
my $rss = $parser->XMLin("$feed_to_parse");
# Get the data we want
my $message = "NEWSFLASH:: $rss->{'channel'}->{'item'}->[0]->{'title'}";
# Send the message
my @gateway = WWW::SMS->gateways( );
my $sms = WWW::SMS->new($number, $message);
foreach my $gateway(@gateway) {if ($sms->send($gateway)) {
print 'Message sent!';
last;
} else {
print "Error: $WWW::SMS::Error\n";
}}You can use the script in Example 10-2 from the command line or crontab like so:
perl rsssms.pl http://full.urlof/feed.xml 123456789You can see how to set this up on crontab to send the latest news at the desired interval. But how ...