[mythtv-commits] Ticket #3303: Mythweb Weather throws error when MSNBC unavailable

MythTV mythtv at cvs.mythtv.org
Mon Apr 16 14:58:14 UTC 2007


#3303: Mythweb Weather throws error when MSNBC unavailable
-----------------------+----------------------------------------------------
 Reporter:  anonymous  |        Owner:  xris   
     Type:  defect     |       Status:  new    
 Priority:  minor      |    Milestone:  unknown
Component:  mythweb    |      Version:  head   
 Severity:  low        |   Resolution:         
-----------------------+----------------------------------------------------

Comment(by reallyasi9 [at] g[oogle]mail.com ):

 By the way, the following is the kludge I added.  In the handler.php file
 in modules/weather/, I had to change how the data were parsed, and some of
 the forecast parsing routine:


 {{{
 <?php
 /**
  * Handler for the Weather module.
  *
  * @url         $URL:
 svn+ssh://ijr@cvs.mythtv.org/var/lib/svn/trunk/mythplugins/mythweb/modules/weather/handler.php
 $
  * @date        $Date: 2006-04-02 03:40:11 -0400 (Sun, 02 Apr 2006) $
  * @version     $Revision: 9601 (PaSTE Kludge 01)$
  * @author      $Author: xris $
  * @license     GPL
  *
  * @package     MythWeb
  * @subpackage  Weather
  *
 /**/

 // Unit preference
     if (empty($_SESSION['siunits'])) {
         $_SESSION['siunits'] = setting('SIUnits');
     }

 /**
  * @global  array   $GLOBALS['Weather_Types']
  * @name    $Weather_Types
 /**/
     global $Weather_Types;
     $Weather_Types = array();

 // Load the weather data
     foreach (file(modules_path.'/'.module.'/weathertypes.dat') as $line) {
         list($id, $name, $img) = explode(',', $line);
         $Weather_Types[$id] = array($img, $name);
     }

 // Build a list of the known weather sites
     $WeatherSites = array();

     $sh = $db->query('SELECT data, hostname FROM settings WHERE
 value="locale"');
     while (list($data, $host) = $sh->fetch_row()) {
     // New data site
         if (empty($WeatherSites[$data])) {
             $WeatherSites[$data] = new WeatherSite($data, $hostname,
 $_SESSION['siunits']);
         }
     // Add the hostname to sites we've already seen
         else {
             $WeatherSites[$data]->host .= ', '.$host;
         }

     }
     $sh->finish();

 // Print the weather page template
     require_once tmpl_dir.'weather.php';

 // Exit
     exit;

 class WeatherSite {

     var $acid;
     var $host;

     var $city;
     var $subdiv;
     var $country;
     var $region;

     var $use_metric;
     var $Temperature;
     var $CIcon;
     var $ConditionImage;
     var $ConditionText;
     var $WindSpeed;
     var $WindDirection;
     var $BarometricPressure;
     var $PressureRising;
     var $Humidity;
     var $Real;
     var $UV;
     var $Visibility;
     var $ConText;
     var $Forecast;

     var $RadarImage;

     var $LastUpdated;

     function WeatherSite($data, $hostname, $use_metric) {
         $this->acid       = $data;
         $this->host       = $hostname;
         $this->use_metric = $use_metric;

         $this->getData();
         $this->RadarImage = $this->getRadarURL();
     }

     function getData() {
     $data = file("http://weather.yahooapis.com/forecastrss?p=" .
 $this->acid);

     $forecastday = -1;
     foreach($data as $line) {
         if(strpos($line, "yweather:") === false)
         continue;

         $line = trim($line);
         preg_match_all('/
 ([a-z]+)="(.*?)"/',$line,$matches,PREG_SET_ORDER);
         foreach($matches as $n => $match) {
                 $name = $match[1];
                 $value = $match[2];

                 switch ($name) {
                 case "city":
                     $this->city = $value;
                     break;
 /*
                 case "SubDiv":
                     $this->subdiv = $value;
                     break;
 */
                 case "country":
                     $this->country = $value;
                     break;

                 case "region":
                     $this->region = $value;
                 break;

                 case "temp":
                     $this->Temperature = $value;
                     break;

                 case "code":
                     $this->CIcon = $value;
                     break;

                 case "speed":
                     $this->WindSpeed = $value;
                     break;

                 case "direction":
                     $this->WindDirection = $value;
                     break;

                 case "pressure":
                     $this->BarometricPressure = $value;
                     break;

                 case "rising":
                     $this->PressureRising = $value;
                     break;

                 case "humidity":
                     $this->Humidity = $value;
                     break;

                 case "chill":
                     $this->Real = $value;
                     break;
 /*
                 case "UV":
                     $this->UV = $value;
                     break;
 */
                 case "visibility":
                     $this->Visibility = $value;
                     break;

                 case "date":
                     if (!$this->LastUpdated) $this->LastUpdated = $value;
                     break;

                 case "text":
                     if (!$this->ConText) $this->ConText = $value;
                     break;
 /*
                 case "Fore":
                     $this->Forecast = $this->readForecast($value);
                     break;
 */
                 default:
                    // Weird, unknown type
                    break;
                 }
         }
         // Forecast
         if (stripos($line,'yweather:forecast') === false) continue;
         $forecastday++;
         preg_match_all('/
 ([a-z]+)="(.*?)"/',$line,$matches,PREG_SET_ORDER);
         foreach($matches as $n => $match) {
                 $name = $match[1];
                 $value = $match[2];

                 switch ($name) {

                 case 'day':
                         $wonk[$forecastday][0] = $value;
                         break;

                 case 'date':
                         $wonk[$forecastday][1] = $value;
                         break;

                 case 'low':
                         $wonk[$forecastday][5] = $value;
                         break;

                 case 'high':
                         $wonk[$forecastday][4] = $value;
                         break;

                 case 'text':
                         $wonk[$forecastday][3] = $value;
                         break;

                 case 'code':
                         $wonk[$forecastday][2] = $value;
                         break;

                 default:
                         break;
                 }
         }
     }
     ksort($wonk);
     foreach($wonk as $datarray) {
         ksort($datarray);
         foreach ($datarray as $datum) {
             if ($datastring) $datastring .= "|";
             $datastring .= $datum;
         }
     }
     $this->Forecast = $this->readForecast($datastring);


     //Are we using metric or imperial system
     if($this->use_metric == "YES") {
         $this->Temperature = round((5/9) * ($this->Temperature - 32));
         $this->Real = round((5/9) * ($this->Real - 32));
         $this->BarometricPressure = round($this->BarometricPressure *
 2.54);
         $this->Visibility = round($this->Visibility * 1.609344);
         $this->WindSpeed = round($this->WindSpeed * 1.609344);
     }

     if(strlen($this->ConText) > 0) {
         $this->ConditionText = $this->ConText;
         $this->ConditionImage = getImageFromName($this->ConditionText);
         if(strlen($this->ConditionImage) == 0)
         list($this->ConditionImage, $blah) =
 getImageAndDescFromId($this->CIcon);
     } else {
         list($this->ConditionImage, $this->ConditionText) =
 getImageAndDescFromId($this->CIcon);
     }
     $this->ConditionImage = (strlen($this->ConditionImage) > 0) ?
 $this->ConditionImage : "unknown.png";
     }

     function getRadarURL() {
     $data = file("http://w3.weather.com/weather/map/" . $this->acid .
 "?from=LAPmaps&setcookie=1");
     foreach($data as $line) {
         if(substr(trim($line), 0, 29) != "if (isMinNS4) var mapNURL = \"")
 continue;

         $url1 = substr(trim($line), 30);
         $url1 = "http://w3.weather.com/" . substr($url1, 0, strlen($url1)
 - 2);

         break;
     }

     $data = file($url1);
     foreach($data as $line) {
         if(substr(trim($line), 0, 48) != "<IMG NAME=\"mapImg\"
 SRC=\"http://image.weather.com") continue;

         $url2 = substr(trim($line), 24);
         $url2 = substr($url2, 0, strpos($url2, '"'));
         break;
     }
     return $url2;
     }

     function readForecast($data) {
     $ret = array();

     $data = explode("|", $data);
     # XXX
     #format: dow|date|code|text|high|low|etc...
     for($i = 0;$i<2;$i++) {
         # mktime uses 0-6;  msnbc gives us 1-7;  adjust msnbc to match
 mktime
         switch ($data[6*$i]) {
         case 'Sun':
                 $dayofweek = 0;
                 break;
         case 'Mon':
                 $dayofweek = 1;
                 break;
         case 'Tue':
                 $dayofweek = 2;
                 break;
         case 'Wed':
                 $dayofweek = 3;
                 break;
         case 'Thu':
                 $dayofweek = 4;
                 break;
         case 'Fri':
                 $dayofweek = 5;
                 break;
         case 'Sat':
                 $dayofweek = 6;
                 break;
         }
         $datestring = strtotime($data[6*$i + 1]);
         $datestring = date("mdY",$datestring);
         $forecast = new Forecast($datestring,$dayofweek);
         $forecast->dayofweek = $dayofweek;
         list($forecast->DescImage,$forecast->DescText) =
 getImageAndDescFromId($data[6*$i + 2]);
         $forecast->DescImage = (strlen($forecast->DescImage) > 0) ?
 $forecast->DescImage : "unknown.png";
         $forecast->DescText = (strlen($forecast->DescText) > 0) ?
 $forecast->DescText : t('Unknown') . " (" . $data[6*$i + 3] . ")";
         if($this->use_metric == "YES") {
             $forecast->HighTemperature = round((5/9) * ($data[6*$i + 4] -
 32));
             $forecast->LowTemperature = round((5/9) * ($data[6*$i + 5] -32
 ));
         } else {
         $forecast->HighTemperature = $data[6*$i + 4];
         $forecast->LowTemperature = $data[6*$i + 5];
         }

         $ret[$i] = $forecast;
     }

     return $ret;
     }
 }

 function getImageAndDescFromId($my_id) {
     global $Weather_Types;
     return $Weather_Types[$my_id];
 }

 function getImageFromName($my_name) {
     global $Weather_Types;
     foreach ($Weather_Types as $pair) {
         if ($pair[1] == $my_name)
             return $pair[0];
     }
 }

 class Forecast {
     var $date;
     var $dayofweek;

     var $DescImage;
     var $DescText;

     var $HighTemperature;
     var $LowTemperature;

     function Forecast($date,$real_dayofweek) {

     $month = substr($date,0,2);
     $day = substr($date,3,2);
     $year = substr($date,6,4);

     $temp_date = mktime(0,0,0,$month,$day,$year);
     $date_time_array = getdate( $temp_date );
     $claimed_dayofweek = $date_time_array['wday'];

     if($real_dayofweek != $claimed_dayofweek)

         $this->date = date("m/d/Y", mktime(0, 0, 0, $month  , $day+1,
 $year));
     else
         $this->date = $date;
     }
 }

 }}}

-- 
Ticket URL: <http://svn.mythtv.org/trac/ticket/3303#comment:7>
MythTV <http://svn.mythtv.org/trac>
MythTV


More information about the mythtv-commits mailing list