Skip to content

Instantly share code, notes, and snippets.

@stanaka
Created August 5, 2012 10:17
Show Gist options
  • Save stanaka/3263705 to your computer and use it in GitHub Desktop.
Save stanaka/3263705 to your computer and use it in GitHub Desktop.
Convert IDM export XML to KeepassX XML
#! /usr/bin/env ruby
# idm2keepassx.rb
# convert IDM export XML to KeepassX XML
#
# usage:
# cat idm_export.xml | idm2keepassx.rb > keepassx.xml
require "rexml/document"
require "date"
class Idm2KeepassX
def initialize(source)
@doc = REXML::Document.new source
@out = REXML::Document.new
@current = @out.add_element("database")
end
def check(element)
if element.name == 'folder' then
# puts "folder: " + element.attributes['name']
@current = @current.add_element("group")
@current.add_element("title").add_text(element.attributes['name'])
@current.add_element("icon").add_text("48")
element.elements.each { |e|
self.check(e)
}
@current = @current.parent
elsif element.name == 'item' then
# puts "item: " + element.attributes['name']
@current = @current.add_element("entry")
@current.add_element("title").add_text(element.attributes['name'])
@current.add_element("username").add_text(element.elements["account" ].text)
@current.add_element("password").add_text(element.elements["password"].text)
@current.add_element("url" ).add_text(element.elements["url" ].text)
month, day, year = element.elements["issueDate"].text.split('/')
date = Date.new(year.to_i, month.to_i, day.to_i)
@current.add_element("creation").add_text(date.iso8601)
month, day, year = element.elements["expirationDate"].text.split('/')
date = Date.new(year.to_i, month.to_i, day.to_i)
@current.add_element("expire" ).add_text(date.iso8601)
item = element.elements["item1"]
memo = ""
if item.attributes['name'].length > 0 || item.text then
name = item.attributes['name'].length > 0 ? item.attributes['name'] : "item1"
memo = "%s: %s\n" % [name, item.text]
end
item = element.elements["item2"]
if item.attributes['name'].length > 0 || item.text then
name = item.attributes['name'].length > 0 ? item.attributes['name'] : "item2"
memo += "%s: %s\n" % [name, item.text]
end
item = element.elements["serialNumber"]
if item.text then
memo += "Serial Number: %s\n" % item.text
end
item = element.elements["e-mail"]
if item.text then
memo += "e-mail: %s\n" % item.text
end
item = element.elements["file"]
if item.text then
memo += "file: %s\n" % item.text
end
if memo.length > 0 then
memo += "\n"
end
comment = element.elements["comment"]
@current.add_element("comment").add_text("%s%s"%[memo,comment.text])
@current.add_element("icon").add_text("3")
@current = @current.parent
else
# puts 'other type item is found'
end
end
def parse
@doc.elements.each("*/folder"){ |e|
self.check(e)
}
end
def dump
puts @out
end
end
source = STDIN.read
i2k = Idm2KeepassX.new(source)
i2k.parse
i2k.dump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment