OK, I realise that not everyone who uses gMFSK is a php programmer, and somne people are a little wary of editing the cron tab, so Ive written out a script that does everything - ie: it checks if there is a recent local copy of the RSS xml and builds the weather data from that - only downloading from yahoo if more than 30 minutes (by default) have passed.
Ive added lots of comments so as a combination of the new script and reading the previous blog entry it should be very clear how this works. I hope it will lead you on to a bit more experimenting with other on-line RSS sources you may want to use.
enjoy:
#!/usr/bin/php
<?
/* gMFSK getwx::
A simple script to grab your local weather from the web to insert into F-key
macros in the same way as $mycall $yourcall etc.
This is a slightly more finished version of the test scripts recently published
on my blog. This version checks if it has a local copy with a recent datestamp
of the RSS data from yahoo - then builds a complete weather report from the cached
data to save hitting yahoo every time the script is called.
Yes it could be done more easilly using cron, but feedback from some users
has suggested that a self contained "easy" script is wanted
Yes I could have used CURL - but a basic php_cli install doesnt always have
CURL compiled in.
to get the right url for your local yahoo weather vist weather.yahoo.com
type in your location and hit return. When you get your local weather click
on the orange RSS button and make a note of the URL. its the p=XXXXXXXXX bit that
is important. Changing the u=f to u=c gets you metric results
You need php_cli installed for this script to work eg: sudo apt-get install php5_cli
save this script to your gMFSK directory, remember to chmod it +x to be executeable
in one of the gMFSK function key windows add the command:
$(/home/g7nbp/gMFSK/getwx)
Remember to alter your path to point at wherever the file is saved.
Please feel free to experiment :)
Written by g7nbp 28-12-2009
*/
// OK first a few variables
$pageurl = "http://weather.yahooapis.com/forecastrss?p=UKXX1012&u=c"; // url of yahoo weather RSS feed
$file = "wx.xml"; // the file to save the xml to
$update_time = 1800; // period between getting updates down in seconds
if (file_exists($file)){
// the wx.xml file has been found :)
$fileage = time() - filemtime($file); // workout how old the local copy is
if ($fileage >= $update_time){
// local copy older than update_time - call the get_xml routine to read from website
get_xml();
}
}else{
// no the wx.xml file has not been found (first run??) so call the get_xml routine
get_xml();
}
// OK assuming nothing broke we can now work with the local copy :)
$rss = simplexml_load_file($file); // open local copy
// Build the variables from the assorted yweather:something namespaces
$condx = $rss->xpath('//yweather:condition/@text');
$condx = ltrim(rtrim($condx[0])) ." ";
$pressure = $rss->xpath('//yweather:atmosphere/@pressure');
$pressure_units = $rss->xpath('//yweather:units/@pressure');
$pressure = ltrim(rtrim($pressure[0])) . $pressure_units[0];
$temp = $rss->xpath('//yweather:condition/@temp');
$temp_units = $rss->xpath('//yweather:units/@temperature');
$temp = ltrim(rtrim($temp[0])) ."°". $temp_units[0];
$windd = $rss->xpath('//yweather:wind/@direction');
$winds = $rss->xpath('//yweather:wind/@speed');
$wind_units = $rss->xpath('//yweather:units/@speed');
$wind = ltrim(rtrim($windd[0])) . "° ". ltrim(rtrim($winds[0])) . $wind_units[0];
// finally build our output string
echo "local WX: Temp=". $temp . " Pressure=" . $pressure . " Wind=" . $wind . " Condx=" . $condx . "\n" ;
// --- thats all folks! ---
// functions live here
function get_xml(){
// a really simple file_get file_put routine to grab a copy of the weather XML
// and save it local to save hitting yahoo every time time we run the srcipt
global $file, $pageurl;
$xml =file_get_contents($pageurl);
file_put_contents($file, $xml);
}
?>
No comments:
Post a Comment