WunderBot

To continue my recent work with the Wunderground api, I have now hacked together a new StatusNet bot plugin. With this plugin, you can ask the bot for the current weather conditions with just the word 'weather' and the five digit US zip code of your choice. So long as Wundergound recognizes the zip code, it should return some basic details. This plugin is currently active over on my Matrix instance, just ping @Niobe.

@jpope It is currently Overcast and feels like 64.0 F (17.8 C) in Jefferson City. The humidity is 58% and the wind is From the NNE at 2.9 MPH Gusting to 5.8 MPH. #cloudy cloudy http://www.wunderground.com/US/MO/Jefferson_City.html

Niobe (niobe)'s status on Monday, 17-Sep-12 22:18:02 CDT - matrix.jpope.org

I have looked to do this before but, hadn't had much success. Mostly, my lack of proper coding skills has usually been my holdup. Previously, I found a bot on identi.ca (source) that already did what I've done here, except the execution is quite different. That bot (which currently doesn't work properly due to it repeating notices) requires a php script to be running in the background, using CuRL to listen for notices to it and using CuRL to pull and post the requested weather details. The php script is run from a separate server than the StatusNet instance. My implementation is that the bot is an actual StatusNet plugin. It's installed and configured just as any other SN plugin.

When I started hacking this plugin together, I simply copied Evan's 8ball plugin script and went to work. First I had to figure out how to pull a zip code out of a notice sent to the bot. And by using regex (using pregmatch and pregmatch_all) I was able to capture the zip code. There is also another regex in there to check if the term 'weather' is in the notice. Now that I was able to capture the zip code, it was actually quite easy to pull the data via the API, just needed to plug in the pieces.

http://api.wunderground.com/api/$apikey/conditions/q/$zipcode.json

Also, to make things super easy, Wunderground has code samples as well. I just grabbed the PHP code sample, made a few changes so I'd get the details that I wanted and was done. Ok, so it wasn't just like that but, after playing with the script, I'm now mostly happy with it.

To set this up on your instance, you'll need to clone the plugin to {statusnetbase}/plugins/WundergroundBot/ with:

git clone git://gitorious.org/wundergoundbotplugin/wundergoundbotplugin.git WundergroundBot

Then, add this to your config.php:

addPlugin('WundergroundBot');
$config['wundergroundbot']['nickname'] = 'botname';
$config['wundergroundbot']['apikey'] = 'wundergroundapikey';

You'll need to edit in the username of the bot and your Wunderground API key. Run php scripts/checkschema.php just to make sure things take and restart the daemons. After that, you should be able to send your bot a notice that contains the term 'weather' and the five digit zip code and get a reply with the details.

@pla1 It is currently Clear and feels like 73.6 F (23.1 C) in Moncks Corner. The humidity is 44% and the wind is Calm. #clear clear

Niobe (niobe)'s status on Monday, 17-Sep-12 20:18:07 CDT - matrix.jpope.org

In the future, I'd like to get this to work with more than just US zip codes. The Wunderground API has quite a few options on how to figure out a location, I just need to figure out how to utilize it properly. It'll probably be easier than I currently imagine. ;)

Now I'm off to go check the weather.

@mike It is currently Light Thunderstorms and Rain and feels like 89 F (27.5 C) in Clearwater, FL. The humidity is 86% and the wind is Calm. #tstorms tstorms http://www.wunderground.com/US/FL/Clearwater.html

Niobe (niobe)'s status on Tuesday, 18-Sep-12 07:06:01 CDT - matrix.jpope.org

EDIT 2012.09.18: The plugin now supports Canadian postal codes as well. The postal codes need not have any spaces such as this code for Toronto: m4b2c1.

@jpope It is currently Clear and feels like 53.4 F (11.9 C) in Toronto, Ontario. The humidity is 81% and the wind is From the NNW at 1.6 MPH Gusting to 6.0 MPH. #clear clear http://www.wunderground.com/global/stations/71508.html

Niobe (niobe)'s status on Tuesday, 18-Sep-12 20:05:26 CDT - matrix.jpope.org


Comments:

Chimo

Awesome!
Feature request: Canadian Postal Codes, eh!
Format: [A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9]
Ex (through their web interface): http://www.wunderground.com/cgi-bin/findweather/hdfForecast?query=k1s4k1
I’d do it, but I’m not sure I want to exchange my phone number for an API key :

2012-09-18 10:28:33

jpope

Beauty, eh? That shouldn’t be too difficult to work in.

I usually use my unused google voice number for any phone number entering that I may come across. I generally don’t like handing a phone number out online but, at least with google voice, I’ve got a few more options on blocking various numbers.

Thanks buddy!

2012-09-18 10:59:13

Chimo

That’s a great solution to the phone number thing.
* looks it up *
“Note: At this time Google Voice is only available in the U.S.”
sigh.

2012-09-18 17:51:47

jpope

Son of a b…

It’s still only available in the US? Arrgh!

2012-09-18 18:12:57

jpope

w00t! Script now supports Canadian postal codes.

2012-09-18 17:06:05


Have a response to this post? Please use this link.