-
Notifications
You must be signed in to change notification settings - Fork 0
/
amazoncrawler.py
290 lines (227 loc) · 6.97 KB
/
amazoncrawler.py
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
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ast
from cmd import Cmd
from datetime import datetime
from os.path import abspath, dirname, join
from time import sleep
import xmltodict
from progressbar import progressbar
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from assets.uris import HOME_PAGE, CART_PAGE
EXIT_SUCCESS, EXIT_FAILURE = 0, 1
OPTIONS = Options()
PROJECT_ROOT = abspath(dirname(__file__))
DRIVER_BIN = join(PROJECT_ROOT, "bin/chromedriver")
def add_to_cart(driver):
"""
Adds current product to cart.
:param driver: driver to be used.
:return:
"""
try:
add_to_chart_button = driver.find_element_by_id("add-to-cart-button")
add_to_chart_button.submit()
except Exception as e:
print("Could not add product to cart: {}".format(e))
return EXIT_FAILURE
return EXIT_SUCCESS
def go_to_home(driver):
"""
Redirects to cart page.
:param driver: driver to be used.
:return:
"""
try:
driver.get(HOME_PAGE)
except Exception as e:
print("Could not redirect to home page: {}".format(e))
return EXIT_FAILURE
return EXIT_SUCCESS
def go_to_cart(driver):
"""
Redirects to cart page.
:param driver: driver to be used.
:return:
"""
try:
driver.get(CART_PAGE)
except Exception as e:
print("Could not redirect to cart page: {}".format(e))
return EXIT_FAILURE
return EXIT_SUCCESS
def retrieve_stock(driver):
"""
Utilizes the 999 trick to retrieve the current stock of certain item.
:param driver: driver to be used.
:return:
"""
select = Select(driver.find_element_by_name("quantity"))
select.select_by_value("10")
quant_box = driver.find_element_by_name("quantityBox")
quant_box.send_keys(Keys.BACK_SPACE)
for i in range(3):
quant_box.send_keys(Keys.NUMPAD9)
quant_box.send_keys(Keys.ENTER)
# wait for correct stock to be set
sleep(5)
quant_box = driver.find_element_by_name("quantityBox")
return quant_box.get_attribute("value")
def get_current_stock_of_item(driver, item):
"""
Complete workflow of retrieving the current stock of a certain item.
:param driver: driver to be used.
:param item: item to retrieve the current stock for.
:return:
"""
go_to_home(driver)
driver.get(item)
add_to_cart(driver)
go_to_cart(driver)
return retrieve_stock(driver)
def remove_last_item_from_cart(driver):
"""
Removes the last added item from cart.
:param driver: driver to be used.
:return:
"""
try:
driver.get(CART_PAGE)
driver.find_element_by_xpath("//input[@type='submit' and @value='Löschen']").click()
except Exception as e:
print("Remove button could not be clicked: {}".format(e))
return EXIT_FAILURE
return EXIT_SUCCESS
def replace_ampersands_in_file(path_to_target_file):
"""
Replaces all ampersands in a certain file with its correct encoding.
:param path_to_target_file: file to replace encodings in.
:return:
"""
try:
with open(path_to_target_file, 'r') as file:
file_data = file.read()
file_data = file_data.replace('&', ' ')
with open(path_to_target_file, 'w') as file:
file.write(file_data)
except Exception as e:
print("Ampersands could not be replaced: {}".format(e))
return EXIT_FAILURE
return EXIT_SUCCESS
def create_log_file_if_not_exists(target_log_name):
"""
Creates log file if not exists.
:type target_log_name: name of target log file.
:return:
"""
import os
try:
if not os.path.exists(target_log_name):
with open(target_log_name, "w") as file_handler:
file_handler.write("item;stock" + "\n")
except Exception as e:
print("File could not be created: {}".format(e))
return EXIT_FAILURE
return EXIT_SUCCESS
def parse_config_file():
"""
Parses the local xml config file.
:return:
"""
global CONF
with open("config.xml") as fd:
CONF = xmltodict.parse(fd.read())
return CONF
def parse_selenium_chrome_options_from_config_file():
"""
Parses selenium specific options.
:return:
"""
selenium_options = CONF["data"]["selenium-options"]
if ast.literal_eval(selenium_options["headless-mode"]):
OPTIONS.add_argument("--headless")
return EXIT_SUCCESS
def retrieve_stock_of_all_items_in_config(driver):
"""
Utilizes the 999 trick to retrieve the current stock of all items specified in config.xml.
:return:
"""
for obj in progressbar(CONF["data"]["item"]):
try:
item_list = list(obj.items())
name, link = item_list[0][1], item_list[1][1]
stock = get_current_stock_of_item(driver, link)
remove_last_item_from_cart(driver)
go_to_home(driver)
create_log_file_if_not_exists(name + ".csv")
with open(name + ".csv", "a") as file_handler:
file_handler.write("{};{}\n".format(datetime.now(), stock))
except Exception as e:
print("Stock could not be retrieved for {}: {}".format(obj, e))
return EXIT_FAILURE
return EXIT_SUCCESS
class MyPrompt(Cmd):
@staticmethod
def do_retrieve_stock_of_all_items_in_config(args):
"""
Retrieves the stock.
:return:
"""
try:
retrieve_stock_of_all_items_in_config(BROWSER)
except Exception as e:
print("Command 'retrieve_stock_of_all_items_in_config failed': {}".format(e))
raise SystemExit
@staticmethod
def do_show_plots(args):
"""
Plots all csv files.
:return:
"""
from tools.plotter import Plotter
p = Plotter()
p.plot_and_show_all_plot_files(target_dir=".")
@staticmethod
def do_save_plots(args):
"""
Plots and saves csv files.
:return:
"""
from tools.plotter import Plotter
p = Plotter()
p.plot_and_save_all_plot_files(target_dir=".")
@staticmethod
def do_exit(args):
"""
Quits the program.
:return:
"""
print("Quitting.")
raise SystemExit
@staticmethod
def do_quit(args):
"""
Quits the program.
:return:
"""
print("Quitting.")
raise SystemExit
def bootstrap_config():
"""
Processes the config file.
:return:
"""
replace_ampersands_in_file("config.xml")
parse_config_file()
parse_selenium_chrome_options_from_config_file()
return EXIT_SUCCESS
if __name__ == '__main__':
bootstrap_config()
global BROWSER
BROWSER = Chrome(executable_path=DRIVER_BIN, options=OPTIONS)
prompt = MyPrompt()
prompt.prompt = '> '
prompt.cmdloop('Starting prompt...')