Ticket #3337: WeatherSite.php

File WeatherSite.php, 6.3 KB (added by maverik044, 13 years ago)

Support script. Same as patched version to Ticket#3303, with a few changes for more data

Line 
1<?php
2/**
3 * WeatherSite class for MythWeb's Weather module
4 *
5 * @url         $URL: http://svn.mythtv.org/svn/trunk/mythplugins/mythweb/modules/weather/includes/objects/WeatherSite.php $
6 * @date        $Date: 2006-12-19 03:17:33 -0500 (Tue, 19 Dec 2006) $
7 * @version     $Revision: 12295 $
8 * @author      $Author: xris $
9 * @license     GPL
10 *
11 * @package     MythWeb
12 * @subpackage  Weather
13 *
14/**/
15
16class WeatherSite {
17
18    var $acid;
19    var $hosts = array();
20
21    var $city;
22    var $subdiv;
23    var $country;
24    var $region;
25    var $location;
26
27    var $use_metric;
28    var $Temperature;
29    var $CIcon;
30    var $ConditionImage;
31    var $ConditionText;
32    var $WindSpeed;
33    var $WindDirection;
34    var $WindDegrees;
35    var $WindGust;
36    var $BarometricPressure;
37    var $Humidity;
38    var $DewPoint;
39    var $Real;
40    var $UV;
41    var $Visibility;
42    var $ConText;
43    var $Forecast;
44
45    var $RadarImage;
46
47    var $LastUpdated;
48
49    function WeatherSite($data, $hostname, $use_metric) {
50        $this->acid       = $data;
51        $this->use_metric = $use_metric;       
52        $this->hosts[]    = $hostname;
53        $this->getData();
54        $this->RadarImage = $this->getRadarURL();
55    }
56
57    function in($One,$Two,$data) {
58        $x = strpos($data,$One) + strlen($One);
59
60        if ($x == false) {
61           return "N/A";
62        }
63
64        $y = strpos($data,$Two,$x);
65
66        if ($y == false) {
67           return "N/A";
68        }
69
70        $str = substr($data, $x, $y - $x);
71          return $str;
72    }
73
74    function getData() {
75        global $Weather_Types;
76
77        $data=join('',file("http://xoap.weather.com/weather/local/".$this->acid."?cc=*&unit=F&dayf=6"));
78
79//
80// Technically we should include the partner id and key
81// if this stops working, change to the following:
82// http://xoap.weather.com/weather/local/".$this->acid."?cc=*&unit=F&dayf=5&prod=xoap&par=1036905921&key=f68c2657a4fd3e2e"
83// may need to get a new one from http://www.weather.com/services/xmloap.html
84//
85        $this->LastUpdated=$this->in("<lsup>","</lsup>",$data);
86        $dnaminfo = $this->in("<dnam>","</dnam>",$data);
87        $sep = strpos($dnaminfo, ',');
88        $this->location = $dnaminfo;
89        if($sep != false) {
90                $this->city = substr($dnaminfo, 0, $sep);
91                $this->country = substr($dnaminfo, $sep + 1, strlen($dnaminfo) - $sep - 1);
92        }
93        $cc = $this->in("<cc>","</cc>",$data);
94        $this->Temperature=$this->in("<tmp>","</tmp>",$cc);
95        $this->Real=$this->in("<flik>","</flik>",$cc);
96        $barinfo = $this->in("<bar>","</bar>",$cc);
97        $this->BarometricPressure=$this->in("<r>","</r>",$barinfo);
98        $windinfo = $this->in("<wind>","</wind>",$cc);
99        $this->WindSpeed = $this->in("<s>","</s>",$windinfo);
100        $this->WindDirection = $this->in("<t>","</t>",$windinfo);
101        $this->WindDegrees = $this->in("<d>","</d>",$windinfo);
102        $this->WindGust = $this->in("<gust>", "</gust>", $windinfo);
103        $this->Humidity = $this->in("<hmid>","</hmid>",$cc);
104        $this->Visibility = $this->in("<vis>","</vis>",$cc);
105        $uvinfo = $this->in("<uv>","</uv>",$cc);
106        $this->UV = $this->in("<i>","</i>",$uvinfo);
107        $this->CIcon = $this->in("<icon>","</icon>", $cc);
108        $this->DewPoint = $this->in("<dewp>", "</dewp>", $cc);
109        $this->ConditionText = $this->in("<t>","</t>",$cc);
110        $this->Forecast = $this->readForecast($data);
111
112    //Are we using metric or imperial system
113        if ($this->use_metric == 'YES') {
114            $this->Temperature        = round((5/9) * ($this->Temperature - 32));
115            $this->DewPoint           = round((5/9) * ($this->DewPoint - 32));
116            $this->Real               = round((5/9) * ($this->Real - 32));
117            $this->BarometricPressure = round($this->BarometricPressure * 2.54);
118            if (strpos($this->Visibility, 'Unlimited') != false) {
119              $this->Visibility         = round($this->Visibility * 1.609344);
120        }
121            $this->WindSpeed          = round($this->WindSpeed * 1.609344);
122            $this->WindGust           = round($this->WindGust * 1.609344);
123
124        }
125
126     $this->ConditionImage = $this->CIcon.".png";
127    }
128
129    function getRadarURL() {
130        $data = file('http://w3.weather.com/weather/map/' . $this->acid . '?from=LAPmaps&setcookie=1');
131        foreach($data as $line) {
132            if(substr(trim($line), 0, 29) != 'if (isMinNS4) var mapNURL = "') continue;
133
134            $url1 = substr(trim($line), 30);
135            $url1 = 'http://w3.weather.com/' . substr($url1, 0, strlen($url1) - 2);
136
137            break;
138        }
139
140        $data = file($url1);
141        foreach($data as $line) {
142            if(substr(trim($line), 0, 48) != '<IMG NAME="mapImg" SRC="http://image.weather.com') continue;
143
144            $url2 = substr(trim($line), 24);
145            $url2 = substr($url2, 0, strpos($url2, '"'));
146            break;
147        }
148        return $url2;
149    }
150
151    function readForecast($data) {
152        $ret = array();
153
154        for($i = 0;$i<6;$i++) {
155            $data1 = $this->in("<day d=\"".$i,"</day>",$data);
156            $day = $this->in("<day d=\"".$i."\" t=\"","\" dt",$data);
157            $forecast = new Forecast($this->LastUpdated, $i);
158            switch ($day) {
159            case 'Sunday': $forecast->dayofweek = 0; break;
160            case 'Monday': $forecast->dayofweek = 1; break;
161            case 'Tuesday': $forecast->dayofweek = 2; break;
162            case 'Wednesday': $forecast->dayofweek = 3; break;
163            case 'Thursday': $forecast->dayofweek = 4; break;
164            case 'Friday': $forecast->dayofweek = 5; break;
165            case 'Saturday': $forecast->dayofweek = 6; break;
166            }
167           // $forecast->dayofweek = $i;
168           
169            $fc = $this->in("<part p=\"d\">","</part>",$data1);
170            $forecast->DescText = $this->in("<t>","</t>",$fc);
171            $icon = $this->in("<icon>","</icon>",$fc);
172            $forecast->DescImage = $icon.".png";
173
174            $forecast->HighTemperature = $this->in("<hi>","</hi>",$data1);
175            $forecast->LowTemperature = $this->in("<low>","</low>",$data1);
176
177            if($this->use_metric == 'YES') {
178              if ($forecast->HighTemperature != 'N/A') {
179                $forecast->HighTemperature = round((5/9) * ($forecast->HighTemperature - 32));
180              }
181              if ($forecast->LowTemperature != 'N/A') {
182                $forecast->LowTemperature = round((5/9) * ($forecast->LowTemperature -32 ));
183              }
184            }
185
186            $ret[$i] = $forecast;
187        }
188
189        return $ret;
190    }
191}