-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.yahoo.php
309 lines (259 loc) · 11.9 KB
/
class.yahoo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?PHP
// Class for obtaining Weather from php
// @url https://developer.yahoo.com/yql/console/
require_once('config.php');
require_once('cache.php');
class cYahooWeather
{
var $bCacheEnabled = false;
var $sCachePath = './cache/';
var $sCacheFile = 'cache_yahoo.xxx';
var $iCacheRetention = 10; // How many minutes cache is considered 'fresh'
// This value will be overwritten by Yahoo TTL parameter
var $aCacheStore = Array();
var $bDebugMode = true;
var $oCache = null;
function __construct($inCacheEnabled = false, $inCachePath = '', $inCacheRetention = 3600)
{
global $config;
$this->bCacheEnabled = $inCacheEnabled;
$this->sCachePath = $inCachePath;
$this->iCacheRetention = $inCacheRetention;
if ($this->bCacheEnabled)
$this->oCache = new cCache($config['database']);
}
// Function caches data in file on disk
// This will be very ineffective and my cause a lot of problems
// but for our home use (*) we can stick with that.
//
// (*) - Weather OS query server once every 15 minutes - there is no risk of access problems
function getYahooData($inYqlQuery)
{
// Try to load cache data from file
// TODO: Change this to fopen with locking (EX).
////if ($this->bCacheEnabled && count($this->aCacheStore) === 0 && file_exists($this->sCachePath.$this->sCacheFile))
//// $this->aCacheStore = unserialize(file_get_contents($this->sCachePath.$this->sCacheFile));
// If we have 'fresh' data then return value from cache
// We can check it anyway (even with cache disabled because then no data will be available at all).
////if (
//// isset($this->aCacheStore[$inYqlQuery]) &&
//// !empty($this->aCacheStore[$inYqlQuery]) &&
//// $this->aCacheStore[$inYqlQuery]['valid-till'] > time()
////)
////{
//// return $this->aCacheStore[$inYqlQuery]['data'];
////}
if ($this->bCacheEnabled)
{
$cachedValue = $this->oCache->getValue('cYahooWeather','JQL:'.$inYqlQuery,$this->iCacheRetention);
if (!empty($cachedValue))
return $cachedValue;
}
// Retrive data from Yahoo!
$tURL = 'http://query.yahooapis.com/v1/public/yql?q='.rawurlencode($inYqlQuery).'&format=json&diagnostics=false';
$tBuffer = @file_get_contents($tURL);
if (strlen($tBuffer) === 0)
return '';
$tReturn = json_decode($tBuffer);
if (isset($tReturn->query->results->channel->ttl) && is_numeric($tReturn->query->results->channel->ttl))
$tResultCacheRetention = $tReturn->query->results->channel->ttl;
else
$tResultCacheRetention = $this->iCacheRetention;
// If we retrived any data - store it on drive
if
(
$this->bCacheEnabled &&
!empty($tReturn) //&&
//// (
//// (
//// is_writable(dirname($this->sCachePath.$this->sCacheFile)) &&
//// !file_exists($this->sCachePath.$this->sCacheFile)
//// ) ||
//// (
//// file_exists($this->sCachePath.$this->sCacheFile) &&
//// is_writable($this->sCachePath.$this->sCacheFile)
//// )
//// )
)
{
//// $this->aCacheStore[$inYqlQuery]['data'] = $tReturn;
//// // Internal Cache Time is Persisted in seconds
//// $this->aCacheStore[$inYqlQuery]['valid-till'] = time() + (60 * $tResultCacheRetention);
//TODO: Check if folder is writeable
//// @file_put_contents($this->sCachePath.$this->sCacheFile,serialize($this->aCacheStore));
$this->oCache->setValue('cYahooWeather','JQL:'.$inYqlQuery,$tReturn);
}
return $tReturn;
}
function getRegions()
{
//
// Check if we have any usable data in cache.
//
if ($this->bCacheEnabled)
{
$cachedValue = $this->oCache->getValue('cYahooWeather','getRegions',$this->iCacheRetention);
if (!empty($cachedValue))
return unserialize($cachedValue);
}
$yqlQuery = 'select woeid,name from geo.continents';
$tResult = Array();
$tYahooData = $this->getYahooData($yqlQuery);
if (!empty($tYahooData))
foreach($tYahooData->query->results->place AS $tPlaceEntry)
$tResult[$tPlaceEntry->woeid] = $tPlaceEntry->name;
//
// Store value in cache.
//
if ($this->bCacheEnabled && !empty($tReturn))
{
$this->oCache->getValue('cYahooWeather','getRegions',serialize($this->iCacheRetention));
}
return $tResult;
}
//TODO: Add Search by WOEID
function getCountryByRegion($inRegionID)
{
//
// Check if we have any usable data in cache.
//
if ($this->bCacheEnabled)
{
$cachedValue = $this->oCache->getValue('cYahooWeather','getCountryByRegion:'.$inRegionID,$this->iCacheRetention);
if (!empty($cachedValue))
return unserialize($cachedValue);
}
$tRequestName = 'EUROPE';
// YQL Accept only searching country name by 'place' string
// So translate WOEID to NAME
$tRegionData = $this->getRegions();
foreach ($tRegionData AS $tRegionID => $tRegionName)
if ($inRegionID === $tRegionID)
$tRequestName = $tRegionName;
$yqlQuery = 'select woeid,name from geo.countries where place="'.$tRequestName.'"';
$tYahooData = $this->getYahooData($yqlQuery);
$tReturn = Array();
if (!empty($tYahooData))
foreach($tYahooData->query->results->place AS $tPlaceEntry)
$tReturn[$tPlaceEntry->woeid] = $tPlaceEntry->name;
//
// Store value in cache.
//
if ($this->bCacheEnabled && !empty($tReturn))
{
$this->oCache->getValue('cYahooWeather','getCountryByRegion:'.$inRegionID,serialize($this->iCacheRetention));
}
return $tReturn;
}
function getStationsByCountry($inCountryID)
{
//
// Check if we have any usable data in cache.
//
if ($this->bCacheEnabled)
{
$cachedValue = $this->oCache->getValue('cYahooWeather','getStationsByCountry:'.$inCountryID,$this->iCacheRetention);
if (!empty($cachedValue))
return unserialize($cachedValue);
}
$yqlQuery = 'select woeid, placeTypeName, name from geo.places.descendants where ancestor_woeid in (select woeid from geo.states where place='.$inCountryID.') and placetype = 7';
//$yqlQuery = 'select woeid, name from geo.places.children where parent_woeid = '.$inCountryID;
$tYahooData = $this->getYahooData($yqlQuery);
$tReturn = Array();
if (!empty($tYahooData))
foreach($tYahooData->query->results->place AS $tStationEntry)
$tReturn[$tStationEntry->woeid] = $tStationEntry->name;
//
// Store value in cache.
//
if ($this->bCacheEnabled && !empty($tReturn))
{
$this->oCache->getValue('cYahooWeather','getStationsByCountry:'.$inCountryID,serialize($this->iCacheRetention));
}
return $tReturn;
}
function getForecastByStation($inStationID)
{
//
// Check if we have any usable data in cache.
//
if ($this->bCacheEnabled)
{
$cachedValue = $this->oCache->getValue('cYahooWeather','getForecastByStation:'.$inStationID,$this->iCacheRetention);
if (!empty($cachedValue))
return unserialize($cachedValue);
}
$yqlQuery = 'select atmosphere,item from weather.forecast where woeid='.$inStationID.' and u=\'c\'';
$tYahooData = $this->getYahooData($yqlQuery);
if
(
!isset($tYahooData) ||
empty($tYahooData) ||
!isset($tYahooData->query) ||
!isset($tYahooData->query->results->channel) ||
!isset($tYahooData->query->results->channel->item->lat)
) return Array();
$tReturn = Array();
$tReturn['position']['latitude'] = $tYahooData->query->results->channel->item->lat;
$tReturn['position']['longitude'] = $tYahooData->query->results->channel->item->long;
$tReturn['weather']['humidity'] = $tYahooData->query->results->channel->atmosphere->humidity;
$tReturn['weather']['pressure'] = $tYahooData->query->results->channel->atmosphere->pressure;
$tReturn['weather']['rising'] = $tYahooData->query->results->channel->atmosphere->rising;
$tReturn['weather']['visibility'] = $tYahooData->query->results->channel->atmosphere->visibility;
$tReturn['weather']['temperature'] = $tYahooData->query->results->channel->item->condition->temp;
$tReturn['weather']['text'] =$tYahooData->query->results->channel->item->condition->text;
//TODO: Add code
foreach($tYahooData->query->results->channel->item->forecast AS $inForecastKey => $inForecastEntry)
{
$tReturn['forecast'][$inForecastKey]['date'] = $inForecastEntry->date; //TODO: Change to format YYYYMMDD
$tReturn['forecast'][$inForecastKey]['temperature']['minimal'] = $inForecastEntry->low;
$tReturn['forecast'][$inForecastKey]['temperature']['maximal'] = $inForecastEntry->high;
// TODO TRANSLATE CODES TO REGON ! $inForecastEntry->code;
$tReturn['forecast'][$inForecastKey]['temperature']['text'] = $inForecastEntry->text;
}
$tReturn['general']['time'] = $this->getTimeByID($inStationID);
//
// Store value in cache.
//
if ($this->bCacheEnabled && !empty($tReturn))
{
$this->oCache->getValue('cYahooWeather','getForecastByStation:'.$inStationID,serialize($this->iCacheRetention));
}
return $tReturn;
}
function getTimeByID($inStationID)
{
//
// Check if we have any usable data in cache.
//
if ($this->bCacheEnabled)
{
$cachedValue = $this->oCache->getValue('cYahooWeather','getTimeByID:'.$inStationID,$this->iCacheRetention);
if (!empty($cachedValue))
return unserialize($cachedValue);
}
$yqlQuery = 'select timezone from geo.places where woeid = '.$inStationID;
$tYahooData = $this->getYahooData($yqlQuery);
$tReturn = Array('value'=>time(),'utcoffset' => 0);
if (isset($tYahooData->query->count) && $tYahooData->query->count > 0)
{
$tLocalTimeZone = $tYahooData->query->results->place->timezone->content;
$tDateTime = new DateTime(); // Get current server time
$tTimeZone = new DateTimeZone($tLocalTimeZone);
$tDateTime->setTimezone($tTimeZone);
//$tLocalTime = $tDateTime->format('Y-m-d H:i:s').' '.$tTimeZone;
//$tUTCTimeZone = new DateTimeZone("UTC");
$tReturn['value'] = $tDateTime->format('Y-m-d H:i:s').' '.$tLocalTimeZone;
$tReturn['utcoffset'] = $tTimeZone->getOffset($tDateTime);// - $tUTCTimeZone->getOffset();
}
//
// Store value in cache.
//
if ($this->bCacheEnabled && !empty($tReturn))
{
$this->oCache->getValue('cYahooWeather','getTimeByID:'.$inStationID,serialize($this->iCacheRetention));
}
return $tReturn;
}
};
?>