Google APIで天気を取得

Google API から天気の取得 - みずぴー日記 を見て Perlで書いて
みました。

#!/usr/bin/env perl
use strict;
use warnings;

use XML::LibXML;
use LWP::Simple;

binmode STDOUT, ":utf8";

my $region = shift @ARGV or die "usage google_weather.pl region\n";
my $url    = 'http://www.google.com/ig/api?weather=' . $region;
my $weather_xml = get $url;

die "Can't get $url\n" unless defined $weather_xml;

my $doc = XML::LibXML->new()->parse_string($weather_xml);

print "$region weather\n";
foreach my $forecast ( $doc->findnodes("//forecast_conditions") ) {
    my $day_of_week = ($forecast->getElementsByTagName("day_of_week"))[0];
    my $condition   = ($forecast->getElementsByTagName("condition"))[0];

    print $day_of_week->getAttribute("data"), " ";
    print $condition->getAttribute("data"), "\n";
}


実行結果

% perl google_weather.pl kyoto
kyoto weather
Thu Clear
Fri Clear
Sat Cloudy
Sun Chance of Rain

久しぶりに XML処理をしてみましたけど、面倒ですね。
楽な方法を模索しないとな〜。