Skip to content

Instantly share code, notes, and snippets.



Last active Dec 13, 2015
What would you like to do?
Reap an m3u playlist from an iTunes library xml file
use xml
use math
import math
import xml/[Xml, XPath]
import structs/ArrayList
import io/File
main: func(args: ArrayList<String>) -> Int {
if(args getSize() <= 3) {
"Usage: %s <path to iTunes Music Library.xml> <name of playlist to extract> <destination m3u file>" printfln(args get(0))
return 0
iTunesXml := File new(args get(1)) read()
playlist := args get(2)
dest := args get(3)
iTunes := XmlDoc new(iTunesXml, "iTunes.xml")
songs := iTunes evalXPath("/plist/dict/array/dict[key='Name' and string='%s']/array/*//integer/text()" format(playlist))
buff := Buffer new()
buff append("#EXTM3U\n")
songs each(|song|
// Song ID
id := song@ content toString() toInt()
// Tags
tags := iTunes evalXPath(
"/plist/dict/dict/key[text()='%d']/following-sibling::dict[1]/key[text()='Name' or text()='Artist' or text()='Total Time' or text() = 'Location']/following-sibling::*[1]/text()" \
(name, artist, tracklength, path) := (tags get(0)@ content toString(), tags get(1)@ content toString(), (tags get(2)@ content toString() toInt()) as Float/1000, tags get(3)@ content toString() replaceAll("%20", " "))
buff append("#EXTINF:%d,%s - %s\n%s\n" format(tracklength round() as Int, artist, name, path))
File new(dest) write(buff toString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.