Last active
December 13, 2015 21:38
-
-
Save alexnask/4978547 to your computer and use it in GitHub Desktop.
Reap an m3u playlist from an iTunes library xml file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()" \ | |
format(id)) | |
(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()) | |
0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment