Well I'm back to once again to demonstrate just how bad a Factor programmer I still am. Tonight I'm going to dive into a little xml-rpc code to pull down an 'interesting' photo from Flickr.
Before we get into the mess of Factor code I used to actually get this working I need to do a little background first. (when you see my example, you'll see that I clearly have a lot of Factor shortcuts still to learn)
When you are working with any sort of Web Service, the first step you need to do is to get familiar with the various methods and details of that service. In our case, we'll be calling the flickr.interestingness.getList method, which for our needs accepts two parameters (api_key and per_page).
NOTE: In order to attempt this example, you'll need to get your own api_key from Flickr (they're free so take two!)
Without getting into all the boring details about XML-RPC or the Flickr Web Service itself, we do have to at least state that XML-RPC services are strongly-typed. That is they expect their parameters to be passed as one of a set of defined types (such as integers, strings, or structures). In our example, the interestingness method expects a one element struct, which itself contains a hashtable of our api_key and per_page values.
OK so assuming you have the basic understanding of the Flickr service, and what it's expecting to get and return, let's move on to our Factor code.
The first thing we'll need to do is define some of the things we'll be using with our USE statements (I'm doing all of this in the Factor Listener this time around, but usually with an example this lengthy in words you will want to create a factor file or vocabulary and factor out a lot of the craziness I do below)
USE: xml
USE: xml-rpc
USE: xml.utilities
Now we need to go ahead and do our crazy one line Factor bit (see if you can decipher this in it's given state):
{ H{ { "api_key" "YOUR_API_KEY_GOES_HERE" } { "per_page" 1 } } } "flickr.interestingness.getList" "http://www.flickr.com/services/xmlrpc/" invoke-method rpc-response-params 0 over nth nip string>xml "photo" tag-named* "http://farm" swap "farm" over at swap ".static.flickr.com/" swap "server" over at swap "/" swap "id" over at swap "_" swap "secret" over at swap ".jpg" swap drop append append append append append append append append
When it's all said and done, you should have a URL left on your stack. You can copy/paste that URL into your favorite web browser to see what it produces (pretty interesting eh?).
Tomorrow, when I have a little more time, I'll go over the details of the mess above and explain the hows and whys (at least as far as why I did what I did; which isn't to say it's the right Factor way to do it)...perhaps I'll even find a few new shortcuts to make it all a little less of a jumble as I walk through the explanation...
So stay tuned!



