Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: exporting a playlist from itunes as xml



Adam Ochonicki wrote:

What I'd like to do is take an iTunes playlist and export certain ID3 elements of each track to an xml file. Does anyone know how to do this?

What's the structure of the XML file, and which ID3 tags? Anyway, the following Python script outputs the name, album, artist and duration for each track in the named playlist. The template markup/logic can be modified to suit. You'll need to install the appscript package from my site in order to use it.


======= START SCRIPT =======

#!/usr/bin/env pythonw

import os.path, sys
from appscript import *
from HTMLTemplate import Template

#######
# Template

templatemarkup = u"""<?xml version="1.0" encoding="UTF-8"?>

<playlist node="con:playlist" name="Some Playlist">
<track node="rep:track">
    <name node="con:name">Track Name</name>
    <album node="con:album">Album</album>
    <artist node="con:artist">Artist Name</artist>
    <duration node="con:duration">180</duration>
</track>
</playlist>
"""

def render_template(node, playlist):
node.playlist.atts['name'] = playlist.name.get()
ref = playlist.tracks
trackdescriptions = zip(ref.name.get(), ref.artist.get(), ref.album.get(), ref.duration.get())
node.playlist.track.repeat(render_track, trackdescriptions)


def render_track(node, (name, artist, album, duration)):
    node.name.content = name
    node.album.content = album
    node.artist.content = artist
    node.duration.content = str(duration)

#######
# Main

def write(path, txt):
    f = open(os.path.expanduser(path), 'w')
    f.write(txt.encode('utf8'))
    f.close()

if len(sys.argv) != 3:
    print 'Usage: pythonw playlist_to_xml.py playlist-name output-file'
    sys.exit()
playlistname, path = sys.argv[1:3]
template = Template(render_template, templatemarkup)
playlist = app('iTunes').playlists[playlistname]
xml = template.render(playlist)
write(path, xml)

======= END SCRIPT =======

(Note: it's a bit of a cheat since HTMLTemplate isn't a proper XML templating engine, but as long as tag names are all lowercase there shouldn't be a problem.)


To use it, save the above script as playlist_to_xml.py, then use Terminal or 'do shell script' to run a command like:


    pythonw playlist_to_xml.py 'playlist name' '/path/to/file.xml'


The resulting XML file will look similar to this:

<?xml version="1.0" encoding="UTF-8"?>

<playlist name="Recently Played">
<track>
    <name>Verklärte Nacht 7. Adagio</name>
    <album>Berg: Violin Concerto, Schoenberg Verklärte Nacht</album>
    <artist>BBC Symphony Orchestra Donald Runnicles</artist>
    <duration>245</duration>
</track>
...
</playlist>


If you really want to write it all in AppleScript though then I'd suggest using Satimage's XMLLib osax to do your XML generation.


HTH

has
--
http://freespace.virgin.net/hamish.sanderson/


_______________________________________________ Do not post admin requests to the list. They will be ignored. Applescript-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/applescript-users/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.