Now Playing
March 23, 2005

I’ve been having fun over the last couple of days playing with a nice combination of SOAP, Applescript, Amazon, Drupal and iTunes.

The result of this unholy alliance is my Now Playing script, which is yet another of those publish-what-iTunes-is-doing-as-if-anyone-cared tools.

At the moment I’ve got an applescript with an idle loop that gets the current track info from iTunes, looks it up on amazon to get a picture, then writes the information out to a small html file.

I’ve then got a bit of PHP code in a custom Drupal block which reads in the html.

At some point it would be nice to merge these into one script, but I think that there might be issues with a perl script on the server talking to an application running logged in as another user (time for a bit more experimentation).

I’m really impressed at the ease with which one can make SOAP requests from Applescript. This stuff has apparently been around since 2001, but I’ve been so wrapped up with Championship Manager that I had no idea.

Thanks to Tim Jarrett, for some scripts which helped set me off in the right direction.

As it happens, his Amazon Handler script seems to be a bit out of date now, so after a lot of head scratching, I figured out how to do things using the latest Amazon SOAP api.

By way of a thank you, here’s some example code. I haven’t generalised things as much as Tim did, so this function is for a very specific job (look up a track and return urls for the amazon page and an image), but it should give you some idea:


on amazonLookup(trackName, bandName)
	try
		set soapParameters to ¬
			 { ¬
				|SubscriptionId|:"1HT82XR9S43B5V8YDQ02", ¬
				|SearchIndex|:"Music", ¬
				|Title|:trackName, ¬
				|Artist|:bandName, ¬
				|ResponseGroup|:"Small,Images" ¬
			}

		using terms from application "http://www.apple.com/placebo"
			tell application "http://webservices.amazon.com/onca/soap?Service=AWSECommerceService"
				set soapResult to call soap ¬
					{ ¬
						method name: "ItemSearch", ¬
						method namespace uri: "http://webservices.amazon.com/AWSECommerceServices/2005-02-23", ¬
						parameters: soapParameters, ¬
						SOAPAction: "http://soap.amazon.com" ¬
					}	
			end tell
		end using terms from
		
		set soapOutput to |items| of soapResult
		set itemList to |item| of soapOutput
		repeat with i in itemList
			try
				set a to the itemattributes of i
				if the productgroup of a is "Music" then
					if the artist of a is bandName then
						set imageURL to the |url| of the smallimage of i
						set trackURL to the detailpageurl of i
						return {ok:true, track:trackURL, image:imageURL}
					end if
				end if
			end try
		end repeat
	end try
	
	return {ok:false}
end amazonLookup

It took me a long time to figure out exactly what parameters I was supposed to be supplying in the SOAP call (the Apple documentation is a bit sketchy), but once I did, it worked like a dream, and it opens up all sorts of interesting possibilities.

What you get back from the call soap call is a compound record which follows the same structure as the XML SOAP reply. From there it is easy enough to extract the information that you’re after.

As I mentioned, this code just gives a very specific example of one call to Amazon to do one thing. There’s a lot more you can do, and the Amazon documentation seems quite extensive.

Have fun…

« The Next Hypercard SmartSlab »
Got a comment on this post? Let me know at @samdeane@mastodon.org.uk.