What I did on my vacation from reality

Home About RTL8187B Laptop Cam Quotes Login

December 25, 2006

The weather outside is frightful

10:14 pm — Code, Main

Here, code to grab weather from rssweather.com. Merry Christmas, don’t say I never did anything for you.


#!/usr/bin/perl -w

use LWP::UserAgent;

%urls = (
        'newport'       =>
     'http://www.rssweather.com/rss.php?config=&forecast=zandh'
          .'&place=newport+beach'
          .'&state=ca&zipcode='
          .'&country=us'
          .'&county=06059'
          .'&zone=caz042&alt=rss20a',
);

$ua = LWP::UserAgent->new;
$data = '';

if (! defined($city = shift))
{
        $city = (keys %urls)[0];
}

if ($city eq '-')
{
        $data .= <> while (! eof STDIN);
}
else
{
        $url = $urls{$city};
        $data = $ua->get($url);

        die $data->status_line if (! $data->is_success);

        $data = $data->content;
}

for (split /\n/, $data)
{
        s/\&#176;/ deg /g;

        if (/<title>(.*?),\s/ && ! exists $data{'city'})
        {
                $data{'city'} = $1;
        }

        if (/<span class="sky">(.*?)<\/span>/)
        {
                $data{'sky'} = $1;
        }
        elsif (/<dd id="(\S+)" style="display: inline;">(.*?)<\/dd>/)
        {
                $data{$1} = $2;
        }
}

if (exists $data{'winddir'})
{
        $data{'winddir'} = (split /\s+/, $data{'winddir'}, 2)[0];
}
else
{
        $data{'winddir'} = "?";
}

$data{'city'} = (split /\s+/, $data{'city'}, 2)[0];

printf "%s\nsky: %s\n"
     ."temp: %s\n"
     ."chill: %s\n"
     ."humid: %s\n"
     ."wind: %s \@ %s\n"
     ."vis: %s\n",
        $data{'city'},
        $data{'sky'},
        $data{'heatindex'},
        $data{'windchill'},
        $data{'humidity'},
        $data{'winddir'},
        $data{'windspeed'},
        $data{'visibility'};




RSS feed

Comments»

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.