Skip to content

Instantly share code, notes, and snippets.

@rietta
Created June 4, 2020 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rietta/2cf71dd59abfa01a004c391a20d967d9 to your computer and use it in GitHub Desktop.
Save rietta/2cf71dd59abfa01a004c391a20d967d9 to your computer and use it in GitHub Desktop.
American Express updated their online banking interface and provides an XML-based QFX format that the GnuCash updater cannot import. This Ruby script converts the new format back to the QFXSGML format that GnuCash knows how to import. Experimental; use at your own risk. It works for me.
#!/usr/bin/env ruby
# frozen_string_literal: true
# Convert new American Express QFX files (since June 2020) to older SGML format
# that GnuCash can import.
require 'nokogiri'
def show_usage
warn 'Usage: amex-ofx-downgrader.rb /path/to/source/file.qfx /path/to/output.qfx'
exit 1
end
show_usage unless ARGV.count == 2
source_file = ARGV.first
unless File.exist?(source_file)
warn "Input file #{source_file} does not exist."
show_usage
end
output_file = ARGV.last
if File.exist?(output_file)
warn "Output file #{output_file} ALREADY EXISTS! Wont overwrite."
show_usage
end
doc = Nokogiri::XML.parse(File.read(source_file))
File.open(output_file, 'wb') do |out|
out.puts <<~HEADER
OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:UTF8
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
HEADER
out.puts '<OFX>'
out.puts doc.xpath('//SIGNONMSGSRSV1').to_xml
out.puts doc.xpath('//CREDITCARDMSGSRSV1').to_xml
out.puts '</OFX>'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment