Public
Snippet $3 authored by Alexander Klink

Script to download relevant currency data from fixer.io and output them in ledger pricemap formatting

get_currencydata.rb
Raw
#!/usr/bin/env ruby

require 'open-uri'
require 'net/http'
require 'json'
require 'pp'

ledger_file = ARGV[0]
if ! ledger_file then
  STDERR.puts "Usage: #{$0} ledger_file"
  exit 1
end

wanted_currencies = `ledger -f #{ledger_file} commodities`.split("\n").select { |c| c != "€" }

d_fixer  = Time.new.strftime("%Y-%m-%d")
d_ledger = Time.new.strftime("%Y/%m/%d")
content  = Net::HTTP.get(URI.parse("http://api.fixer.io/#{d_fixer}"))
j = JSON.parse(content)
wanted_currencies.each do |c|
  if j['rates'][c] then
    puts "P #{d_ledger} 00:00:00 € #{c}#{j['rates'][c]}"
  end
end