Richard Jones' Log: Random stuff

Thu, 08 Jun 2006

I just don't seem to find the time for "real" weblog entries these days, so here's some random stuff:

  • Pixen looks like a great tool for pixel art - certainly better than The Gimp for such things. Pity it's OS X-only.
  • I should nudge Phil Hassey about running that lightcycle Python bot comp.

And ObPython - because it's been a while - here's a very short little utility I just wrote on the train that fills my iRiver T10 (512MB capacity) with random music from my iTunes library using the ratings I assign in the library:

# usage: itunes-load.py <export xml> <destination folder> <size>
# export xml is generated by iTunes' File > Export Library ...
# size may be in bytes or MB with a 'M' suffix
import urlparse, urllib, random, shutil, os, sys, cElementTree 

# command-line args
source_xml = sys.argv[1]
dest_folder = sys.argv[2]
assert os.path.isdir(dest_folder)
max_size = sys.argv[3]
if max_size[-1] == 'M': max_size = int(max_size[:-1]) * 1024 * 1024
else: max_size = int(max_size)

# parse the XML
library = cElementTree.ElementTree(file=source_xml)
sizes = {}
for track in library.getroot()[0].find('dict').findall('dict'):
    l = list(track)
    info = dict(zip([t.text for t in l[::2]], [t.text for t in l[1::2]]))
    if int(info.get('Rating', 0)) < 60:   # need 3+ stars
        continue
    track = urllib.unquote(urlparse.urlparse(info['Location'])[2])
    sizes[track] = int(info['Size'])

# shuffle
l = sizes.keys()
random.shuffle(l)

# fill
total_size = 0
for filename in l:
    size = sizes[filename]
    size += 512 - size%512  # 512 byte blocks
    total_size += size
    if total_size > max_size: break
    print '%3d%%'%(total_size * 100. / max_size), filename
    shutil.copy(filename, dest_folder)

The list comp / zip bit in the middle was kinda fun and I believe it constitutes the first time I've ever used a step slice argument, if you can believe that ;)

Comment by Daniel Bickett on Thu, 08 Jun 2006

"I should nudge Phil Hassey about running that lightcycle Python bot comp."

That sounds interesting -- I'm curious :)