| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Yahoo::Weather; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 25696 | use 5.008008; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 4 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 5 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 6 |  | 
|  | 1 |  |  |  |  | 54 |  | 
| 6 | 1 |  |  | 1 |  | 5 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 143 |  | 
| 7 | 1 |  |  | 1 |  | 1095 | use LWP::Simple qw/get/; | 
|  | 1 |  |  |  |  | 323884 |  | 
|  | 1 |  |  |  |  | 11 |  | 
| 8 | 1 |  |  | 1 |  | 676 | use XML::Simple; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | use Data::Dumper; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | require Exporter; | 
| 12 |  |  |  |  |  |  | our @ISA = qw(Exporter); | 
| 13 |  |  |  |  |  |  | our $xs=XML::Simple->new(); | 
| 14 |  |  |  |  |  |  | # Items to export into callers namespace by default. Note: do not export | 
| 15 |  |  |  |  |  |  | # names by default without a very good reason. Use EXPORT_OK instead. | 
| 16 |  |  |  |  |  |  | # Do not simply export all your public functions/methods/constants. | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | # This allows declaration	use Yahoo::Weather ':all'; | 
| 19 |  |  |  |  |  |  | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | 
| 20 |  |  |  |  |  |  | # will save memory. | 
| 21 |  |  |  |  |  |  | our %EXPORT_TAGS = ( 'all' => [ qw( | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | ) ] ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | our @EXPORT = qw( | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | ); | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | our $VERSION = '0.04'; | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | use constant YAHOOWAPI => 'http://weather.yahooapis.com/forecastrss?'; | 
| 34 |  |  |  |  |  |  | use constant YAHOOYDN => 'http://query.yahooapis.com/v1/public/yql?q='; | 
| 35 |  |  |  |  |  |  | use constant LOCATION_EMPTY => -1; | 
| 36 |  |  |  |  |  |  | use constant INVALID_LOCATION=> -2; | 
| 37 |  |  |  |  |  |  | use constant INVALID_ZIP=> -2; | 
| 38 |  |  |  |  |  |  | use constant ZIP_EMPTY=> -1; | 
| 39 |  |  |  |  |  |  | use constant WEATHER_FORECAST_NOT_AVAILABLE => -3; | 
| 40 |  |  |  |  |  |  | use constant INVALID_KEYWORD => -4; | 
| 41 |  |  |  |  |  |  | use constant SUGESTIONS_NOT_AVAILABLE => -4; | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub new { | 
| 45 |  |  |  |  |  |  | my $class=shift; | 
| 46 |  |  |  |  |  |  | my $self = {}; | 
| 47 |  |  |  |  |  |  | bless ($self,$class); | 
| 48 |  |  |  |  |  |  | return $self; | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub getWeatherByLocation { | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | my $self = shift; | 
| 54 |  |  |  |  |  |  | my $loc  = shift; | 
| 55 |  |  |  |  |  |  | my $degree  = shift; | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | if (! $loc) { | 
| 58 |  |  |  |  |  |  | return LOCATION_EMPTY ; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | my $woeid= $self->_getWoeidByLoc($loc); | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | if (! $woeid) { | 
| 64 |  |  |  |  |  |  | return INVALID_LOCATION; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | my $xml = get( $self->_getLocationURL($woeid,$degree) ); | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | my $w = $xs->xml_in($xml) or return WEATHER_FORECAST_NOT_AVAILABLE; | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | my $detailedweather=$self->_parseWeatherXML($w); | 
| 72 |  |  |  |  |  |  | return $detailedweather; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub _getLocationURL { | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | my ( $self, $field ,$degree) = @_; | 
| 80 |  |  |  |  |  |  | my $str='u=c&w='; | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | if (lc($degree) eq 'f'){ | 
| 83 |  |  |  |  |  |  | $str='u=f&w='; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | my $url = YAHOOWAPI .$str.$field; | 
| 87 |  |  |  |  |  |  | return $url; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | sub getWeatherByZip { | 
| 91 |  |  |  |  |  |  | my $self = shift; | 
| 92 |  |  |  |  |  |  | my $zip  = shift; | 
| 93 |  |  |  |  |  |  | my $degree  = shift; | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | if (! $zip) { | 
| 96 |  |  |  |  |  |  | return ZIP_EMPTY; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | my $woeid= $self->_getWoeidByZip($zip); | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | if (! $woeid){ | 
| 102 |  |  |  |  |  |  | return WEATHER_FORECAST_NOT_AVAILABLE | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | my $xml = get( $self->_getLocationURL($woeid,$degree) ); | 
| 106 |  |  |  |  |  |  | my $w = $xs->xml_in($xml) or return WEATHER_FORECAST_NOT_AVAILABLE; | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | my $detailedweather=$self->_parseWeatherXML($w); | 
| 109 |  |  |  |  |  |  | return $detailedweather; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | sub getSugestions{ | 
| 113 |  |  |  |  |  |  | my $self = shift; | 
| 114 |  |  |  |  |  |  | my $keyword = shift; | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | if (! $keyword) { | 
| 117 |  |  |  |  |  |  | return INVALID_KEYWORD; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | my $suggest= $self->_getSugestions($keyword); | 
| 121 |  |  |  |  |  |  | return $suggest; | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | sub _getWoeidByLoc{ | 
| 125 |  |  |  |  |  |  | my $self = shift; | 
| 126 |  |  |  |  |  |  | my $loc  = shift; | 
| 127 |  |  |  |  |  |  | my $finder = "http://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text =\' $loc \' "; | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | my $xml = get($finder); | 
| 130 |  |  |  |  |  |  | if (! $xml){ | 
| 131 |  |  |  |  |  |  | return INVALID_LOCATION; | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | my $w = $xs->xml_in($xml) or return WEATHER_FORECAST_NOT_AVAILABLE; | 
| 135 |  |  |  |  |  |  | my $wwoeid=$self->_parseXmlForWoeid($w,$loc); | 
| 136 |  |  |  |  |  |  | return $wwoeid; | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | sub _getSugestions{ | 
| 141 |  |  |  |  |  |  | my $self = shift; | 
| 142 |  |  |  |  |  |  | my $field  = shift; | 
| 143 |  |  |  |  |  |  | my $finder = "http://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text =\' $field \' "; | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | my $xml = get($finder); | 
| 146 |  |  |  |  |  |  | if (! $xml){ | 
| 147 |  |  |  |  |  |  | return INVALID_KEYWORD; | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | my $w = $xs->xml_in($xml) or return SUGESTIONS_NOT_AVAILABLE; | 
| 151 |  |  |  |  |  |  | my $suggest=$self->_parseXmlForSuggestions($w,$field); | 
| 152 |  |  |  |  |  |  | return $suggest; | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | sub _parseXmlForWoeid{ | 
| 157 |  |  |  |  |  |  | my $self = shift; | 
| 158 |  |  |  |  |  |  | my $xmlref  = shift; | 
| 159 |  |  |  |  |  |  | my $check = shift; | 
| 160 |  |  |  |  |  |  | $check = lc($check); | 
| 161 |  |  |  |  |  |  | my $woeid; | 
| 162 |  |  |  |  |  |  | my $locdetailsref = $xmlref->{results}->{Result}; | 
| 163 |  |  |  |  |  |  | if (ref($locdetailsref) eq 'HASH'){ | 
| 164 |  |  |  |  |  |  | $woeid=$locdetailsref->{woeid}; | 
| 165 |  |  |  |  |  |  | } | 
| 166 |  |  |  |  |  |  | elsif(ref($locdetailsref) eq 'ARRAY'){ | 
| 167 |  |  |  |  |  |  | my @arr= @$locdetailsref; | 
| 168 |  |  |  |  |  |  | foreach my $iter (@arr) { | 
| 169 |  |  |  |  |  |  | $woeid=$iter->{woeid} if((lc($iter->{uzip}) eq $check) || (lc($iter->{city}) eq $check) || (lc($iter->{neighborhood}) eq $check)); | 
| 170 |  |  |  |  |  |  | } | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | }else{ | 
| 173 |  |  |  |  |  |  | return; | 
| 174 |  |  |  |  |  |  | } | 
| 175 |  |  |  |  |  |  | return $woeid; | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | } | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | sub _parseXmlForSuggestions{ | 
| 181 |  |  |  |  |  |  | my $self = shift; | 
| 182 |  |  |  |  |  |  | my $xmlref  = shift; | 
| 183 |  |  |  |  |  |  | my $check = shift; | 
| 184 |  |  |  |  |  |  | my $locdetailsref = $xmlref->{results}->{Result}; | 
| 185 |  |  |  |  |  |  | return $locdetailsref; | 
| 186 |  |  |  |  |  |  | } | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | sub _getWoeidByZip { | 
| 189 |  |  |  |  |  |  | my $self = shift; | 
| 190 |  |  |  |  |  |  | my $zip  = shift; | 
| 191 |  |  |  |  |  |  | my $finder = "http://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text =\' $zip \' "; | 
| 192 |  |  |  |  |  |  | my $xml = get($finder); | 
| 193 |  |  |  |  |  |  | my $w = $xs->xml_in($xml) or return WEATHER_FORECAST_NOT_AVAILABLE; | 
| 194 |  |  |  |  |  |  | my $wwoeid=$self->_parsexmlforwoeid($w,$zip); | 
| 195 |  |  |  |  |  |  | return $wwoeid; | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | } | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | sub _parseWeatherXML { | 
| 200 |  |  |  |  |  |  | my $self = shift; | 
| 201 |  |  |  |  |  |  | my $xmlref  = shift; | 
| 202 |  |  |  |  |  |  | my $title=$xmlref->{channel}->{title}; | 
| 203 |  |  |  |  |  |  | my $finalresult={}; | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | $finalresult->{LocationDetails}=$xmlref->{channel}->{'yweather:location'}; | 
| 206 |  |  |  |  |  |  | $finalresult->{WindDetails}=$xmlref->{channel}->{'yweather:wind'}; | 
| 207 |  |  |  |  |  |  | $finalresult->{Title}=$xmlref->{channel}->{description}; | 
| 208 |  |  |  |  |  |  | $finalresult->{WeatherUnits}=$xmlref->{channel}->{'yweather:units'}; | 
| 209 |  |  |  |  |  |  | $finalresult->{Atmosphere}=$xmlref->{channel}->{'yweather:atmosphere'}; | 
| 210 |  |  |  |  |  |  | $finalresult->{LastUpdatedTime}=$xmlref->{channel}->{lastBuildDate}; | 
| 211 |  |  |  |  |  |  | $finalresult->{Astronomy}=$xmlref->{channel}->{'yweather:astronomy'}; | 
| 212 |  |  |  |  |  |  | $finalresult->{CurrentObservation}=$xmlref->{channel}->{item}->{'yweather:condition'}; | 
| 213 |  |  |  |  |  |  | $finalresult->{TwoDayForecast} = $xmlref->{channel}->{item}->{'yweather:forecast'}; | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | return $finalresult; | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | } | 
| 218 |  |  |  |  |  |  | # Preloaded methods go here. | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | # Autoload methods go after =cut, and are processed by the autosplit program. | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | 1; | 
| 223 |  |  |  |  |  |  | __END__ |