Anyway, playing around with the Bing API the other day got me in the mood to play around with web-based data once again, which happens on rare occasions. I find data, then I want to find out if I can import that data and make it useful to me, personally, and maybe a few of my friends. That was part of my motivation behind messing with the Bing API (not to mention porn) but now I've moved on to other areas.
Way back earlier this year I started working on a project to grab weather data from the National Weather Service and was planning on creating a way to send short-term forecasts to Twitter. I already started a way to send news headlines to Twitter, using the free Daylife API, and I've long thought that getting the local weather via Twitter would be cool, too. I may still do that, but now I have other plans.
When I first started looking at the weather data, figuring out how to use it was way beyond my capabilities. I only knew how to deal with PHP-REST data, and had no idea how to deal with plain old-fashioned XML data in PHP. Once, a long time ago, I was able to write a script that imported and parsed RSS feeds, but that was a few years back and had long since been forgotten. But messing with the Bing API taught me a few more tricks so I started playing around a little more.
Before the weather idea came up, however, I had a more immediate need. I'm on your basic residential DSL connection at home, and it serves my purposes quite well until FiOS is available in my area. But being on a residential DSL circuit means I have a dynamic IP Address, which makes it difficult to remotely connect to my home machines if the lease expires and my IP changes. So the question was, can I monitor my home IP address and send a text message to my cell phone when it changes?
The answer was a surprisingly easy yes. Sort of. Writing the script to send the message was extremely easy, but the difficult part was configuring sendmail and apache to send an email through Verizon's smtp servers. That's a long story and not really pertinent to this discussion, but let's say that spammers have made life much more difficult for us honest folks.
Anyway, if you have a Verizon cell phone, here's the little script I use to send a text to my phone when my IP changes. Note, I don't have telnet or SNMP access to this cheap-ass router they provide, so I have to grab the IP from an external website. I store it in a database so that I can check it frequently but only send a text message when there's a change:
@mysql_pconnect("localhost", "username", "password")
or die(mysql_error());
@mysql_select_db("ip_monitor")
or die(mysql_error());
function mailIP($ip) {
$headers = "your email address \r\n";
$headers .= "Cc: another email address \r\n";
$headers .= "Content-Type: text/plain;\r\n charset=iso-8859-1\r\n";
mail('your verizon phone number@vtext.com', '', $ip, $headers);
}
$ip = file_get_contents('http://www.biranchi.com/ip.php');
$time = time();
$lastRow = "SELECT * FROM addresses ORDER BY id DESC LIMIT 1";
$numRows = mysql_query($lastRow);
while ($row = mysql_fetch_assoc($numRows)) {
$id = $row['id'];
$epoch = $row['epoch'];
$oldIP = $row['ip'];
if ($ip == $oldIP) {
break;
}
else {
$insert = "INSERT INTO addresses SET epoch='$time', ip='$ip'";
$insertAction = mysql_query($insert);
if (!$insertAction) {
//echo "Old IP ".$oldIP." matches Current IP ".$ip; //just a visual reference
die(mysql_error());
}
else {
//echo "time= ".$time." IP= ".$ip; //just a visual reference
mailIP($ip);
}
}
}That's it. Easy as pie and now I get a text message whenever my IP address changes.
Once that was done, I had another idea. Some strong winter weather was moving through the area (the whole country almost) and I noticed on the National Weather Service website there was an alert issued for my area. So the little lightbulb goes off and I think it would be really, really nice to have those sent by text to my phone as well.
That, of course, was a little more difficult, but I managed it. I won't post all of the code here because I'm not done making changes to it, but basically it's very similar to the code above except instead of just grabbing an IP from a web page, I now have to parse a potentially lengthy XML response.
Parsing the XML was a little bit of a learning experience. In all honesty, it was pretty easy once I figured out what I was doing, but I did run into a problem with namespaces. Parts of the XML document I use for severe weather alerts is based on the Common Alerting Protocal, but the document didn't provide a reference to the namespace schema, so I had to learn how to deal with that. It would have been ok, but the prefixes used in the document weren't completely consistent with the CAP schema, so I had to come up with a workaround.
Well, I didn't have to come up with a workaround. I didn't need the CAP messages for what I was trying to do with the data since I'm only sending the timestamp and the first 120 characters of the alert summary to my cell phone, but I definitely wanted to be able access all of it regardless of its current usefulness. In the process of figuring out how to use the CAP data in the weather alert messages, I found that there are all kinds of agencies using that same protocol. That resulted in the realization that I could grab tons and tons of XML-based data relating to emergency situations and use it in dozens and dozens of different ways. Send it to Twitter, send it to cell phones, display it on the web in maps or develop mobile applications.
I even saw where the Department of Homeland Security uses RSS to disseminate alert information, including the famous color-coded terrorist threat level, although I could easily fake that and just send out a message every day saying the threat level is orange. California's emergency management agency uses CAP, as does the USGS and some Canadian provincial governments. The amount of XML emergency data sitting out there on the web, relatively unused, is tremendous, and I'd like to find a good, preferably profitable, use for it.
Will I? Probably not. I'm not sure I have the inclination or the energy to set about another project that huge, at least not by myself. I don't really have the skill to do it properly, either. For now, I'm just satisfied playing with the data for my own personal interest as a way to teach myself new things. I'll figure out how to parse some of the more complex weather XML documents and grab useful information from them over the next few days, and if I'm not entirely bored with it by the time I'm done, maybe I'll sit down and figure out what it will take to start a small company that mines emergency data from the web. It could be fun, it could also be a headache, so we'll see how it goes.
0 comments:
Post a Comment