fixed with Fabio's assistance

pull/96/head
tony baldwin 2013-02-11 05:24:21 -05:00
parent 0ccb9f5c0c
commit 3ce891c7d3
4 changed files with 200 additions and 184 deletions

Binary file not shown.

View File

@ -4,8 +4,10 @@
* Description: Shows current temperature for user's location on their network page
* Version: 1.0
* Author: Tony Baldwin <t0ny@friendica.tonybaldwin.info>
* Author: Fabio Comuni <fabrixxm@kirkgroup.com>
*
*/
require_once('addon/curtemp/getweather.php');
function curtemp_install() {
register_hook('network_mod_init', 'addon/curtemp/curtemp.php', 'curtemp_network_mod_init');
@ -22,30 +24,31 @@ function curtemp_uninstall() {
}
function curtemp_network_mod_init($a,$b) {
function curtemp_network_mod_init(&$fk_app,&$b) {
if(! intval(get_pconfig(local_user(),'curtemp','curtemp_enable')))
return;
$a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/curtemp/curtemp.css' . '" media="all" />' . "\r\n";
$fk_app->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $fk_app->get_baseurl() . '/addon/curtemp/curtemp.css' . '" media="all" />' . "\r\n";
// the getweather file does all the work here
// the $rpt value is needed for location
// which getweather uses to fetch the weather data for weather and temp
$curtemp_loc = get_pconfig(local_user(), 'curtemp', 'curtemp_loc');
$rpt = get_pconfig(local_user(), 'curtemp', 'curtemp_loc');
$wxdata = GetWeather::get($rpt);
$temp = $wxdata['TEMPERATURE_STRING'];
$weather = $wxdata['WEATHER'];
$curtemp = '<div id="curtemp-network" class="widget">
<div class="title tool">
<h4>'.t("Current Temp").'</h4></div>';
$curtemp .= '<?php
require_once(\'addon/curtemp/getweather.php\');
$rpt = "' . $curtemp_loc . '";
?>
Weather: <php echo $wxdata[\'WEATHER\'];?><br />
Temperature: <php echo $wxdata[\'TEMPERATURE_STRING\'];?>';
$curtemp .= '</div></div><div class="clear"></div>';
$curtemp .= 'Weather: "' . $weather . '"<br />
Temperature: "' . $temp . '"';
$curtemp .= '</div><div class="clear"></div>';
$fk_app->page['aside'] = $curtemp.$fk_app->page['aside'];
$a->page['aside'] = $curtemp . $a->page['aside'];
}

View File

@ -28,6 +28,15 @@ Complete list of Weather stations available at
http://weather.gov/data/current_obs/index.xml
*/
class GetWeather {
// Initialize some variables
static $itemdata;
static $itemname;
static $wxdata;
function get($rpt) {
// URL for the XML file
$xmlurl="http://www.weather.gov/data/current_obs/$rpt.xml";
@ -35,16 +44,15 @@ $xmlurl="http://www.weather.gov/data/current_obs/$rpt.xml";
// Base url for the icons
$imgpath="http://weather.gov/weather/images/fcicons";
// Get the icons as an array
$icons=defineIcons();
// Initialize some variables
$itemdata="";
$itemname="";
$wxdata=array();
self::$itemdata="";
self::$itemname="";
self::$wxdata=array();
$icons=self::defineIcons();
$icon="";
$data="";
$report="";
$icon="";
// create a new CURL resource
if($ch = curl_init()) {
@ -72,13 +80,13 @@ if($ch = curl_init()) {
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
// Assign the element starting and ending event handlers
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_element_handler($xml_parser, array(self,"startElement"), array(self,"endElement"));
// Assign a function to handle character data
xml_set_character_data_handler($xml_parser, "characterData");
xml_set_character_data_handler($xml_parser, array(self,"characterData"));
// Parse the file. This will place the data into an associative
// array assigned to the $wxdata variable
// array assigned to the self::$wxdata variable
xml_parse($xml_parser,$data,true);
// Free the parser object
@ -90,7 +98,7 @@ if($ch = curl_init()) {
// Strip out the "Last Updated on " portion of the date/time
// so we can display that separately in our tabular output
$datetime=str_replace("Last Updated on ","",$wxdata['OBSERVATION_TIME']);
$datetime=str_replace("Last Updated on ","",self::$wxdata['OBSERVATION_TIME']);
// We now have the format as "May 18, 8:53 am CDT"
// Now, get the time zone. It will be everything from
@ -117,77 +125,76 @@ if($ch = curl_init()) {
// Format the date and time the way we want it and add
// back the time zone
$datetime=date("l F j, Y g:i A",strtotime($datetime)).$z;
$wxdata['OBSERVATION_TIME']=$datetime;
self::$wxdata['OBSERVATION_TIME']=$datetime;
// Get the WEATHER element
$wx=trim($wxdata['WEATHER']);
$wx=trim(self::$wxdata['WEATHER']);
// Now, get the icon to match the weather
foreach($icons as $k=>$i){
$a=explode(" | ",$i);
if(is_numeric(array_search($wx,$a))){
$wxdata['ICON']="$imgpath/$k.jpg";
self::$wxdata['ICON']="$imgpath/$k.jpg";
break;
}
}
// Replace any null elements with "Not available"
foreach(array_keys($wxdata) as $key){
$wxdata[$key]=$wxdata[$key]=="NULL"?"Not available":$wxdata[$key];
foreach(array_keys(self::$wxdata) as $key){
self::$wxdata[$key]=self::$wxdata[$key]=="NULL"?"Not available":self::$wxdata[$key];
}
// If we got humidity
if(is_numeric($wxdata['RELATIVE_HUMIDITY']))
if(is_numeric(self::$wxdata['RELATIVE_HUMIDITY']))
// Append a percent sign
$wxdata['RELATIVE_HUMIDITY'].="%";
self::$wxdata['RELATIVE_HUMIDITY'].="%";
// Do some formatting to make the output a little friendlier
if($wxdata['VISIBILITY_MI']=="NA")
$wxdata['VISIBILITY']="Not available";
if($wxdata['VISIBILITY']!="Not available")
$wxdata['VISIBILITY']=(1*$wxdata['VISIBILITY_MI'])." miles";
if(self::$wxdata['VISIBILITY_MI']=="NA")
self::$wxdata['VISIBILITY']="Not available";
if(self::$wxdata['VISIBILITY']!="Not available")
self::$wxdata['VISIBILITY']=(1*self::$wxdata['VISIBILITY_MI'])." miles";
// If we got wind data
if(is_numeric($wxdata['WIND_MPH'])){
if(is_numeric(self::$wxdata['WIND_MPH'])){
// We're going to output wind data as both MPH from a cardinal direction
// and as Knots from a direction in degrees
// Calculate the value for Knots
$wxdata['WIND_KNOTS']=$wxdata['WIND_MPH']/1.15;
self::$wxdata['WIND_KNOTS']=self::$wxdata['WIND_MPH']/1.15;
// Format the output
$wind=sprintf("From the %s at %d mph (%03.0f&deg; at %d knots)",$wxdata['WIND_DIR'],$wxdata['WIND_MPH'],$wxdata['WIND_DEGREES'],$wxdata['WIND_KNOTS']);
$wind=sprintf("From the %s at %d mph (%03.0f&deg; at %d knots)",self::$wxdata['WIND_DIR'],self::$wxdata['WIND_MPH'],self::$wxdata['WIND_DEGREES'],self::$wxdata['WIND_KNOTS']);
// If we got a value for wind gusts
if(is_numeric($wxdata['WIND_GUST_MPH']) && $wxdata['WIND_GUST_MPH']>0){
if(is_numeric(self::$wxdata['WIND_GUST_MPH']) && self::$wxdata['WIND_GUST_MPH']>0){
// add it into the wind string
$wind=str_replace("mph","gusting to ".$wxdata['WIND_GUST_MPH']." mph<br>", $wind);
$knots=sprintf("%d",$wxdata['WIND_GUST_MPH']/1.15);
$wind=str_replace("mph","gusting to ".self::$wxdata['WIND_GUST_MPH']." mph<br>", $wind);
$knots=sprintf("%d",self::$wxdata['WIND_GUST_MPH']/1.15);
$wind=str_replace("knots","gusting to $knots knots",$wind);
}
} else {
// Otherwise, if wind is zero, we'll show "Calm"
$wind=$wxdata['WIND_MPH']=="Not available"?"Not available":"Calm";
$wind=self::$wxdata['WIND_MPH']=="Not available"?"Not available":"Calm";
} // Done with wind
$wxdata['WIND_STRING']=$wind;
self::$wxdata['WIND_STRING']=$wind;
} // Done getting and formatting the data
return self::$wxdata;
}
function startElement($parser, $name, $attrs) {
global $itemname,$itemdata,$wxdata;
$itemname=$name;
$itemdata="";
self::$itemname=$name;
self::$itemdata="";
}
function endElement($parser, $name) {
global $itemname,$itemdata,$wxdata;
$wxdata[$itemname]=($itemdata);
$itemdata="";
self::$wxdata[self::$itemname]=self::$itemdata;
self::$itemdata="";
}
function characterData($parser, $data) {
global $itemname,$itemdata,$wxdata;
$itemdata.=$data;
self::$itemdata.=$data;
}
function defineIcons(){
@ -218,5 +225,6 @@ function defineIcons(){
$retVal['mist']="Haze";
return $retVal;
}
// end CLASS
}
?>

5
curtemp/test.php Normal file
View File

@ -0,0 +1,5 @@
<?php
require_once 'getweather.php';
$rpt = "KHVN";
$wxdata = GetWeather::get($rpt);
var_dump($rpt, $wxdata);