forked from pwaldhauer/amazon-account-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amazon.rb
executable file
·167 lines (123 loc) · 4.01 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/env ruby
# encoding: utf-8
require 'rubygems'
require 'mechanize'
require 'highline/import'
# Will use global variables $tld, $logintext, $accounttext, $orderstext, currency, overviewtext, nexttext
# They are used to identify different texts in different languages
# (eg. $logintext = 'personalised recommendations' if UK is chosen,
# because that's the text of the login-link on amazon.co.uk)
def get_order_links(agent, page, order_links)
links = page.links_with(:text => $overviewtext)
order_links += links
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'
$logintext = 'personalised recommendations'
$accounttext = 'Your Account'
$orderstext = 'Your Orders'
$currency = '£'
$overviewtext = 'Printable Order Summary'
$pricedelimiter = '.'
$thousandseperator = ','
$nexttext = 'Next »'
else
$tld = 'de'
$logintext = 'Melden Sie sich an'
$accounttext = 'Mein Konto'
$orderstext = 'Meine Bestellungen'
$currency = 'EUR '
$overviewtext = 'Bestellübersicht drucken'
$pricedelimiter = ','
$thousandseperator = '.'
$nexttext = 'Weiter→'
end
open('amazon.csv', 'w') { |f|
f.puts "year, amount"
}
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://amazon.' + $tld + '/') do |page|
start_page = page.link_with(:id => 'nav-your-account')
if (start_page != nil )
login_page = a.click(start_page)
logged_in_page = login_page.form_with(:id => 'ap_signin_form') do |f|
f.email = email
f.password = password
end.click_button
account_page = a.click(logged_in_page.link_with(:text => $accounttext))
else
account_page = a.click(page.link_with(:text => $accounttext))
end
orders_page = a.click(account_page.link_with(:text => $orderstext))
years = Array.new
select_form = orders_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
open('amazon.csv', 'a') { |f|
f.puts year.text + "," + year_sum.to_s
}
end
end
puts "Done. Total amount: " + total_sum.to_s