Ticket #4171: handler.php

File handler.php, 8.5 KB (added by anonymous, 18 years ago)

/var/www/html/mythweb/modules/weather/handler.php

Line 
1<?php
2/**
3 * Handler for the Weather module.
4 *
5 * @url $URL: svn+ssh://ijr@cvs.mythtv.org/var/lib/svn/trunk/mythplugins/mythweb/modules/weather/handler.php $
6 * @date $Date: 2006-04-02 03:40:11 -0400 (Sun, 02 Apr 2006) $
7 * @version $Revision: 9601 $
8 * @author $Author: xris $
9 * @license GPL
10 *
11 * @package MythWeb
12 * @subpackage Weather
13 *
14/**/
15
16// Unit preference
17 if (empty($_SESSION['siunits'])) {
18 $_SESSION['siunits'] = setting('SIUnits');
19 }
20
21/**
22 * @global array $GLOBALS['Weather_Types']
23 * @name $Weather_Types
24/**/
25 global $Weather_Types;
26 $Weather_Types = array();
27
28// Load the weather data
29 foreach (file(modules_path.'/'.module.'/weathertypes.dat') as $line) {
30 list($id, $name, $img) = explode(',', $line);
31 $Weather_Types[$id] = array($img, $name);
32 }
33
34// Build a list of the known weather sites
35 $WeatherSites = array();
36
37 $sh = $db->query('SELECT data, hostname FROM settings WHERE value="locale"');
38 while (list($data, $host) = $sh->fetch_row()) {
39 // New data site
40 if (empty($WeatherSites[$data])) {
41 $WeatherSites[$data] = new WeatherSite($data, $hostname, $_SESSION['siunits']);
42 }
43 // Add the hostname to sites we've already seen
44 else {
45 $WeatherSites[$data]->host .= ', '.$host;
46 }
47
48 }
49 $sh->finish();
50
51// Print the weather page template
52 require_once tmpl_dir.'weather.php';
53
54// Exit
55 exit;
56
57class WeatherSite {
58
59 var $acid;
60 var $host;
61
62 var $city;
63 var $subdiv;
64 var $country;
65 var $region;
66
67 var $use_metric;
68 var $Temperature;
69 var $CIcon;
70 var $ConditionImage;
71 var $ConditionText;
72 var $WindSpeed;
73 var $WindDirection;
74 var $BarometricPressure;
75 var $Humidity;
76 var $Real;
77 var $UV;
78 var $Visibility;
79 var $ConText;
80 var $Forecast;
81
82 var $RadarImage;
83
84 var $LastUpdated;
85
86 var $Sunrise;
87 var $Sunset;
88
89 function WeatherSite($data, $hostname, $use_metric) {
90 $this->acid = $data;
91 $this->host = $hostname;
92 $this->use_metric = $use_metric;
93
94 $this->getData();
95 $this->RadarImage = $this->getRadarURL();
96 }
97
98 function getData() {
99$myfile = fopen("/tmp/weather", "w");
100
101 //$data = file("http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=" . $this->acid);
102 $data = file("http://xml.weather.yahoo.com/forecastrss?p=" . $this->acid);
103
104 //$xml = simplexml_load_file("http://xml.weather.yahoo.com/forecastrss?p=" . $this->acid);
105 $xml = simplexml_load_string(implode("\n",$data));
106
107fwrite($myfile, "xml = ".print_r($xml,TRUE)."\n");
108 $this->LastUpdated = implode(',', $xml->xpath("//lastBuildDate"));
109 $this->city = implode(',', $xml->xpath('//yweather:location/@city'));
110 $this->subdiv = implode(',', $xml->xpath("//yweather:location/@region"));
111 $this->country = implode(',', $xml->xpath("//yweather:location/@country"));
112 $this->region = implode(',', $xml->xpath("//yweather:location/@region"));
113 $this->Real = implode(',', $xml->xpath("//yweather:wind/@chill"));
114 $dir_str = array('N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N');
115 $temp = $xml->xpath("//yweather:wind/@direction");
116 $this->WindDirection = $dir_str[($temp[0] - 22.5) / 45 ];
117 $this->WindSpeed = implode(',', $xml->xpath("//yweather:wind/@speed"));
118 $this->Humidity=implode(',',$xml->xpath("//yweather:atmosphere/@humidity"));
119 $this->Visibility = implode(',',
120 $xml->xpath("//yweather:atmosphere/@visibility")) / 100;
121 $this->BarometricPressure = implode(',',
122 $xml->xpath("//yweather:atmosphere/@pressure"));
123 $this->Sunrise = implode(',', $xml->xpath("//yweather:astronomy/@sunrise"));
124 $this->Sunset = implode(',', $xml->xpath("//yweather:astronomy/@sunset"));
125 $this->ConText = implode(',', $xml->xpath("//yweather:condition/@text"));
126 $this->Temperature=implode(',', $xml->xpath("//yweather:condition/@temp"));
127 //$this->LastUpdated=implode(',',$xml->xpath("//yweather:condition/@date"));
128 //$this->CIcon = implode(',', $xml->xpath("//img/@src"));
129 //$this->UV = implode(',', $xml->xpath("//@unknown"));
130$temp = implode('|', $xml->xpath("//yweather:forecast/@*"));
131fwrite($myfile, "data = ".print_r($temp,TRUE)."\n");
132 $this->Forecast = $this->readForecast(
133 implode('|', $xml->xpath("//yweather:forecast/@*")));
134
135fwrite($myfile, "this = ".print_r($this,TRUE)."\n");
136fclose($myfile);
137
138
139 foreach($data as $line) {
140 //if(strpos($line, "this.sw") === false)
141 if(strpos($line, "yweather:") === false)
142 continue;
143 //if(strpos($line, "swTempCel") !== false)
144 //continue;
145
146 $line = trim($line);
147 list($name, $value) = explode(" = \"", $line);
148 $name = substr($name, 7);
149 $value = substr($value, 0, strlen($value) - 2);
150
151 switch ($name) {
152 default:
153 // Weird, unknown type
154 break;
155 }
156 }
157
158 //Are we using metric or imperial system
159 if($this->use_metric == "YES") {
160 $this->Temperature = round((5/9) * ($this->Temperature - 32));
161 $this->Real = round((5/9) * ($this->Real - 32));
162 $this->BarometricPressure = round($this->BarometricPressure * 2.54);
163 $this->Visibility = round($this->Visibility * 1.609344);
164 $this->WindSpeed = round($this->WindSpeed * 1.609344);
165 }
166
167 if(strlen($this->ConText) > 0) {
168 $this->ConditionText = $this->ConText;
169 $this->ConditionImage = getImageFromName($this->ConditionText);
170 if(strlen($this->ConditionImage) == 0)
171 list($this->ConditionImage, $blah) = getImageAndDescFromId($this->CIcon);
172 } else {
173 list($this->ConditionImage, $this->ConditionText) = getImageAndDescFromId($this->CIcon);
174 }
175 $this->ConditionImage = (strlen($this->ConditionImage) > 0) ? $this->ConditionImage : "unknown.png";
176 }
177
178 function getRadarURL() {
179 $data = file("http://www.weather.com/weather/map/" . $this->acid . "?from=LAPmaps&setcookie=1");
180 foreach($data as $line) {
181 if(substr(trim($line), 0, 29) != "if (isMinNS4) var mapNURL = \"") continue;
182
183 $url1 = substr(trim($line), 30);
184 $url1 = "http://www.weather.com/" . substr($url1, 0, strlen($url1) - 2);
185
186 break;
187 }
188
189 $data = file($url1);
190 foreach($data as $line) {
191 if(substr(trim($line), 0, 48) != "<IMG NAME=\"mapImg\" SRC=\"http://image.weather.com") continue;
192
193 $url2 = substr(trim($line), 24);
194 $url2 = substr($url2, 0, strpos($url2, '"'));
195 break;
196 }
197 return $url2;
198 }
199
200 function readForecast($data) {
201 $ret = array();
202
203 $data = explode("|", $data);
204 for($i = 0;$i<count($data);$i+=6) {
205 $dayofweek = $data[$i];
206 $forecast = new Forecast($data[1 + $i],$dayofweek);
207 //$forecast->dayofweek = $dayofweek;
208 list($forecast->DescImage,$forecast->DescText) = getImageAndDescFromId($data[5 + $i]);
209 $forecast->DescImage = (strlen($forecast->DescImage) > 0) ? $forecast->DescImage : "unknown.png";
210 $forecast->DescText = (strlen($forecast->DescText) > 0) ? $forecast->DescText : t('Unknown') . " (" . $data[5+$i] . ")";
211 $forecast->DescText = $forecast->DescText."<p>(".$data[5+$i]." - ".$data[4+$i].")";
212 if($this->use_metric == "YES") {
213 $forecast->HighTemperature = round((5/9) * ($data[3 + $i] - 32));
214 $forecast->LowTemperature = round((5/9) * ($data[2 + $i] -32 ));
215 } else {
216 $forecast->HighTemperature = $data[3 + $i];
217 $forecast->LowTemperature = $data[2 + $i];
218 }
219
220 $ret[$i / 6] = $forecast;
221 }
222
223 return $ret;
224 }
225}
226
227function getImageAndDescFromId($my_id) {
228 global $Weather_Types;
229 return $Weather_Types[$my_id];
230}
231
232function getImageFromName($my_name) {
233 global $Weather_Types;
234 foreach ($Weather_Types as $pair) {
235 if ($pair[1] == $my_name)
236 return $pair[0];
237 }
238}
239
240class Forecast {
241 var $date;
242 var $dayofweek;
243
244 var $DescImage;
245 var $DescText;
246
247 var $HighTemperature;
248 var $LowTemperature;
249
250 function Forecast($date,$real_dayofweek) {
251
252 //$date_time_array = date_parse($date);
253 $date_time_array = getdate(strtotime($date));
254 $month = $date_time_array["month"];
255 $day = $date_time_array["day"];
256 $year = $date_time_array["year"];
257
258 //$temp_date = mktime(0,0,0,$month,$day,$year);
259 //$date_time_array = getdate( $temp_date );
260 $claimed_dayofweek = $date_time_array['weekday'];
261 $this->dayofweek = $date_time_array['wday'];
262
263 $this->date = $date;
264
265 //if($real_dayofweek != $claimed_dayofweek)
266 // $this->date = date("m/d/Y", mktime(0, 0, 0, $month, $day, $year));
267 //else
268 // $this->date = $date;
269 }
270}