-
Notifications
You must be signed in to change notification settings - Fork 13
/
amazon.rb
executable file
·145 lines (106 loc) · 3.35 KB
/
amazon.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
#!/bin/env ruby
# encoding: utf-8
require 'rubygems'
require 'mechanize'
require 'highline/import'
# Will use global variables $tld, currency, overviewtext, nexttext
# They are used to identify different texts in different languages
# (eg. $overviewtext = 'Printable Order Summary' if UK is chosen,
# because that's the text of the link to a printable order summary
# on amazon.co.uk)
def get_order_links(agent, page, order_links)
links = page.links_with(:text => $overviewtext)
links.each { |link|
order_links.push(link)
}
next_link = page.link_with(:text => $nexttext)
if(next_link != nil) then
get_order_links(agent, agent.click(next_link), order_links)
end
end
total_sum = 0.0
email = ask('E-Mail:')
password = ask('Password:') { |q|
q.echo = '*'
}
# Dear amazon.com redesign,
# FUCK YOU!
# Sincerely, Pascal
puts "1) Germany 2) UK"
country = ask('Country: ', Integer) { |q| q.in = 1..2 }
case country
when 2
$tld = 'co.uk'
$currency = '£'
$overviewtext = 'Printable Order Summary'
$pricedelimiter = '.'
$thousandseperator = ','
$nexttext = 'Next »'
else
$tld = 'de'
$currency = 'EUR '
$overviewtext = 'Bestellübersicht drucken'
$pricedelimiter = ','
$thousandseperator = '.'
$nexttext = 'Weiter→'
end
a = Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}
# Disable SSL verification to make it work under windows without problems
a.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
puts 'Now scanning https://amazon.' + $tld + '/'
a.get('https://www.amazon.' + $tld + '/gp/your-account/order-history/') do |page|
logged_in_page = page.form_with(:name => 'signIn') do |f|
f.email = email
f.password = password
end.click_button
years = Array.new
select_form = logged_in_page.form_with(:id => 'timePeriodForm')
if (select_form == nil)
puts "Seems like I could not log you in. I'm sorry :("
Process.exit
end
select_form.field_with(:name => 'orderFilter').options.each do |option|
if( option.value == 'select-another' or option.value == 'last30' or option.value == 'months-6') then
next
end
option.select
year_page = select_form.submit
count = year_page.search(".//span[@class='num-orders']")
year = year_page.search(".//span[@class='a-dropdown-prompt']")
puts "Year " + year.text + ", " + count.text + " orders"
print "\t"
year_sum = 0.0
order_links = Array.new
get_order_links(a, year_page, order_links)
order_links.each do |link|
order_page = a.click(link)
money_str = order_page.search(".//body//table[1]/tr/td/table/tr[3]/td/b").text
if(money_str.index($currency) == nil) then
money_str = order_page.search(".//body//table[1]/tr/td/table/tr[4]/td/b").text
if(money_str.index($currency) == nil) then
puts $currency + " not found. Link: " + link.href
puts "title " + order_page.title
next
end
end
# multi-line result, select currency until end-of-string
money_str = money_str[money_str.index($currency)..-1]
money_str[$currency]= ''
# Delete all thousand seperators
if(money_str.index($thousandseperator) != nil) then
money_str[$thousandseperator]=''
end
money_str[$pricedelimiter]= '.'
eurs = money_str.to_f
total_sum = total_sum + eurs
year_sum = year_sum + eurs
print eurs
print " "
STDOUT.flush
end
puts "Done. Year total: " + year_sum.to_s
end
end
puts "Done. Total amount: " + total_sum.to_s