Skip to content

Instantly share code, notes, and snippets.

@haileys
Created August 27, 2016 05:15
Show Gist options
  • Save haileys/d962375d4cbddc2c9475244c0f1cc8b8 to your computer and use it in GitHub Desktop.
Save haileys/d962375d4cbddc2c9475244c0f1cc8b8 to your computer and use it in GitHub Desktop.
require "nokogiri"
require "pry"
doc = Nokogiri::XML(File.read("collection.nml"))
def update_playlist(doc, playlist_name, xpath)
playlist = doc.xpath("/NML/PLAYLISTS//NODE[@TYPE='PLAYLIST'][@NAME='#{playlist_name}']/PLAYLIST").first
playlist.children.remove
entry_nodes = doc.xpath(xpath).map { |entry|
loc = entry.xpath("LOCATION").first
"#{loc["VOLUME"]}#{loc["DIR"]}#{loc["FILE"]}"
}.map { |key|
doc.create_element("ENTRY") do |entry|
entry.add_child(doc.create_element("PRIMARYKEY", "", "TYPE" => "TRACK", "KEY" => key))
entry.add_child(doc.create_text_node("\n"))
end
}
entry_nodes.each do |entry|
playlist.add_child(entry)
playlist.add_child(doc.create_text_node("\n"))
end
playlist.attribute("ENTRIES").value = entry_nodes.count.to_s
end
# Set Cued playlist to tracks with the second cuepoint set
update_playlist(doc, "Cued", "/NML/COLLECTION/ENTRY[CUE_V2[@HOTCUE='1']]")
# Set Uncued playlist to tracks without the second cuepoint set
update_playlist(doc, "Uncued", "/NML/COLLECTION/ENTRY[not(CUE_V2[@HOTCUE='1'])]")
nml = doc.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)
# traktor puts a space before ?>, but nokogiri doesn't
# fix that up to avoid unnecessary diffs
nml.sub!("\"?>", "\" ?>")
File.write("collection.nml", nml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment