Skip to content

Instantly share code, notes, and snippets.

@bmaland
Created June 4, 2012 06:33
Show Gist options
  • Save bmaland/2866684 to your computer and use it in GitHub Desktop.
Save bmaland/2866684 to your computer and use it in GitHub Desktop.
Parses XML dumps from discogs.com and converts it to a nice text format
#!/usr/bin/env ruby
require "active_support/all"
require "hashie"
require "nokogiri"
if ARGV.blank?
puts "Usage: #{$0} filename.xml"
exit 1
end
doc = Nokogiri::XML(open(ARGV.first))
doc.xpath("//release").each do |release_doc|
release = Hashie::Mash.new(Hash.from_xml(release_doc.to_s)).release
artists = release.artists.artist
artists = [artists] unless artists.kind_of?(Array)
artist = artists.map { |a| a.name }.join(" / ")
formats = release.formats.format
formats = [formats] unless formats.kind_of?(Array)
format = formats.map do |f|
if f.descriptions
format_desc = f.descriptions.description
format_desc = format_desc.join(", ") if format_desc.respond_to?(:each)
f.name + " " + format_desc
else
f.name
end
end.join(" / ")
puts artist.upcase + " - " + release.title + " (" + format + ")"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment