Hak5
Save 10% at GoDaddy.com with coupon code HAK

RSS Alarm Clock Under Linux

From Hak5

Jump to: navigation, search
This article (or section) may need to be wikified.
Please help improve this article, especially its introduction, section layout, and relevant internal links.
This article has been tagged since February 2007.

The RSS alarm clock is probably one of the greatest ways to start your morning, there is only one problem, they only showed you how to run it on Windows. Can you really trust Windows with such an important task such as waking you up? Here is how to get it to run under Linux.

I tested this under Fedora Core 4 and Ubuntu Dapper Drake.

Things you will need:

  • Festival Speech Synth
  • mplayer or amaroK
  • XML::Parser plugin
  • XML::RSS plugin
  • perl
  • rss2html.pl provided below.

1st, install festival, mplayer and perl, if it is not already done.

2nd, install the XML::RSS plugin

-2.1 extract XML-RSS-1.10.zip

-2.2 open a new bash terminal, cd to the extracted directory

-2.3 run perl Makefile.PL

-2.4 run Make

-2.5 run sudo Make install

Do the same for the XML::Parser plugin

3rd, copy and paste the rss2html.pl perl script into a text editor. Make a new directory for this project and save the script in the new directory as rss2html.pl

Perl Script:

#!/usr/bin/perl -w
# rss2html - converts an RSS file to HTML
# It take one argument, either a file on the local system,
# or an HTTP URL like http://slashdot.org/slashdot.rdf
# by Jonathan Eisenzopf. v1.0 19990901
# Copyright (c) 1999 Jupitermedia Corp. All Rights Reserved.
# See http://www.webreference.com/perl for more information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

#  ~~~P1mped 0ut l33t hax0r s7yle by d1gital~~~

# INCLUDES
use strict;
use XML::RSS;
use LWP::Simple;

# Declare variables
my $content;
my $file;

# MAIN
# check for command-line argument
die "Usage: rss2html.pl <lines> (<RSS file> | <URL>)\n" unless @ARGV == 2;

# get the command-line argument
my $arg = shift;

my $lines = $arg;
$arg = shift;
# create new instance of XML::RSS
my $rss = new XML::RSS;

# argument is a URL
if ($arg=~ /http:/i) {
    $content = get($arg);
    die "Could not retrieve $arg" unless $content;
    # parse the RSS content
    $rss->parse($content);

# argument is a file
} else {
    $file = $arg;
    die "File \"$file\" does't exist.\n" unless -e $file;
    # parse the RSS file
    $rss->parsefile($file);
}

# print the HTML channel
&print_html($rss);

# SUBROUTINES
sub print_html {
    my $rss = shift;
    print <<HTML;
HTML

    # print the channel items
    my $i = 0;
    foreach my $item (@{$rss->{'items'}})
    {
      next unless defined($item->{'title'});
      print "$item->{'title'}... ... ... ... ...\n";
      if($i==$lines - 1){
         last;   
      }
       $i = $i + 1;
    }

    print <<HTML;
HTML
}

3 1/2th, create weather.pl

#!/usr/bin/perl -w
# rss2html - converts an RSS file to HTML
# It take one argument, either a file on the local system,
# or an HTTP URL like http://slashdot.org/slashdot.rdf
# by Jonathan Eisenzopf. v1.0 19990901
# Copyright (c) 1999 Jupitermedia Corp. All Rights Reserved.
# See http://www.webreference.com/perl for more information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.


#  ~~~P1mped 0ut l33t hax0r s7yle by d1gital~~~

# INCLUDES
use strict;
use XML::RSS;
use LWP::Simple;

# Declare variables
my $content;
my $file;

# MAIN
# check for command-line argument
die "Usage: weather.pl <zipcode> \n" unless @ARGV == 1;

# get the command-line argument
my $arg = shift;

# create new instance of XML::RSS
my $rss = new XML::RSS;

my $url = "http://www.rssweather.com/zipcode/$arg/rss.php";
    $content = get($url);
    die "Could not retrieve $url" unless $content;
    # parse the RSS content
    $rss->parse($content);

# print the HTML channel
&print_html($rss);

# SUBROUTINES
sub print_html {
    my $rss = shift;
    print <<HTML;
HTML

    # print the channel items
    my $i = 0;
    foreach my $item (@{$rss->{'items'}})
    {
      next unless defined($item->{'description'});
      print "$item->{'description'}... ... ... ... ...\n";
      if($i==1){
         last;   
      }
       $i = $i + 1;
    }

    print <<HTML;
HTML
}

4th, create the bash script. The bash script is relatively simple, it grabs your RSS feeds using rss2html.pl, stores them in alarm.txt, plays some music, and then reads the contents of alarm.txt. Be sure to save it in the same directory as the rss2html.pl file.

If you use mplayer:


cd /home/adam/rss_alarm_clock/

echo "good morning , today is" >> alarm.txt

date +"%A %B %d ..... Time .. %l hours. and %M minutes" >> alarm.txt

echo "... ... ... Todays forecast ..." >> alarm.txt

perl weather.pl <PUT YOUR ZIPCODE HERE> >> alarm.txt

echo "... ... ... The lastest from dig is ..." >> alarm.txt

perl rss2html.pl 3 http://digg.com/rss/index.xml >> alarm.txt

echo "The top stories from slash dot are ..." >> alarm.txt

perl rss2html.pl 3 http://rss.slashdot.org/Slashdot/slashdot >> alarm.txt

echo "time to meet that real girl, have a nice day" >> alarm.txt

mplayer music.mp3

festival --tts alarm.txt

rm -f alarm.txt

Line 0 cd's the working directory wit the scripts line 1 & 2 gives an intro with the date and time lines 3-6 grab stories from digg and ./ line 7 gives some insparation and an outro line 8 plays music line 9 synth's the contents of alarm.txt line 10 deletes alarm.txt

if you use amaroK:

echo **Getting Weather**
echo Todays Forecast ... ... ... >> alarm.txt
perl weather.pl <PUT YOUR ZIPCODE HERE> >> alarm.txt
echo **Getting RSS Feeds**
echo Today on hack a day ... ... ... >> alarm.txt
perl rss2html.pl 1 http://www.hackaday.com/rss.xml >> alarm.txt
echo **Going to next song**
amarok -f
echo **Playing**
amarok -p
echo **Waiting...**
sleep 30
echo **Pausing playback**
amarok --pause
echo **Reading RSS Feeds**
cat alarm.txt
festival --tts alarm.txt
echo **Resuming playback**
amarok --play-pause
echo **Waiting...**
sleep 30
echo **Stopping playback**
amarok -s
echo **Removing alarm.txt**
rm -f alarm.txt

amarok users: for best results go to Mode>Random>Tracks in amarok to enable randomness


4th, give the batch script the ability to be executed 4.1a:the easy way: Right clik on the batch script, properties>permissions under owner, check Execute 4.1b:the hard way: run command chmod [batchfile] -rwxr--r--

5th, schedule the batch script for the desired time using cron.