| 1 | #!perl | ||
| 2 | |||
| 3 | package Weather::Fetch; | ||
| 4 | |||
| 5 | use XML::LibXML; | ||
| 6 | |||
| 7 | our $VERSION = 0.02; | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my $class = shift; | ||
| 11 | my $self = { | ||
| 12 | _metric => shift, | ||
| 13 | _code => shift, | ||
| 14 | _lang => shift, | ||
| 15 | _country => shift, | ||
| 16 | _city => shift | ||
| 17 | }; | ||
| 18 | |||
| 19 | bless $self, $class; | ||
| 20 | |||
| 21 | return $self; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub getXml { | ||
| 25 | my( $self ) = @_; | ||
| 26 | my $city = $self->{_city}; | ||
| 27 | my $lang = $self->{_lang}; | ||
| 28 | my $code = $self->{_code}; | ||
| 29 | my $country = $self->{_country}; | ||
| 30 | my $metric = $self->{_metric}; | ||
| 31 | |||
| 32 | if ($metric eq "C" || $metric eq "c") { | ||
| 33 | $metric = "1"; | ||
| 34 | } else { | ||
| 35 | $metric = "0"; | ||
| 36 | } | ||
| 37 | |||
| 38 | my $var = qx{curl -s "http://rss.accuweather.com/rss/liveweather_rss.asp?metric=$metric&locCode=$lang|$country|$code|$city"}; | ||
| 39 | return $var; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub getWeather { | ||
| 43 | my ( $self, $xml, $type ) = @_; | ||
| 44 | |||
| 45 | $xml = XML::LibXML->load_xml(string=>$xml); | ||
| 46 | my $str; | ||
| 47 | foreach my $title ($xml->findnodes('/rss/channel/item/title')) { | ||
| 48 | if($title =~ /Currently/){ | ||
| 49 | my ($junk, $cond, $temp) = split("\:", $title->to_literal()); | ||
| 50 | if($type eq "temp"){ | ||
| 51 | $str = $temp; | ||
| 52 | } else { | ||
| 53 | $str = $cond; | ||
| 54 | } | ||
| 55 | last; | ||
| 56 | } else { | ||
| 57 | $str = "ERROR!!"; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | $str =~ s/^\s+//; | ||
| 61 | return $str; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub getForecast { | ||
| 65 | my ( $self, $xml ) = @_; | ||
| 66 | $xml = XML::LibXML->load_xml(string=>$xml); | ||
| 67 | my $str; | ||
| 68 | my $count = 0; | ||
| 69 | foreach my $description ($xml->findnodes('/rss/channel/item/description')) { | ||
| 70 | if($description =~ /High:/){ | ||
| 71 | my @arr = split("<", $description->to_literal()); | ||
| 72 | if($count == 0){ | ||
| 73 | $str = "Today: " . $arr[0] . "\n"; | ||
| 74 | } else { | ||
| 75 | $str .= "Tomorrow: " . $arr[0] . "\n"; | ||
| 76 | } | ||
| 77 | |||
| 78 | if($count == 1){ | ||
| 79 | last; | ||
| 80 | } else { | ||
| 81 | $count += 1; | ||
| 82 | } | ||
| 83 | } else { | ||
| 84 | $str = "ERROR!!"; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | chomp($str); | ||
| 88 | return $str; | ||
| 89 | } | ||
| 90 | |||
| 91 | =head1 NAME | ||
| 92 | |||
| 93 | Weather::Fetch - a module that fetches the weather | ||
| 94 | |||
| 95 | =head1 DESCRIPTION | ||
| 96 | |||
| 97 | This is a module that can be used to make a weather app. | ||
| 98 | see F<script/weather.pl> for example | ||
| 99 | |||
| 100 | =head1 AUTHOR | ||
| 101 | |||
| 102 | Brad Heffernan | ||
| 103 | |||
| 104 | =head1 LICENSE | ||
| 105 | |||
| 106 | FreeBSD | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | 1; |