Its been a while since I last blogged, but feeling suitably pleased with my recent efforts, so its time to blog :)
A while back I noticed that some PSK31 contacts have included automatic weather data, data from qrz.com, data from all over the web in fact. I wanted to add similar functionality to gMFSK
I noted that gMFSK (others will probably work too!) allows a shell command to be run as part of the macro system assigned to the function keys - extending the usual $mycall $yourcall syntax as far as you care to imagine. This is the way in :)
Basically to run any other command in gMFSK the syntax is $(/path/to/the/app)
So... after a bit of digging I found that the yahoo weather xml call brings back the data Im looking for (temp, pressure, wind and a description) and although it uses some extended xml namespace synatx is very easy to parse. I quickly wrote some test apps in php (yes I could have written them in C, perl or any other lang, but php makes this type of rss scraping easy and its what Im most fulent in).
(The reason for deciding upon yahoo by the way is that they change their XML far less frequently than other well known RSS feeds)
Basically the test scripts drag back the rss page from
http://weather.yahooapis.com/forecastrss?p=UKXX1012&u=c
which is my local weather station. To find the url for yours visit http://weather.yahoo.com/ type in your location into the city or zipcode box, and then click on the orange RSS feed button on the right and note the url of the rss feed. The important bit is the p=XXXXXXXXXX bit. You may want to change the final u=f to u=c if you want mainly metric results.
If you view the source of what comes back you will seethat most of the required info is there for the taking, if you can manipulate the
<yweather:wind chill="1" direction="260" speed="14.48" />
colon extended namespace format.
I wrote 4 test scripts for temp, pressure, wind, and conditions using php (If you dont have php-cli installed you need to add it - debian / ubuntu / similar systems can simply run
> sudo apt-get-install php5-cli
to add php script support without the need to add all the apache libs etc)
Each script calls the web page direct, but this is of course wasteful as the data is only updated every 30 mins or so. Most users will want to modify the scripts to point at a local version of the data and cron an hourly download, but thats up to you to work out ;)
temp::
#!/usr/bin/php
<?
$rss = simplexml_load_file('http://weather.yahooapis.com/forecastrss?p=UKXX1012&u=c');
$temps = $rss->xpath('//yweather:condition/@temp');
$units = $rss->xpath('//yweather:units/@temperature');
$temp = ltrim(rtrim($temps[0])) ."°". $units[0];
echo "T: " .$temp;
?>
Pressure::
#!/usr/bin/php
<?
$rss = simplexml_load_file('http://weather.yahooapis.com/forecastrss?p=UKXX1012&u=c');
$pressures = $rss->xpath('//yweather:atmosphere/@pressure');
$units = $rss->xpath('//yweather:units/@pressure');
$pressure = ltrim(rtrim($pressures[0])) . $units[0];
echo "P: ". $pressure;
?>
Wind::
#!/usr/bin/php
<?
$rss = simplexml_load_file('http://weather.yahooapis.com/forecastrss?p=UKXX1012&u=c');
$windd = $rss->xpath('//yweather:wind/@direction');
$winds = $rss->xpath('//yweather:wind/@speed');
$units = $rss->xpath('//yweather:units/@speed');
$wind = ltrim(rtrim($windd[0])) . "° ". ltrim(rtrim($winds[0])) . $units[0];
echo "W: " .$wind;
?>
condx::
#!/usr/bin/php
<?
$rss = simplexml_load_file('http://weather.yahooapis.com/forecastrss?p=UKXX1012&u=c');
$condx = $rss->xpath('//yweather:condition/@text');
$condx = ltrim(rtrim($condx[0])) ." ";
echo $condx;
?>
You need to mod each of the scripts to be executable via chmod +x and save to the gMFSK dir in your homedir.
Each script can be tested on the command line.
Finally you need to add them to gMFSK. In my case Ive added a combined wx macro to F12:
WX: $(/home/chrisw/gMFSK/condx) - $(/home/chrisw/gMFSK/temp) $(/home/chrisw/gMFSK/wind) $(/home/chrisw/gMFSK/pressure)
(change chrisw to whatever your homedir is!)
Hitting F12 now of course brings back to the TX window:
WX: Mostly Cloudy - T: 4°C W: 290° 8.05km/h P: 982.05mb
As commented, each script hits yahoo for about 1k of data so while this inst a huge amount, its not very sporting every QSO so consider using a wget script from the cron to grab a local offline copy of the page and parse that rather than hitting the web every time!
Hopefully that provides some inspiration for others to go on to explore other dynamic data includes in gMFSK from not only yahoo weather, but the whole of the net :)
Thanks for this! I will try in the morning when my head is more clear. 73 de dimitri
ReplyDeleteThis is great!!! I can combine all the code snippets into one and pull back all the WX results for my QRSS beacon. Thanks again Chris!
ReplyDelete