So nobody does these things, right?
I thought about your quiz question over coffee. I've never dealt with XML in lisp before, or web interfaces so I thought it might be fun to see how much is involved. Luckily people have thought about this sort of problem and come up with some neat programs.
So, here is some terrible, terrible code using drakma and cxml to do the simple task you asked:
Code: Select all
(defun geo-lookup (query)
(drakma:http-request
(format nil "http://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query=~A"
(if (numberp query) query (substitute #\+ #\space query))))) ; hack around spaces in query
(defun current-conditions (query)
(drakma:http-request
(format nil "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=~A" query)))
(defun temperature-at (&optional (query 94107))
(klacks:with-open-source (source (cxml:make-source (geo-lookup query)))
(klacks:find-element source "location")
(loop for tag in (list "country" "state" "city" "lat" "lon") collecting
(progn
(klacks:find-element source tag)
(klacks:peek-next source)
(klacks:current-characters source))
into list
finally (format t "The temperature near ~A, ~A, ~A, (lat:~A, lon:~A) is "
(third list) (second list) (first list) (fourth list) (fifth list)))
(klacks:find-element source "icao")
(klacks:peek-next source)
(klacks:with-open-source (source (cxml:make-source (current-conditions (klacks:current-characters source))))
(klacks:find-element source "temperature_string")
(klacks:peek-next source)
(format t "~A~%" (klacks:current-characters source)))))
This is the first time I've used either library (and many of their dependencies, it seems) so I'm probably going about it all wrong.
Also, the code is horribly brittle and shouldn't be though of as anything but a proof of concept. Someone with more time that I have at the moment could write something fairly straightforward to unmarshall these xml structures properly into classes and do more interesting things with them....
Anyway, it has no error recovery, no flexibility, no validation, and makes unreasonable assumptions about the data, but at least it works with different queries:
Code: Select all
CL-USER> (temperature-at 10451)
The temperature near Bronx, NY, US, (lat:40.82016373, lon:-73.92166138) is 63 F (17 C)
CL-USER> (temperature-at "san diego, ca")
The temperature near San Diego, CA, US, (lat:32.72109985, lon:-117.16430664) is 67 F (19 C)
And know I know where to go looking if/when I need to deal with XML data, so your quiz is a success for me.