-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmoneywellspent.rb
executable file
·280 lines (257 loc) · 8.83 KB
/
moneywellspent.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env ruby
# encoding: utf-8
begin
require 'rubygems'
require 'optparse'
require 'yaml'
require 'highline/import'
require 'mechanize'
require 'bigdecimal'
require 'logger'
rescue LoadError => err
puts "Gem missing:\n #{err}"
exit 1
end
# Logger
$log = Logger.new(STDOUT)
$log.level = Logger::INFO
$log.formatter = proc do |severity, datetime, progname, msg|
"#{msg}"
end
class MoneyWellSpent
def self.run()
parseopts
# Init
agent = Mechanize.new
agent.user_agent_alias = 'Linux Firefox'
agent.cookie_jar.clear!
agent.follow_meta_refresh = true
agent.redirect_ok = true
$log.debug "DEBUG: Accessing #{@@cfg[:url]}\n"
page = agent.get(@@cfg[:url])
$log.debug "DEBUG: Login page body\n#{page.body}\n"
login_form = page.form('signIn')
login_form.email = @@cfg[:login]
login_form.password = @@cfg[:password]
# Login
$log.info "Logging in to amazon.#{@@cfg[:site]}\n"
page = agent.submit(login_form, login_form.buttons.last)
$log.debug "DEBUG: First order page body\n#{page.body}\n"
# Get first page of orders
$log.info "Retrieving order history"
xpath = '//div[@class="a-box a-color-offset-background order-info"]//div[@class="a-fixed-right-grid-col a-col-left"]'
orders = page.parser.xpath(xpath).to_a
if orders.empty?
$log.warn "\nError retreiving orders or no orders available on " +
"amazon.#{@@cfg[:site]} during #{@@cfg[:year]}\n" +
"Is the supplied password correct?\n"
exit 1
end
# Get remaining pages
while !(page.link_with(:text => "#{@@cfg[:next]}→").nil?)
page = page.link_with(:text => "#{@@cfg[:next]}→").click
orders.concat(page.parser.xpath(xpath).to_a)
print "." if $log.info?
end
$log.info "\n\n"
# Parse order xml
strdata = []
orders.each do |order|
strdata << order.xpath('./div/div/div[@class="a-row a-size-base"]/span[@class="a-color-secondary value"]').children.to_a.map do |val|
val.text.gsub("\n", ' ').squeeze(' ').strip
end
end
# Delocalization
data = []
strdata.each do |strdata_item|
strdate = strdata_item[0]
strprice = @@cfg[:account_type] == 'business'.freeze ? strdata_item[2] : strdata_item[1]
# Date delocalization
if %w(de).include? @@cfg[:site]
strdate.gsub!(/Januar|Februar|März|April|Mai|Juni|Juli|August|September|Oktober|November|Dezember/,
'Januar' => 'January',
'Februar' => 'February',
'März' => 'March',
'April' => 'April',
'Mai' => 'May',
'Juni' => 'June',
'Juli' => 'July',
'August' => 'August',
'September' => 'September',
'Oktober' => 'October',
'November' => 'November',
'Dezember' => 'December')
end
date = Date.parse(strdate)
# Prize delocalization
if %w(de fr).include? @@cfg[:site]
price = strprice.gsub(/\./,'').scan(/EUR\s(\d+,\d\d)/)
if price.first.nil?
$log.warn "\nError parsing prices from order page\n" +
"Is the supplied account type (#{@@cfg[:account_type]}) correct?\n"
exit 1
end
strprice = price.first.first.gsub(/,/, '.')
elsif %w(com).include? @@cfg[:site]
strprice = strprice.gsub(/,/,'').scan(/\$(\d+\.\d\d)/).first.first
elsif %w(co.uk).include? @@cfg[:site]
strprice = strprice.gsub(/,/,'').scan(/\£(\d+\.\d\d)/).first.first
end
price = BigDecimal(strprice)
data << [date, price]
end
# Sort orders by date (data.reverse! would do the same in theory)
data.sort! { |a,b| a.first <=> b.first }
# Output
sum = BigDecimal("0")
data.each do |date, price|
unless @@cfg[:overview]
puts "#{date} #{'%10.2f'%price}"
end
sum += price
end
puts "Overall #{@@cfg[:year]}:#{'%10.2f'%sum}"
# CSV
if @@cfg[:csv]
$log.info "\n"
filepath = File.expand_path(@@cfg[:csv])
begin
$log.debug "Appending data to #{filepath}\n"
file = File.new(filepath, 'a')
data.each do |date, price|
file.write "#{date};#{'%.2f'%price}\n"
end
rescue => e
$log.fatal "Error writing csv #{filepath}.\n"
$log.warn "#{e.message}\n"
exit 1
ensure
file.close unless file.nil?
end
$log.info "Data successfully exported in csv format to #{filepath}\n"
end
end
def self.parseopts()
# Option/ configuration parsing
# Parse the command line options
attrs = {}
options = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [OPTION]..."
opts.separator "A script to summarize your money spent on Amazon"
opts.separator "Options:"
opts.on("-l [LOGIN]", "--login [LOGIN]",
"Specify the username (e-mail) of your Amazon account") do |login|
attrs[:login] = login
end
opts.on("-p [PASSWORD]", "--password [PASSWORD]",
"Specify the password of your Amazon account") do |password|
attrs[:password] = password
end
opts.on("-y [YEAR]", "--year [YEAR]",
"Specify the year to be summed up") do |year|
attrs[:year] = year
end
opts.on("-a [ACCOUNT]", "--account-type [ACCOUNT]",
"Specify account type (private or business) of your Amazon account") do |account_type|
unless %(private business).include? account_type
puts "Account type #{account_type} not supported."
puts "Currently only private or business accounts are supported."
exit 1
end
attrs[:account_type] = account_type
end
opts.on("-s [SITE]", "--site [SITE]",
"Specify the site to be queried. " +
"Currently amazon.{com,de,fr,co.uk} are supported") do |site|
attrs[:site] = site
end
opts.on("-o", "--overview", "Show overview only") do
attrs[:overview] = true
end
opts.on("-c [CSV]", "--csv [CSV]",
"Export data to CSV") do |csv|
attrs[:csv] = csv
end
opts.on("-d", "--debug", "Enable debug output") do
$log.level = Logger::DEBUG
end
opts.on("-q", "--quiet", "Show less information") do
$log.level = Logger::WARN
end
opts.on("-h", "--help", "Show this help") do
puts options
exit 0
end
end
options.parse!
# Read the default configuration file at ~/.moneywellspentrc
configf = {}
f = File.expand_path("~/.moneywellspentrc")
if File.exist?(f)
begin
$log.debug "Loading configuration file #{f}\n"
configf = YAML.load(File.read(f))
rescue => e
$log.warn "Error loading configuration file #{f}.\n"
$log.info "#{e.message}\n"
exit 1
end
else
$log.info "No configuration file #{f} found. Asking interactively\n"
end
# Make sure configf["default"] exists
configf["default"] ||= {}
@@cfg = configf["default"].merge(attrs)
# Ask for the settings if not given via command line or configuration file
unless @@cfg[:site]
$log.debug "No site given, asking"
@@cfg[:site] = ask("Enter the site to be summed up: ") { |q|
q.echo = true
}
end
unless @@cfg[:login]
$log.debug "No logininfo given, asking"
@@cfg[:login] = ask("Enter your #{@@cfg[:site]} username: ") { |q|
q.echo = true
}
end
unless @@cfg[:password]
$log.debug "No password given, asking"
@@cfg[:password] = ask("Enter your #{@@cfg[:site]} password: ") { |q|
q.echo = "*"
}
end
unless @@cfg[:account_type]
$log.debug "No account type given, set account type to private"
@@cfg[:account_type] = 'private'.freeze
end
unless @@cfg[:year]
$log.debug "No year given, asking"
@@cfg[:year] = ask("Enter the year to be summed up: ") { |q|
q.echo = true
}
end
# Site specific settings (URL + next_button)
if %w(amazon.de amazn.de de).include? @@cfg[:site]
@@cfg[:next] = "Weiter"
@@cfg[:site] = "de"
elsif %w(amazon.com amazn.com com us).include? @@cfg[:site]
@@cfg[:next] = "Next"
@@cfg[:site] = "com"
elsif %w(amazon.co.uk amazn.co.uk co.uk uk).include? @@cfg[:site]
@@cfg[:next] = "Next"
@@cfg[:site] = "co.uk"
elsif %w(amazon.fr amazn.fr fr).include? @@cfg[:site]
@@cfg[:next] = "Suivant"
@@cfg[:site] = "fr"
else
valid_sites = %w([amazon.]de [amazon.]com [amazon.]co.uk [amazon].fr)
$log.warn "Invalid site specified. Available sites:"
$log.warn "\t" + valid_sites.join(" ")
exit 1
end
@@cfg[:url] = "https://www.amazon.#{@@cfg[:site]}/gp/css/order-history?" +
"opt=ab&digitalOrders=1&unifiedOrders=0&orderFilter=year-#{@@cfg[:year]}"
end
end
MoneyWellSpent.run