Last active
September 26, 2024 14:28
-
-
Save pioz/13e1bb26c63481afdd42b1665c4f2b98 to your computer and use it in GitHub Desktop.
Ruby script to get Amazon expenses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'date' | |
months_mapping = { | |
'gennaio' => '01', | |
'febbraio' => '02', | |
'marzo' => '03', | |
'aprile' => '04', | |
'maggio' => '05', | |
'giugno' => '06', | |
'luglio' => '07', | |
'agosto' => '08', | |
'settembre' => '09', | |
'ottobre' => '10', | |
'novembre' => '11', | |
'dicembre' => '12', | |
'january' => '01', | |
'february' => '02', | |
'march' => '03', | |
'april' => '04', | |
'may' => '05', | |
'june' => '06', | |
'july' => '07', | |
'august' => '08', | |
'september' => '09', | |
'october' => '10', | |
'november' => '11', | |
'december' => '12' | |
}.freeze | |
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| | |
if str_date | |
months_mapping.each do |month_name, month_number| | |
str_date.gsub!(month_name, month_number) | |
end | |
date = Date.strptime(str_date, "%d %m %Y").strftime("%B %Y").downcase | |
end | |
expenses[date] = (expenses[date] + str_amount.gsub(',', '.').to_f).round(2) if str_amount | |
end | |
expenses['total'] = expenses.values.sum.round(2) | |
pp 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