Section 10.11. Feeds by SMS


10.11. Feeds by SMS

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.

Example 10-2. rsssms.pl sends the first headline title to a mobile phone via SMS
#!/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 123456789

You can see how to set this up on crontab to send the latest news at the desired interval. But how about using the system status module, mod_systemstatus, to automatically detect and inform you of system failures? Perhaps you can use something like Example 10-3.

Example 10-3. mod_systemstatusSMS.pl
#!/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 my $url=$ARGV[0]; my $number=$ARGV[1];     # Retrieve the feed, or die gracefully 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");     # initialise the $message my $message;     # Look for downed servers foreach my $item (@{$rss->{'item'}}) {     next unless ($item->{'ss:responding'}) eq 'false';     $message .= "Emergency! $item->{'title'} is down.";            }     # Send the message if ($message) { my @gateway = WWW::SMS->gateways( ); my $sms = WWW::SMS->new($number, $message); foreach my $gateway(@gateway) {if ($sms->send($gateway)) {           print 'Message sent!';      } else {           print "Error: $WWW::SMS::Error\n";      }}      };

Again, run from cron, this little beasty will let you monitor hundreds of machinesas long as they are generating the correct RSSand inform you of a server outage via your mobile phone.

This combination of selective parsing, interesting output methods, and cron allows you to do many things with RSS feeds that a more comprehensive system may well inhibit. Monitoring a list of feeds for mentions of keywords is simple, as is using RSS feeds of stock prices to alert you of falls in the market. Combining these techniques with Publish and Subscribe systems (discussed in Chapter 9) gives you an even greater ability to monitor the world. Want an IRC channel to be notified of any new weblog postings? No problem. Want an SMS whenever the phrase "Free Beer" appears in your local feeds? Again, no problem.



    Developing Feeds with RSS and Atom
    Developing Feeds with Rss and Atom
    ISBN: 0596008813
    EAN: 2147483647
    Year: 2003
    Pages: 118

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net