-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skinbaron.py
395 lines (334 loc) · 12.6 KB
/
skinbaron.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import time
import json
import pickle
import logging as log
from selenium import webdriver
from selenium.common.exceptions import (ElementClickInterceptedException,
NoSuchElementException,
StaleElementReferenceException)
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
log.basicConfig(level=log.INFO)
with open('config.json', 'r') as configFile:
config = json.load(configFile)
def start_driver():
useLocal = False
if useLocal:
# Uses local Firefox
driver = webdriver.Firefox()
else:
# Uses a Firefox node connected to this selenium grid
seleniumGridUrl = config['seleniumGridUrl']
driver = webdriver.Remote(options=webdriver.FirefoxOptions(),
command_executor=seleniumGridUrl)
return driver
def check_exists_by_xpath(xpath):
try:
driver.find_element(By.XPATH, xpath)
except NoSuchElementException:
return False
return True
def click_if_exists_by_xpath(xpath):
if check_exists_by_xpath(xpath) is True:
driver.find_element(By.XPATH, xpath).click()
# Accepts Cookies
def accept_cookies():
click_if_exists_by_xpath(
'/html/body/div[3]/div[3]/div/div/div[2]/div/button[2]')
# Close the welcome popup
def close_welcome_popup():
click_if_exists_by_xpath(
'/html/body/modal-container/div/div/sb-landing-page-modal/div[1]/button'
)
# Logging in with the loaded Steam Cookies
def login():
# Clicking on the Steam Redirect
driver.get('https://skinbaron.com/steam-login')
time.sleep(1)
# Clicking on the Sign In button in Steam and redirecting to Skinbaron
driver.find_element(By.XPATH, '//*[@id="imageLogin"]').click()
time.sleep(2)
# Waiting until returning logged in to skinbaron
if check_exists_by_xpath(
'/html/body/sb-root/div/sb-layout-header/sb-layout-header-default/div/header/nav/ul/li[1]/sb-profile-widget/a/span/span[2]/strong'
) is False:
log.warning('Requires manual login by user')
while True:
if check_exists_by_xpath(
'/html/body/sb-root/div/sb-layout-header/sb-layout-header-default/div/header/nav/ul/li[1]/sb-profile-widget/a/span/span[2]/strong'
) is True:
time.sleep(0.5)
break
time.sleep(0.1)
def get_price(price):
price = price.text
price = price.split('\n')
price.reverse()
price = price[0]
price = price.replace(' €', '').replace(',', '.')
price = float(price)
return price
def get_simple_items():
return_items = []
items = driver.find_elements(
By.XPATH, '//*[@id="offer-container"]/ul/li/sb-stackable-offer'
) + driver.find_elements(
By.XPATH, '//*[@id="offer-container"]/ul/li/sb-single-offer')
for item in items:
return_item = []
return_item.append(
item.find_element(By.XPATH,
'.//div[contains(@class, "product-name")]').text)
return_item.append(1)
prices = item.find_elements(
By.XPATH, './/div[contains(@class, "product-price-unlocked")]'
) + item.find_elements(
By.XPATH, './/div[contains(@class, "product-price-locked")]')
if prices == []:
prices = item.find_elements(
By.XPATH, './/div[contains(@class, "product-price")]')
print(get_price(prices[0]))
return_item.append(get_price(prices[0]))
prices.reverse()
return_item.append(get_price(prices[0]))
cart_buttons = item.find_elements(
By.XPATH, './/div[2]/div[7]/sb-buy-button/div/div/button'
) + item.find_elements(
By.XPATH,
'.//div/div[2]/div[4]/div[2]/sb-buy-button/div/div/div/button')
return_item.append(cart_buttons[0])
cart_buttons.reverse()
return_item.append(cart_buttons[0])
if return_item[4] == return_item[5]:
return_item[5] = None
return_item[3] = None
return_items.append(return_item)
return return_items
def get_advanced_items():
return_items = []
items = driver.find_elements(By.XPATH, '//*/li/sb-single-offer/div/div[2]')
for item in items:
return_item = []
name = item.find_element(By.XPATH, './/div[2]/div[1]').text
return_item.append(name)
price = item.find_element(By.XPATH, './/div[6]')
return_item.append(get_price(price))
cart_button = item.find_element(
By.XPATH, './/div[7]/sb-buy-button/div/div/button')
return_item.append(cart_button)
wear = item.find_element(By.XPATH, './/div[8]/p[2]').text
wear = float(wear.replace('WEAR ', '').replace('%', ''))
return_item.append(wear)
return_items.append(return_item)
return return_items
# Getting all items
def get_all_items():
return_items = []
# This while loop is going through all pages of items
x = 4
while True:
return_items.append(get_simple_items())
if check_exists_by_xpath(
f'//*[@id="offer-container"]/div/sb-one-sided-pagination/ul/li[{x}]/button'
) is True:
try:
driver.find_element(
By.XPATH,
f'//*[@id="offer-container"]/div/sb-one-sided-pagination/ul/li[{x}]/button'
).click()
except StaleElementReferenceException:
break
except ElementClickInterceptedException:
break
time.sleep(1)
else:
break
if x != 11:
x += 1
return return_items
# Starting the chromedriver
log.info('Starting driver.')
driver = start_driver()
log.info('Started driver')
# Loads the website
log.info('Loading Skinbaron.')
driver.get("https://skinbaron.de/")
time.sleep(3)
log.info('Closing popups.')
close_welcome_popup()
time.sleep(3)
accept_cookies()
log.info('Logging in.')
try:
cookies = pickle.load(open("cookies/cookies.pkl", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)
except Exception:
login()
driver.get("https://skinbaron.de/")
time.sleep(2)
pickle.dump(driver.get_cookies(), open("cookies/cookies.pkl", "wb"))
log.info('Logged in')
def clear_cart():
driver.get('https://skinbaron.de')
time.sleep(4)
ActionChains(driver).move_to_element(
driver.find_element(
By.XPATH,
'/html/body/sb-root/div/sb-layout-header/sb-layout-header-default/div/header/nav/ul/li[3]/sb-shopping-cart-widget/div/div/div[1]'
)).click().perform()
try:
while True:
driver.find_element(
By.XPATH,
'/html/body/sb-root/div/sb-layout-header/sb-layout-header-default/div/header/nav/ul/li[3]/sb-shopping-cart-widget/div/div/div[2]/div/div[2]/div/div/sb-cart-step-review/div/div/div[1]/div[1]/div[3]'
).click()
time.sleep(0.2)
except Exception:
total = 0
return total
def checkout_cart(excepted_total):
excepted_total = round(excepted_total, 2)
driver.find_element(
By.XPATH,
'/html/body/sb-root/div/sb-layout-header/sb-layout-header-default/div/header/nav/ul/li[3]/sb-shopping-cart-widget/div/div/div[1]'
).click()
time.sleep(0.5)
try:
cart_total = driver.find_element(
By.XPATH,
'/html/body/sb-root/div/sb-layout-header/sb-layout-header-default/div/header/nav/ul/li[3]/sb-shopping-cart-widget/div/div/div[2]/div/div[2]/div/div/sb-cart-step-review/div/div[3]/div/div[1]/div[2]'
).text
cart_total = float(cart_total.replace(' €', '').replace(',', '.'))
if cart_total != excepted_total:
log.info('Total of checkout is not matching')
log.info('Saved total: ' + str(excepted_total) + '€')
log.info('Real total: ' + str(cart_total) + '€')
return clear_cart()
driver.find_element(
By.XPATH,
'//*[@id="cart-container"]/div[2]/div/div/sb-cart-step-review/div/div[3]/div/div[2]/button[2]'
).click()
time.sleep(1)
# Choose balance as payment method
driver.find_element(
By.XPATH,
'//*[@id="cart-container"]/div[2]/div[2]/sb-cart-step-payment-method/div[1]/div[2]/ul/li[1]/div'
).click()
time.sleep(1)
# Accept the Widerrufsrecht checkbox
driver.find_element(
By.XPATH,
'//*[@id="cart-container"]/div[2]/div[2]/sb-cart-step-payment-method/div[1]/div[2]/label'
).click()
time.sleep(1)
# Clicking Pay Now
driver.find_element(
By.XPATH,
'//*[@id="cart-container"]/div[2]/div[2]/sb-cart-step-payment-method/div[2]/div/div[2]/div/button[2]'
).click()
time.sleep(1)
# Clicking Store in Inventory
driver.find_element(
By.XPATH,
'/html/body/modal-container/div/div/sb-select-buy-location-modal/div[3]/div/button'
).click()
time.sleep(2)
log.info('Checked out successfully')
return 0
except Exception as e:
log.error('Failed to check out')
log.error(e)
return clear_cart()
def calculate_f(p):
f = -0.083 * pow(p, 3) + 2.75 * pow(p, 2) - 30.667 * p + 118
return f
def add_item_to_cart(cart_button, name, price, total):
try:
action = ActionChains(driver)
action.move_to_element_with_offset(cart_button, 0, 0)
action.perform()
action.reset_actions()
time.sleep(0.2)
cart_button.click()
click_if_exists_by_xpath('/html/body/div[5]/div/div'
) # Click on the notification to remove it
log.info('Added ' + name + ' to cart')
total += price
except Exception as e:
log.error(e)
log.error('Failed to add ' + name + ' to cart')
return total
def buy_simple_search(search):
checkout = False
total = 0
link = search[1]
max_price = search[2]
if max_price is not None:
link = link + '&pub=' + f'{max_price}'
driver.get(link)
time.sleep(3)
items = get_simple_items()
for item in items:
name = item[0]
# stock = item[1]
price1 = item[2]
price2 = item[3]
cart_button1 = item[4]
cart_button2 = item[5]
if price1 <= max_price:
total = add_item_to_cart(cart_button1, name, price1, total)
checkout = True
elif (price2 is not None and price2 <= max_price):
total = add_item_to_cart(cart_button2, name, price2, total)
checkout = True
if checkout is True:
total = checkout_cart(total)
return checkout
def buy_advanced_item(search):
checkout = False
total = 0
basic_link = search[1]
factor = search[2]
pages = search[3]
for page in range(pages):
link = f'{basic_link}&page={page}'
driver.get(link)
time.sleep(2)
items = get_advanced_items()
for item in items:
name = item[0]
price = item[1]
cart_button = item[2]
wear = item[3]
max_float = calculate_f(price * 100)
if wear <= (max_float * factor):
total = add_item_to_cart(cart_button, name, price, total)
# buy_item(cart_button, name, price)
checkout = True
if checkout is True:
total = checkout_cart(total)
return checkout
def main():
searches = config['searches']
clear_cart()
for search in searches:
search_type = search[0]
if search_type == 'simple':
while True:
if buy_simple_search(search) is False:
break
driver.get('https://www.myexternalip.com/raw')
time.sleep(3)
elif search_type == 'advanced':
while True:
if buy_advanced_item(search) is False:
break
driver.get('https://www.myexternalip.com/raw')
time.sleep(3)
driver.get('https://www.myexternalip.com/raw')
x = 0
while True:
log.info(str(x) + ': Searching for offers.')
main()
x += 1