Skip to content

Instantly share code, notes, and snippets.

@pioz
Last active February 6, 2024 11:51
Show Gist options
  • Save pioz/13e1bb26c63481afdd42b1665c4f2b98 to your computer and use it in GitHub Desktop.
Save pioz/13e1bb26c63481afdd42b1665c4f2b98 to your computer and use it in GitHub Desktop.
Ruby script to get Amazon expenses
require 'date'
regexp = /(\d{1,2} \w+ \d{4})|(?:-EUR (\d+,\d{2}))/
results = DATA.read.scan(regexp)
expenses = Hash.new { 0 }
date = nil
results.each do |str_date, str_amount|
date = Date.parse("0#{str_date}").strftime("%B-%Y") if str_date
if str_amount
amount = str_amount.gsub(',', '.').to_f
expenses[date] += str_amount.gsub(',', '.').to_f
end
end
expenses['total'] = expenses.values.sum.round(2)
puts expenses
# Copy data from https://www.amazon.it/cpe/yourpayments/transactions
__END__
6 febbraio 2024
Mastercard ****1234-EUR 9,99
Ordine n.xxx-xxxxxxx-xxxxxxx
Amazon.it
Mastercard ****1234-EUR 12,38
Ordine n.xxx-xxxxxxx-xxxxxxx
AMZN Mktp IT
5 febbraio 2024
Mastercard ****1234-EUR 8,99
Ordine n.xxx-xxxxxxx-xxxxxxx
AMZN Mktp IT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment