Created
August 27, 2016 05:15
-
-
Save haileys/d962375d4cbddc2c9475244c0f1cc8b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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