/
WeatherChan.class.php
106 lines (92 loc) · 1.92 KB
/
WeatherChan.class.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
<?php
/**
* This class featches the current weather and forecast for a zip code from the weather channel's rss
*@author bibby <bibby@surfmerchants.com>
*$Id$
*
*@requires xml_parser.php
*http://www.phpclasses.org/browse/package/4.html
* by Manuel Lemos
* http://www.manuellemos.net/
*/
require_once(BZ_CLS_PATH.'xml_parser.php');
class WeatherChan extends ziggi
{
function WeatherChan()
{
$this->cache='/tmp/botzilla_xml';
}
/*** parseBuffer ***
@access
@param
@return
*/
function parseBuffer()
{
if($this->getArg(0) == CMD_CHAR.'weather')
$this->weather($this->getArg(1));
if($this->getArg(0) == CMD_CHAR.'forecast')
$this->forecast($this->getArg(1));
}
/*** getWeather ***
@access
@param
@return
*/
function getWeather($zip)
{
if(!$zip) $zip = '02111';
$file=file_get_contents('http://rss.weather.com/weather/rss/local/'.$zip.'?cm_ven=LWO&cm_cat=rss&par=LWO_rss');
$fs = new FileStorage($this->cache,FS_WRITE);
$fs->write($file);
$error=XMLParseFile($parser,$this->cache,0);
if($error)
return $error;
else
{
$ret=array();
foreach($parser->structure as $k => $v)
{
if(is_string($v) && strstr($v, 'For more')!==FALSE)
{
$v=str_replace('°','°',$v);
if(strstr($v,'---')!==FALSE)
{
$days = explode('---',$v);
$junk = array_pop($days);
$ret['forecast']=$days;
}
else
{
$v=strip_tags($v);
$ret['current'] = substr($v,0, strpos($v,'For more')-1);
}
}
if(count($ret)==2)
break;
}
}
return $ret;
}
/*** weather ***
@access
@param
@return
*/
function weather($zip)
{
$w = $this->getWeather($zip);
$this->pm( !is_array($w) ? "sorry, couldn't get zip $zip" : $w['current'] );
}
/*** forecast ***
@access
@param
@return
*/
function forecast($zip)
{
$w = $this->getWeather($zip);
$this->pm( !is_array($w) ? "sorry, couldn't get zip $zip" : $w['forecast'] );
}
}
?>