RSS Alarm Clock

I finally managed to get a sound card for an old computer. Now, I can make my Ubuntu Alarm Clock! What better way to wake up each morning, than to have your friendly linux box read the news to you? The techniques that I am using are nothing new (Hak5 did it and some people at the Ubuntu Forums have talked about it). It is pretty simple, just download and install the rss2html.pl file and the necessary perl libaries, then create a bash script and add it to the crontab.

Get the programs

You will need festival and perl. You will also need mplayer if you want to play music. You can do the standard "sudo apt-get install ..." to get these (they were all installed in my basicedgy installation).

I am going to use cpan to install the perl modules. cpan is like apt for perl. You might have to set up cpan if this is your first time using it. If you are asked to set it up, I recommend typing "no" on the first question, and cpan will autoconfigure.

At the terminal:

sudo cpan

This is the cpan terminal, autoconfigure if necessary. It may take a while to install each package. If it says it requires other modules, install them as well.

install XML::RSS
install XML::Parser
install DateTime::Format::Mail
install DateTime::Format::W3CDTF

Create a file called "rss2html.pl" in ~/alarm and put this in it:

rss2html.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: 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
}

Play a Random Song

You don't have to do this, but I want to hear a song after the news, so make a file, music.sh in ~/alarm From the Ubuntu Forum

music.sh

#!/bin/bash
#made by rusl modifying file
#from http://www.macosxhints.com/article.p...51108193636341

# File locations
WritingsPath=/home/<USERNAME>/<pathtomp3s>/
TempLog=/tmp/random-alarm-music.log

# Create a temporary logfile of all matches
find $WritingsPath -iregex ".*.mp3" > $TempLog
find $WritingsPath -iregex ".*.ogg" >> $TempLog
find $WritingsPath -iregex ".*.wav" >> $TempLog

# Choose a random line number (any number from 1 to the length of the file)
LowerBound=1
RandomMax=32767
UpperBound=$(cat $TempLog | wc -l)
RandomLine=$(( $LowerBound + ($UpperBound * $RANDOM) / ($RandomMax + 1) ))

# Use sed to grab the random line
Command=$(sed -n "$RandomLine{p;q;}" "$TempLog")

# open the random line in TextEdit
mplayer "$Command"

Make Your Alarm File

Make a file alarm.sh in ~/alarm

alarm.sh

rm -rf alarm.txt
echo "good morning , today is" >> alarm.txt
date >> alarm.txt
echo "From Digg" >> alarm.txt
~/alarm/rss2html.pl 6 http://digg.com/rss/index.xml >> alarm.txt

echo "From Slashdot" >> alarm.txt
~/alarm/rss2html.pl 6 http://rss.slashdot.org/Slashdot/slashdot >> alarm.txt

festival --tts alarm.txt
rm -rf alarm.txt

~/alarm/music.sh

You can add any number of news sources.

Set Permissions

cd ~/alarm 
chmod +x rss2html.pl
chmod +x alarm.sh

Set Your Alarm

Use the crontab to set your alarm. From the terminal:

crontab -e

And paste this into the file:

0 7 * * * /home/USERNAME/alarm/alarm.sh

Further Additions

Hak5 has a script to get the weather.