ねもぷらす

ふぁいんでぃんぐねもの日記。プログラミングとか育児とか

天気予報APIサンプルプログラム

毎朝自分の住んでるところの天気予報だけちょうど良く見逃していることに腹が立ってきたので、
Livedoor の天気予報API を使った、今日or明日の天気をブラウザいっぱいに表示する CGI を組んでみた。

#!/opt/local/bin/perl -w
use strict;
use XML::TreePP;
use Template;

eval{
    my $get  = $ARGV[0] || "today";
    my $city = $ARGV[1] || "31";
    my $tpp = XML::TreePP->new();
    my $tree = $tpp->parsehttp(
        'GET',
        'http://weather.livedoor.com/forecast/webservice/rest/v1?city=' . $city . '&day=' . $get,
    );

    # result
    my $tt = Template->new();
       $tt->process(\*DATA, $tree->{lwws}) or die $tt->error();

}; if ( $@ ) {
    print <<HTML;
Content-type: text/plain;charset=UTF-8

Exception: $@
HTML
}

exit 0;
__DATA__
Content-type: text/html;charset=UTF-8

<html>
<head>
    <title>[% forecastdate %] [% image.title %]</title>
</head>
<body>
<center>
    <a href=/cgi-bin/weather.cgi?today>今日</a>
    [% forecastdate %]
    <a href=/cgi-bin/weather.cgi?tomorrow>明日</a><br />
    <img src="[% image.url %]" alt="[% image.title %]" width="70%" hight="70%" /><br />
    [% IF temperature.min.celsius %]最低:[% temperature.min.celsius %] / [% END %]
    最高:[% temperature.max.celsius %]
    <br />
</center>
</body>
</html>

動かすとこんないい加減な感じに。