Source for file GoogleWeather.php

Documentation is available at GoogleWeather.php

  1. <?php
  2. /* SVN FILE: $Id: GoogleWeather.php 8 2009-10-12 19:22:01Z Chris $ */
  3. /**
  4.  * Google Weather data provider
  5.  *
  6.  * @copyright    Copyright 2009 PBM Web Development - All Rights Reserved
  7.  * @filesource
  8.  * @package      weatherForecast
  9.  * @subpackage   weatherForecast.providers
  10.  * @version      $Revision: 8 $
  11.  * @modifiedby   $LastChangedBy: Chris $
  12.  * @lastmodified $Date: 2009-10-12 20:22:01 +0100 (Mon, 12 Oct 2009) $
  13.  * @license      http://www.opensource.org/licenses/bsd-license.php The BSD License
  14.  */
  15.  
  16. include dirname(__FILE__'/../WeatherForecastProvider.php';
  17.  
  18. /**
  19.  * Google Weather data provider
  20.  *
  21.  * <p>Values in params:<br>
  22.  * dateFormat (string) => Date format for days;
  23.  * Used by {@link http://www.yiiframework.com/doc/api/CDateFormatter#format-detail CDateFormatter::format()},
  24.  * see {@link http://www.unicode.org/reports/tr35/#Date_Format_Patterns Date Format Patterns}
  25.  * for a description of formats. Default: "EEE d'&nbsp;'MMM yyyy"<br>
  26.  * language (string) => {@link http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes ISO 639-1 code}.
  27.  * Default: 'en' (English)<br>
  28.  * location (string) => Location.<br>
  29.  * The API is not published, but UK post codes, US zip codes and "place,region" seem to work.
  30.  * Place is the name of a town or city. Region is a US state, country name or
  31.  * an {@link http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 ISO 3166-1 alpha-2 code}.
  32.  * Place and region are separated by a comma <b>only</b> - no spaces allowed.<br>
  33.  * units (string) => Units to provide the forecast in.
  34.  * "C"|"F" C=metric (default), F=imperial</p>
  35.  *
  36.  * @package  weatherForecast
  37.  * @subpackage weatherForecast.providers
  38.  */
  39. class GoogleWeather extends WFXMLProvider {
  40.   /**
  41.    * @var string Driver description
  42.    */
  43.   private $provider 'Google Weather';
  44.   /**
  45.    * @var string Forecast feed url
  46.    */
  47.   private $feed 'http://www.google.com/ig/api?weather=%s&hl=%s';
  48.   /**
  49.    * @var string Language
  50.    */
  51.   protected $language;
  52.   /**
  53.    * @var string Default values
  54.    */
  55.   protected $defaults array(
  56.     'location' => 'London',
  57.     'units' => 'C',
  58.     'dateFormat' => "EEE d'&nbsp;'MMM yyyy",
  59.     'language' => 'en'
  60.   );
  61.   /**
  62.    * @var array Maps weather conditions to symbols
  63.    * Note: Probably not complete as the API is not documented
  64.    */
  65.   protected $symbolMap array(
  66.     'Chance of Rain' => 'rain.png',
  67.     'Chance of Showers' => 'showers.png',
  68.     'Chance of Snow' => 'snow.png',
  69.     'Chance of Storm' => 'storm.png',
  70.     'Chance of Tstorm' => 'thunderstorm.png',
  71.     'Clear' => 'sunny.png',
  72.     'Cloudy' => 'cloudy.png',
  73.     'Dust' => 'dust.png',
  74.     'Flurries' => 'snow.png',
  75.     'Fog' => 'fog.png',
  76.     'Haze' => 'haze.png',
  77.     'Icy' => 'icy.png',
  78.     'Light Rain' => 'rain_light',
  79.     'Mist' => 'mist.png',
  80.     'Mostly Cloudy' => 'partly_cloudy_day.png',
  81.     'Mostly Sunny' => 'partly_cloudy_day.png',
  82.     'Overcast' => 'overcast.png',
  83.     'Partly Cloudy' => 'partly_cloudy_day.png',
  84.     'Partly Sunny' => 'partly_cloudy_day.png',
  85.     'Rain' => 'rain.png',
  86.     'Rain Showers' => 'showers.png',
  87.     'Scattered Showers' => 'showers.png',
  88.     'Showers' => 'showers_heavy.png',
  89.     'Sleet' => 'sleet.png',
  90.     'Smoke' => 'smoke.png',
  91.     'Snow' => 'snow.png',
  92.     'Storm' => 'storm.png',
  93.     'Sunny' => 'sunny.png',
  94.     'Thunderstorm' => 'thunderstorm.png',
  95.   );
  96.  
  97.   /**
  98.    * Fetches the forecast from the provider's datasource
  99.    *
  100.    * Required by IWeatherForecastProvider interface
  101.    *
  102.    * @return WFWeatherForecast Forecast object
  103.    */
  104.   public function getForecast($params$symbols{
  105.     $this->init($params$symbols);
  106.  
  107.     $forecast new WFWeatherForecast();
  108.     $dateFormatter Yii::app()->dateFormatter;
  109.  
  110.     $this->read(vsprintf($this->feed,  array($this->location$this->language)));
  111.  
  112.     $contextNode $this->xpath->evaluate('/xml_api_reply/weather')->item(0);
  113.  
  114.     $issued $this->xpath->evaluate('string(forecast_information/current_date_time/@data)'$contextNode);
  115.  
  116.     if (substr($issued-5== '+0100'{
  117.       $tz 'BST';
  118.     }
  119.     else {
  120.       $tz 'GMT';
  121.     }
  122.     $issued substr($issued020$tz;
  123.  
  124.     $forecast->provider $this->provider;
  125.     $forecast->issued $issued;
  126.     $forecast->location->name $this->xpath->evaluate('string(forecast_information/city/@data)'$contextNode);
  127.     $forecast->location->lat  $this->xpath->evaluate('string(forecast_information/latitude_e6/@data)'$contextNode);
  128.     $forecast->location->long $this->xpath->evaluate('string(forecast_information/longitude_e6/@data)'$contextNode);
  129.  
  130.     $day new WFDay();
  131.     $day->date 'Current Conditions';
  132.     $day->description $this->xpath->evaluate('string(current_conditions/condition/@data)'$contextNode);
  133.     $day->symbol $this->symbolsDir === false 'http://www.google.com' $this->xpath->evaluate('string(icon/@data)'$contextNode$this->symbolsDir $this->symbolMap[$day->description];
  134.     $t $this->xpath->evaluate('string(current_conditions/temp_f/@data)'$contextNode);
  135.     $day->temperature->value $this->units=='F'?$t:$this->f2c($t);
  136.     $day->temperature->units $this->units;
  137.  
  138.     $wind $this->xpath->evaluate('string(current_conditions/wind_condition/@data)'$contextNode);
  139.     preg_match('/([NESW]+) at (\d+)/i'$wind$matches);
  140.     $day->windSpeed->value $this->units=='F'?$matches[2]:$this->mph2kph($matches[2]);
  141.     $day->windSpeed->units $this->units=='F'?'mph':'kph';
  142.     $day->windDirection->value $matches[1];
  143.  
  144.     $humidity $this->xpath->evaluate('string(current_conditions/humidity/@data)'$contextNode);
  145.     preg_match('/(\d+)/i'$humidity$matches);
  146.     $day->humidity->value $matches[1];
  147.     $day->humidity->units '%';
  148.     $forecast->days->add(0$day);
  149.  
  150.     $items $this->xpath->evaluate('forecast_conditions'$contextNode);
  151.     for ($i 0$l $items->length$i $l;{
  152.       $contextNode $items->item($i++);
  153.       $day new WFDay();
  154.       $day->date $dateFormatter->format($this->dateFormat"+$i days");
  155.       $day->description $this->xpath->evaluate('string(condition/@data)'$contextNode);
  156.       $day->symbol $this->symbolsDir === false 'http://www.google.com' $this->xpath->evaluate('string(icon/@data)'$contextNode$this->symbolsDir $this->symbolMap[$day->description];
  157.  
  158.       $t $this->xpath->evaluate('string(high/@data)'$contextNode);
  159.       $day->maxTemperature->value $this->units=='F'?$t:$this->f2c($t);
  160.       $day->maxTemperature->units $this->units;
  161.  
  162.       $t $this->xpath->evaluate('string(low/@data)'$contextNode);
  163.       $day->minTemperature->value $this->units=='F'?$t:$this->f2c($t);
  164.       $day->minTemperature->units $this->units;
  165.  
  166.       $forecast->days->add($i$day);
  167.     }
  168.  
  169.     return $forecast;
  170.   }
  171. }
  172. ?>

Documentation generated on Tue, 13 Oct 2009 14:09:02 +0100 by phpDocumentor 1.4.2