Ticket #3303: mythweb-weather1.patch

File mythweb-weather1.patch, 12.2 KB (added by maverik044, 16 years ago)

updated for svn; fix radar map url; and minor clean-ups

  • modules/weather/includes/objects/Forecast.php

    diff -urp ./mythplugins/mythweb-orig/modules/weather/includes/objects/Forecast.php /var/www/mythweb/modules/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,$d) {
     28        $i = strpos($date, "/");
     29        $j = strpos($date, "/", $i+1);
     30        $month = substr($date,0,$i);
     31        $day   = substr($date,$i+1,$j-$i-1);
     32        $year  = substr($date,$j+1,2);
    2833
    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);
    34         $date_time_array   = getdate( $temp_date );
    35         $claimed_dayofweek = $date_time_array['wday'];
    36 
    37         if ($real_dayofweek != $claimed_dayofweek)
    38             $this->date = date('m/d/Y', mktime(0, 0, 0, $month  , $day+1, $year));
    39         else
    40             $this->date = $date;
     34        $this->date = date('m/d/Y', mktime(0,0,0,$month,$day+$d,$year));
    4135    }
    4236}
  • modules/weather/includes/objects/WeatherSite.php

    diff -urp ./mythplugins/mythweb-orig/modules/weather/includes/objects/WeatherSite.php /var/www/mythweb/modules/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
     57        if($x == false) {
     58           return "N/A";
     59        }
     60
     61        $y = strpos($data,$Two,$x);
     62
     63        if($y == false) {
     64           return "N/A";
     65        }
     66
     67        $str = substr($data, $x, $y - $x);
     68          return $str;
     69    }
     70
    5471    function getData() {
    5572        global $Weather_Types;
    56         $data = @file('http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=' . $this->acid);
     73        $data=join('',file("http://xoap.weather.com/weather/local/".$this->acid."?cc=*&unit=F&dayf=5"));
    5774        if ($data === FALSE)
    5875            return;
    5976
    60         foreach($data as $line) {
    61             if(strpos($line, 'this.sw') === false)
    62                 continue;
    63             if(strpos($line, 'swTempCel') !== false)
    64                 continue;
    65 
    66             $line = trim($line);
    67             list($name, $value) = explode(' = "', $line);
    68             $name  = substr($name, 7);
    69             $value = substr($value, 0, strlen($value) - 2);
    70 
    71             switch ($name) {
    72                 case 'City':    $this->city               = $value;             break;
    73                 case 'SubDiv':  $this->subdiv             = $value;             break;
    74                 case 'Country': $this->country            = $value;             break;
    75                 case 'Region':  $this->region             = $value;             break;
    76                 case 'Temp':    $this->Temperature        = $value;             break;
    77                 case 'CIcon':   $this->CIcon              = $value;             break;
    78                 case 'WindS':   $this->WindSpeed          = $value;             break;
    79                 case 'WindD':   $this->WindDirection      = $value;             break;
    80                 case 'Baro':    $this->BarometricPressure = $value;             break;
    81                 case 'Humid':   $this->Humidity           = $value;             break;
    82                 case 'Real':    $this->Real               = $value;             break;
    83                 case 'UV':      $this->UV                 = $value;             break;
    84                 case 'Vis':     $this->Visibility         = $value;             break;
    85                 case 'LastUp':  $this->LastUpdated        = $value;             break;
    86                 case 'ConText': $this->ConText            = $value;             break;
    87                 case 'Fore':    $this->Forecast = $this->readForecast($value);  break;
    88                 default:        /* Weird, unknown type */                       break;
    89             }
    90         }
     77//
     78// Technically we should include the partner id and key
     79// if this stops working, change to the following:
     80// http://xoap.weather.com/weather/local/".$this->acid."?cc=*&unit=F&dayf=5&prod=xoap&par=1036905921&key=f68c2657a4fd3e2e"
     81// may need to get a new one from http://www.weather.com/services/xmloap.html
     82//
     83        $this->LastUpdated=$this->in("<lsup>","</lsup>",$data);
     84        $dnaminfo = $this->in("<dnam>","</dnam>",$data);
     85        $sep = strpos($dnaminfo, ',');
     86        if($sep != false) {
     87                $this->city = substr($dnaminfo, 0, $sep);
     88                $this->country = substr($dnaminfo, $sep + 1, strlen($dnaminfo) - $sep - 1);
     89        }
     90        $cc = $this->in("<cc>","</cc>",$data);
     91        $this->Temperature=$this->in("<tmp>","</tmp>",$cc);
     92        $this->Real=$this->in("<flik>","</flik>",$cc);
     93        $barinfo = $this->in("<bar>","</bar>",$cc);
     94        $this->BarometricPressure=$this->in("<r>","</r>",$barinfo);
     95        $windinfo = $this->in("<wind>","</wind>",$cc);
     96        $this->WindSpeed = $this->in("<s>","</s>",$windinfo);
     97        $this->WindDirection = $this->in("<t>","</t>",$windinfo);
     98        $this->Humidity = $this->in("<hmid>","</hmid>",$cc);
     99        $this->Visibility = $this->in("<vis>","</vis>",$cc);
     100        $uvinfo = $this->in("<uv>","</uv>",$cc);
     101        $this->UV = $this->in("<i>","</i>",$uvinfo);
     102        $this->CIcon = $this->in("<icon>","</icon>", $cc);
     103        $this->ConditionText = $this->in("<t>","</t>",$cc);
     104        $this->Forecast = $this->readForecast($data);
    91105
    92106    //Are we using metric or imperial system
    93107        if ($this->use_metric == 'YES') {
    94108            $this->Temperature        = round((5/9) * ($this->Temperature - 32));
    95109            $this->Real               = round((5/9) * ($this->Real - 32));
    96110            $this->BarometricPressure = round($this->BarometricPressure * 2.54);
    97             $this->Visibility         = round($this->Visibility * 1.609344);
     111            if (strpos($this->Visibility, 'Unlimited') != false) {
     112              $this->Visibility         = round($this->Visibility * 1.609344);
     113            }
    98114            $this->WindSpeed          = round($this->WindSpeed * 1.609344);
    99115        }
    100116
    101         if (strlen($this->ConText) > 0) {
    102             $this->ConditionText = $this->ConText;
    103             foreach ($Weather_Types as $pair) {
    104                 if ($pair[1] != $this->ConditionText)
    105                     continue;
    106                 $this->ConditionImage = $pair[0];
    107                 break;
    108             }
    109             if(strlen($this->ConditionImage) == 0)
    110                 list($this->ConditionImage, $blah) = $Weather_Types[$this->CIcon];
    111         }
    112         else {
    113             list($this->ConditionImage, $this->ConditionText) = $Weather_Types[$this->CIcon];
    114         }
    115         $this->ConditionImage = (strlen($this->ConditionImage) > 0) ? $this->ConditionImage : 'unknown.png';
     117//        if (strlen($this->ConText) > 0) {
     118//            $this->ConditionText = $this->ConText;
     119//            foreach ($Weather_Types as $pair) {
     120//                if ($pair[1] != $this->ConditionText)
     121//                    continue;
     122//                $this->ConditionImage = $pair[0];
     123//                break;
     124//            }
     125//            if(strlen($this->ConditionImage) == 0)
     126//                list($this->ConditionImage, $blah) = $Weather_Types[$this->CIcon];
     127//        }
     128//        else {
     129//            list($this->ConditionImage, $this->ConditionText) = $Weather_Types[$this->CIcon];
     130//        }
     131//        $this->ConditionImage = (strlen($this->ConditionImage) > 0) ? $this->ConditionImage : 'unknown.png';
     132        $this->ConditionImage = $this->CIcon.".png";
    116133    }
    117134
    118135    function getRadarURL() {
    119         @$data = file('http://w3.weather.com/weather/map/' . $this->acid . '?from=LAPmaps&setcookie=1');
     136        @$data = file('http://www.weather.com/weather/map/' . $this->acid . '?from=LAPmaps&setcookie=1');
    120137        if (is_array($data) && count($data)) {
    121138            foreach($data as $line) {
    122139                if(substr(trim($line), 0, 29) != 'if (isMinNS4) var mapNURL = "') continue;
    123140
    124141                $url1 = substr(trim($line), 30);
    125                 $url1 = 'http://w3.weather.com/' . substr($url1, 0, strlen($url1) - 2);
     142                $url1 = 'http://www.weather.com/' . substr($url1, 0, strlen($url1) - 2);
    126143
    127144                break;
    128145            }
    class WeatherSite { 
    142159    }
    143160
    144161    function readForecast($data) {
    145         global $Weather_Types;
    146162        $ret = array();
    147163
    148         $data = explode('|', $data);
    149164        for($i = 0;$i<5;$i++) {
    150         // mktime uses 0-6;  msnbc gives us 1-7;  adjust msnbc to match mktime
    151             $dayofweek = $data[$i] - 1;
    152             $forecast = new Forecast($data[5 + $i],$dayofweek);
    153             $forecast->dayofweek = $dayofweek;
    154             list($forecast->DescImage,$forecast->DescText) = $Weather_Types[$data[15 + $i]];
    155             $forecast->DescImage = (strlen($forecast->DescImage) > 0) ? $forecast->DescImage : 'unknown.png';
    156             $forecast->DescText  = (strlen($forecast->DescText)  > 0) ? $forecast->DescText  : t('Unknown') . ' (' . $data[15+$i] . ')';
    157             if($this->use_metric == 'YES') {
    158                 $forecast->HighTemperature = round((5/9) * ($data[20 + $i] - 32));
    159                 $forecast->LowTemperature = round((5/9) * ($data[40 + $i] -32 ));
    160             } else {
    161             $forecast->HighTemperature = $data[20 + $i];
    162             $forecast->LowTemperature = $data[40 + $i];
     165            $data1 = $this->in("<day d=\"".$i,"</day>",$data);
     166            $day = $this->in("<day d=\"".$i."\" t=\"","\" dt",$data);
     167            $forecast = new Forecast($this->LastUpdated, $i);
     168            switch ($day) {
     169            case 'Sunday': $forecast->dayofweek = 0; break;
     170            case 'Monday': $forecast->dayofweek = 1; break;
     171            case 'Tuesday': $forecast->dayofweek = 2; break;
     172            case 'Wednesday': $forecast->dayofweek = 3; break;
     173            case 'Thursday': $forecast->dayofweek = 4; break;
     174            case 'Friday': $forecast->dayofweek = 5; break;
     175            case 'Saturday': $forecast->dayofweek = 6; break;
    163176            }
     177           // $forecast->dayofweek = $i;
     178           
     179            $fc = $this->in("<part p=\"d\">","</part>",$data1);
     180            $forecast->DescText = $this->in("<t>","</t>",$fc);
     181            $icon = $this->in("<icon>","</icon>",$fc);
     182            $forecast->DescImage = $icon.".png";
     183
     184            $forecast->HighTemperature = $this->in("<hi>","</hi>",$data1);
     185            $forecast->LowTemperature = $this->in("<low>","</low>",$data1);
     186
     187            if($this->use_metric == 'YES') {
     188              if ($forecast->HighTemperature != 'N/A') {
     189                $forecast->HighTemperature = round((5/9) * ($forecast->HighTemperature - 32));
     190              }
     191              if ($forecast->LowTemperature != 'N/A') {
     192                $forecast->LowTemperature = round((5/9) * ($forecast->LowTemperature -32 ));
     193              }
     194            }
    164195
    165196            $ret[$i] = $forecast;
    166197        }
    167198
    168199        return $ret;
    169200    }
    170 }
    171  No newline at end of file
     201}
  • modules/weather/tmpl/default/weather.php

    diff -urp ./mythplugins/mythweb-orig/modules/weather/tmpl/default/weather.php /var/www/mythweb/modules/weather/tmpl/default/weather.php
    old new  
    7575                ?>)</td>
    7676        </tr>
    7777        </table>
     78
    7879    </div>
    7980
    8081    <div class="forecast clearfix">
     
    145146
    146147    </div>
    147148
    148     <p class="last_updated">
    149         <?php echo t('Last Updated') ?>: <?php echo $site->LastUpdated ?>
    150     </p>
     149    <table border="0" cellspacing="0" cellpadding="5">
     150      <tr>
     151       <th>
     152       <img src="<?php echo skin_url ?>img/weather/TWClogo_64px.png " class="alpha_png" />
     153       </th>
     154       <td>
     155           <h5><?php echo "Weather data provided by weather.com" ?></h5>
     156           <h5><?php echo t('Last Updated') ?>: <?php echo $site->LastUpdated ?></h5>
     157       </td>
     158      </tr>
     159    </table>
    151160</div>
    152161<?php
    153162    }