09.26.06

iTunes remote

Posted in AJAX, Python, javascript at 8:52 pm by Twm

My first AJAX app

I’m now officially Twm2.0 and i feel great.

What is iTunes Remote?

Lets say you have a nice PC hooked up to your swish HI-FI. It’s very inelegant and not very modern to have to go over to the PC every time you want to change the music or mute.Wouldn’t it be great if you could use your mobile phone to view playlists, skip tracks and mute all from the comfort of your armchair or bed.

Enter iTunes remote. It turns any device with a web browser into an iTunes remote control. This could be a slim laptop, or a modern wifi enabled cell phone.

iTunes Remote screenshot(click image for full screen shot)

What are the main features of iTunes remote?

  • Log into iTunes running on a remote PC in the house with any web enabled device
  • Click on the arrow next to a playlist to play it
  • Browse playlists and select an individual song. One click will cause the host PC to start playing.
  • Free text s earch for song,artist. One click on the results causes song to play
  • Stop/mute whatever is currently playing on iTunes
  • Display of album art for the current song retrieved from iTunes store.
  • Valuation of music – it displays how much money you have spent in ITunes store (assuming 79p per track)
  • Wake up timer – register a wakeup time and a playlist to gently ease you out of bed in the morining

The Web UI can be served in two flavours:

  1. Desktop grade which uses advanced browser technologies to emulate the iTunes UI.
  2. Simple device grade which uses simpler technology to deliver a multi page interface for devices with less power.

What use is it?

It was developed while I had broken my ankle and femur and getting up onto crutches to change the tune was a bit of a pain. So the driving use case is for immobile people to make use of their mobile in a local context.

But, what is great for the immobile is fantastic for the rest of us.

Technical notes

The aim of the design was to provide an excellent remote control on a device without having the user install anything. The chief advantages of this approach is.

  1. No client install. User just fires up a bookmark in the mobile web browser
  2. Target mutiple devices – even ones which haven’t been invented yet
  3. Almost Zero upgrade cost for user. e.g a new cell phone just needs a new bookmark in the web browser

Implementation details

The iTunes remote solution is authored in two parts following the “AJAX” model.

  • Seperation of content and display – DHTML + CSS takes care of the layout client side, python produces XML data.
  • Currently uses Apache web server to handle mutiple requests
  • Apache serves a single AJAX front page for the UI, there are no page reloads.
  • The python scripts use the COM interface exposed by iTunes to control the application

Challenges

A key challange in creating a consumer grade remote control is to ensure trouble free install. Bluetooth is a one approach. BT is clearly designed for local area connection of two devices and has concepts such as one time pairing for exactly this sort of use case. BT however requires an application to be written for the target device which contradicts the design goal.

Superficially, the web server based approach seems simple enough in a wifi household. The user installs a service on the PC and then points the mobile web browser to the URL of the server. The problem is that the host PC may be hidden behind a router, and complex configurations of router ports and firewalls.

I’d like to know what the best way of doing this is and will be looking to UPnP for the future. I only want a clean easy way of exposing a web server to another device on the same network (of course without running any code on the device).

Nokia 9500 iTunes Remote photos

The search query page:
Search query on the 9500 browser

The result of selecting a song from the search results:
Play a song using iTunes Remote

09.18.06

Flickr hack

Posted in Development, Python at 12:48 pm by Twm

[Update Jan 2008] Flickr have changed the system for protecting images and this hack no longer works.

Flickr is a photo sharing web site on the web. It has changed the way I take photographs and is a part of my daily internet life.

release all my shots as creative commons license and offer the highest quality images for download. The license means that people can use my images for non-commercial projects free of charge, without my explicit permission so long as I’m credited as the creator.

I have learnt so much about what makes a good photo by studying other’s work as they grow and evolve, but one thing which aways bugged me was people who keep their large images to themselves. As part of the learning process I like to be able to check on some detail in the images, or to see if the photograph is really as sharp as it initially appears in the small version.
Fortunately (or unfortunately for flickr users), you don’t have to be Quincy to deduce the url of the ‘private’ image. There exists a hack to build up the URL from searching the HTML source for the image page. This hack (which doesn’t work as described) is documented here.

I wrote a Python script which maps from a flickr photo URL to the URL of the originally uploaded JPEG. The script retrieves the HTML source via HTTP and parses out the interesting bits using regular expressions.

I used a marvelous regex debugging tool for python called kodos. Here is a screenshot of kodos in action.

Kodos
Link: The code

iTunes mess

Posted in Development, Python at 2:29 am by Twm

I was eager to start using python to manipulate my music collection and to do some nifty remote control stuff. So I downloaded the iTunes COM SDK from Apple and was pleasantly surprised by the comprehensive COM model which iTunes exports.

To make use of COM, the ActivePython distribution includes win32com allowing python programs to create and interrogate windows based COM objects such as XML parsers or office applications sucha s excel. When the iTunes application is loaded, it exposes a COM type library called “iTunes Type Library” which can be used to control the app There is no need to be scared by all the COM terminology, programming for iTunes is pretty intuitive. Once the COM connection is in made, simple tasks like setting the volume, or playing a playlist can be done with a single command.

I couldn’t get my noddy example to work. So tried running an example javascripts which come with the SDK to verify that it should work. This also failed so I repaired the installation of iTunes from control panel. On running a second time the javascript executed fine and greeted me with a dialog “created 600 playlists”. Bugger! It made a huge mess of my iTunes.

Th SDK example I chose to run creates a playlist for every album in the library, and it’s not thoughtful enough to create them in a separate folder. This was distressing since I have numerous lovingly crafted playlists for occasion such as “Sunday morning”, “been dumped”, “getting ready to go out”, and they were now drowned by useless album playlists.
So naturally my first iTunes python script is to undo the damage made by the example code. Sripting got me into this mess and scripting will set me free. It took maybe an hour to do which is pretty good considering I have no COM experience.

So a couple of take home messages

  1. If an application installer tells you to restart your machine now or later, it’s probably worth doing now if you are a COM hacker.
  2. It’s wise to have some notion of undo/confirmation in your script. e.g if your script deletes duplicate entries, then why not provide two scripts. one which creates a new playlist “duplicates” which allows the user to scan through it to check that all is well, and a second scripts which clears the duplicates playlist from the library for good.

Link: The code

09.16.06

Sony cybershot image splitter

Posted in Development, Graphics, Python at 8:03 pm by Twm

I’ve had two Sony cybershot cameras over the last few years, and they were a marvelous introduction to digital photography. One feature I particularly like was the multishot mode where sixteen shots are taken in quick succession.

From a technical point of view, the shots can be taken rapidly because each shot is a reduced resolution image stored as tiles in a single photo frame.

(click on the image for a larger view)

I quite like seeing the images laid out like this, they look similar the supersampler Lomo cameras with 4 lenses on them. However, I often want to play the frames back in an animation. This could be done using photoshop to copy each frame out and paste as a new layer and then save it as an animated GIF, however it’s too fiddly and error prone. So I decided to write a python program to help.

After some hunting around. I found the wonderful PIL (python image library). This library doesn’t do everything I need, notibly it can’t write mutiframe GIFs.

The code simply chops an image up into bitmaps(.bmp) which you can assemble with your favourite animation software. The reason for choosing bitmaps is to avoid further lossy compression.

This to me demonstrates the power of throw away ’scripting’ in the creative process. If you can express an idea or automate a laborious process in a few lines of code then the programming environment itself part of the creative tools suite rather than applications created by the programming environment. Fab.

09.14.06

Monty hall

Posted in Development, Python, maths at 10:34 pm by Twm

With my broken leg elivated I was watching “Deal or no Deal” on daytime TV and got wrapped up in probabilty problems related to game shows on the web. In the process I stumbled on the monty hall problem and remember being intrigued by this little counter-intuitive gem when I first discovered it.

I can’t explain it better than this excellent video:

So since I’m learning Python. I wrote a little script do play the game a few thousand times and report the actual outcomes to see if they really do match up with the theory.

Monte hall simulator
======================
When Playing a game of 10000 tries.
The probability of winning when switching is: 0.6667 (theoretical 2/3 = 0.66666…)
The probability of winning while sticking is: 0.3333 (theoretical 1/3 = 0.3333…)

Link: The code