Ticket #3303: weather.patch

File weather.patch, 7.7 KB (added by maverik044, 18 years ago)

Preliminary patch see comment below; forecast does not work yet...

  • weather/includes/objects/Forecast.php

    diff -rup /home/mythtv/mythtv/myth_src/13238/mythplugins/mythweb/modules/weather/includes/objects/Forecast.php ./weather/includes/objects/Forecast.php
    old new class Forecast { 
    2424    var $HighTemperature;
    2525    var $LowTemperature;
    2626
    27     function Forecast($date, $real_dayofweek) {
     27    function Forecast($date, $i) {
     28       
     29        $month = substr($date,2,2);
     30        $day   = substr($date,0,1);
     31        $year  = substr($date,5,2);
    2832
    29         $month = substr($date,0,2);
    30         $day   = substr($date,3,2);
    31         $year  = substr($date,6,4);
    32 
    33         $temp_date         = mktime(0,0,0,$month,$day,$year);
     33        $temp_date         = mktime(0,0,0,$month,$day,$year) + $i*3600*24;
    3434        $date_time_array   = getdate( $temp_date );
    3535        $claimed_dayofweek = $date_time_array['wday'];
    3636
  • weather/includes/objects/WeatherSite.php

    diff -rup /home/mythtv/mythtv/myth_src/13238/mythplugins/mythweb/modules/weather/includes/objects/WeatherSite.php ./weather/includes/objects/WeatherSite.php
    old new class WeatherSite { 
    5151        $this->RadarImage = $this->getRadarURL();
    5252    }
    5353
     54    function in($One,$Two,$data) {
     55        $x = strpos($data,$One) + strlen($One);
     56        $y = strpos($data,$Two);
     57
     58        if($x == false || $y == false) {
     59           return "N/A";
     60        }
     61
     62        $str = substr($data, $x, $y - $x);
     63          return $str;
     64    }
     65
    5466    function getData() {
    5567        global $Weather_Types;
    56         $data = file('http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=' . $this->acid);
     68        $data=join('',file("http://xoap.weather.com/weather/local/".$this->acid."?cc=*&unit=F&dayf=5"));
    5769
    58         foreach($data as $line) {
    59             if(strpos($line, 'this.sw') === false)
    60                 continue;
    61             if(strpos($line, 'swTempCel') !== false)
    62                 continue;
    63 
    64             $line = trim($line);
    65             list($name, $value) = explode(' = "', $line);
    66             $name  = substr($name, 7);
    67             $value = substr($value, 0, strlen($value) - 2);
    68 
    69             switch ($name) {
    70                 case 'City':    $this->city               = $value;             break;
    71                 case 'SubDiv':  $this->subdiv             = $value;             break;
    72                 case 'Country': $this->country            = $value;             break;
    73                 case 'Region':  $this->region             = $value;             break;
    74                 case 'Temp':    $this->Temperature        = $value;             break;
    75                 case 'CIcon':   $this->CIcon              = $value;             break;
    76                 case 'WindS':   $this->WindSpeed          = $value;             break;
    77                 case 'WindD':   $this->WindDirection      = $value;             break;
    78                 case 'Baro':    $this->BarometricPressure = $value;             break;
    79                 case 'Humid':   $this->Humidity           = $value;             break;
    80                 case 'Real':    $this->Real               = $value;             break;
    81                 case 'UV':      $this->UV                 = $value;             break;
    82                 case 'Vis':     $this->Visibility         = $value;             break;
    83                 case 'LastUp':  $this->LastUpdated        = $value;             break;
    84                 case 'ConText': $this->ConText            = $value;             break;
    85                 case 'Fore':    $this->Forecast = $this->readForecast($value);  break;
    86                 default:        /* Weird, unknown type */                       break;
    87             }
    88         }
     70        $this->LastUpdated=$this->in("<lsup>","</lsup>",$data);
     71        $dnaminfo = $this->in("<dnam>","</dnam>",$data);
     72        $sep = strpos($dnaminfo, ',');
     73        if($sep != false) {
     74                $this->city = substr($dnaminfo, 0, $sep);
     75                $this->subdiv = substr($dnaminfo, $sep + 1, strlen($dnaminfo) - $sep - 1);
     76                $this->country = " ";
     77                $this->region = " ";
     78        }
     79        $this->Condition=$this->in("<t>","</t>",$data);
     80        $this->Temperature=$this->in("<tmp>","</tmp>",$data);
     81        $this->Real=$this->in("<flik>","</flik>",$data);
     82        $barinfo = $this->in("<bar>","</bar>",$data);
     83        $this->BarometricPressure=$this->in("<r>","</r>",$barinfo);
     84        $windinfo = $this->in("<wind>","</wind>",$data);
     85        $this->WindSpeed = $this->in("<s>","</s>",$windinfo);
     86        $this->WindDirection = $this->in("<t>","</t>",$windinfo);
     87        $this->Humidity = $this->in("<hmid>","</hmid>",$data);
     88        $this->Visibility = $this->Humidity;
     89        $this->Visibility = $this->in("<vis>","</vis>",$data);
     90        $uvinfo = $this->in("<uv>","</uv>",$data);
     91        $this->UV = $this->in("<i>","</i>",$uvinfo);
     92        $this->CIcon = $this->in("<icon>","</icon>", $data);
     93        $condinfo = $this->in("<cc>","<bar>",$data);
     94        $this->ConText = $this->in("<t>","</t>",$condinfo);
     95        $this->Forecast = $this->readForecast($data);
    8996
    9097    //Are we using metric or imperial system
    9198        if ($this->use_metric == 'YES') {
    9299            $this->Temperature        = round((5/9) * ($this->Temperature - 32));
    93100            $this->Real               = round((5/9) * ($this->Real - 32));
    94101            $this->BarometricPressure = round($this->BarometricPressure * 2.54);
     102        if (strpos($this-Visibility, 'Unlimited') == false) {
    95103            $this->Visibility         = round($this->Visibility * 1.609344);
     104        }
    96105            $this->WindSpeed          = round($this->WindSpeed * 1.609344);
    97106        }
    98107
    class WeatherSite { 
    139148        global $Weather_Types;
    140149        $ret = array();
    141150
    142         $data = explode('|', $data);
    143151        for($i = 0;$i<5;$i++) {
    144152        // mktime uses 0-6;  msnbc gives us 1-7;  adjust msnbc to match mktime
    145             $dayofweek = $data[$i] - 1;
    146             $forecast = new Forecast($data[5 + $i],$dayofweek);
    147             $forecast->dayofweek = $dayofweek;
    148             list($forecast->DescImage,$forecast->DescText) = $Weather_Types[$data[15 + $i]];
    149             $forecast->DescImage = (strlen($forecast->DescImage) > 0) ? $forecast->DescImage : 'unknown.png';
    150             $forecast->DescText  = (strlen($forecast->DescText)  > 0) ? $forecast->DescText  : t('Unknown') . ' (' . $data[15+$i] . ')';
     153            $data1 = $this->in("<day d=\"".$i,"</day>",$data);
     154            $forecast = new Forecast($this->LastUp,$i);
     155            $forecast->dayofweek = $i;
     156           
     157            $fc = $this->in("<part p=\"d","</part>",$data1);
     158            $forecast->DescText = $this->in("<t>","</t>",$fc);
     159            $forecast->DescImage = 'unknown.png';
     160
     161//            list($forecast->DescImage,$forecast->DescText) = $Weather_Types[$data[15 + $i]];
     162            $forecast->HighTemperature = $this->in("<hi>","</hi>",$data1);
     163            $forecast->LowTemperature = $this->in("<low>","</low>",$data1);
     164
    151165            if($this->use_metric == 'YES') {
    152                 $forecast->HighTemperature = round((5/9) * ($data[20 + $i] - 32));
    153                 $forecast->LowTemperature = round((5/9) * ($data[40 + $i] -32 ));
    154             } else {
    155             $forecast->HighTemperature = $data[20 + $i];
    156             $forecast->LowTemperature = $data[40 + $i];
    157             }
     166                $forecast->HighTemperature = round((5/9) * ($forecast->HighTemperature - 32));
     167                $forecast->LowTemperature = round((5/9) * ($forecast->LowTemperature -32 ));
     168            }
    158169
    159170            $ret[$i] = $forecast;
    160171        }
    161172
    162173        return $ret;
    163174    }