Thursday, August 2, 2007

Magik HTTP requests; Open Source Magik; Reading RSS

Brad packaged a new release (in .zip file) of the Magik Components source tree here. Be sure to choose the latest version of "mclib CST 4.0 SWAF". This will include the dialog designer that I mentioned yesterday.

I wanted to highlight one of the cool components that I have found in this package. The module is called :mclib_http_interface. It provides a Magik interface to making HTTP requests to a server and then return the results back to you as a simple_xml_document. You can then use the API on simple_xml_document to parse information out.

To highlight the ease and power of this capability, I quickly hacked together some code that allows you to connect to the Yahoo!Weather RSS feed and get a weather forecast for your location. This is a simple example and you can clearly extend it to your heart's content.

Download the Magik Components product and register the accompanying product.def file in your curent image.

Load the :mclib_http_interface module (eg., sw_module_manager.load_module(:mclib_http_interface)

Now run the following block of code. As written, it will give you a weather forecast for Boulder, CO, USA in degrees Celsius. The example also shows how you can glean lat/long information from a feed and convert it to a Magik coordinate.

_block
# see http://developer.yahoo.com/weather/ for more information
# about the Weather RSS feed
_constant BASE_URL << "http://weather.yahooapis.com/forecastrss"
_constant QUERY_CHAR << %?
_constant QUERY << "p=80305&u=c" # forecast for USA ZipCode 80305 in Celsius
_constant URL << write_string(BASE_URL,QUERY_CHAR,QUERY)

_local hr << mclib:http_request.new_for_url(_unset,_unset,URL)
_local response << hr.connect()
_local a_xml_doc << response.smart_result()

_local rss_channel << a_doc.top_element.element_matching_name(:channel)

_local item << rss_channel.element_matching_name(:item)

_local long << item.element_matching_name("geo:long").elements[1].as_number()
_local lat << item.element_matching_name("geo:lat").elements[1].as_number()

write()
write(item.element_matching_name(:title).elements[1]," at ",coordinate.new(long,lat))


_local forecast_attributes

_for a_forecast _over item.elements_matching_name("yweather:forecast")
_loop
forecast_attributes << a_forecast.attributes

write(forecast_attributes[:day]," (",forecast_attributes[:date],") ",forecast_attributes[:text]," low=",forecast_attributes[:low]," high=",forecast_attributes[:high]," ")
_endloop
_endblock
$


Incidentally, you can get more information about the Yahoo!Weather API at http://developer.yahoo.com/weather

Where I think this http_interface could come in really handy is for you to write GeoRSS parsers that would allow you to show location events on a Smallworld map (maybe as a SOM or a post_render_set). Have a look at Direction Magazine's Fun with GeoRSS article if you are interested in other ideas for interfacing with other data sources in real time from Smallworld.

3 comments:

Brad said...

Great stuff Alfred - thanks for pointing this one out. It could use a bit of work to better support HTTP authentication and do a better job handling more complex results, but it does provide a simple framework to start from. BTW, there is also an examples directory with the module that shows getting geocoding results from Google Maps and Yahoo maps as additional ways to use web services.

There is also an HTTP server module provided...anyone who saw my presentation on Smallworld integrated with Google Earth at last years users conference has some idea of what can be done with that. How about an RSS Feed out of Smallworld allowing other systems to get a stream of changes for some type of object?

Alfred Sawatzky said...

The first one that comes to mind is a GeoRSS feed for PowerOn that shows outages, crews, etc. With GeoRSS feed you can then overlay it on tools like Google Maps, Google Earth (and other apps, no doubt).

Tim Minto said...

Hey Alfred & Brad,

I recently had a need to write something similar for a client of mine (to do SOAP-compliant web services-based interface to Maximo, both from the client side and the server side). I'll be posting this with the next release of MSS tools.

I'll have to take a closer look at what you've got in MCLIB and see if I can use any of it to improve what we've implemented so far.