-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolfbidder_watcher.py
72 lines (57 loc) · 3.11 KB
/
golfbidder_watcher.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
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import time
import json
from os import environ
watches = json.loads(environ.get('GOLFBIDDER_WATCHES', '[]'))
def get_all_products(urls):
products = []
for url in urls:
if '/SearchResults' in url:
search_page = BeautifulSoup(requests.get(url).text, features='html.parser')
model_urls = [urljoin(url, model_block.find('a')['href']) for model_block in search_page.find_all('div', {'class': 'gallery-item-content'})]
products.extend(get_all_products(model_urls))
elif '/models/' in url:
model_page = BeautifulSoup(requests.get(url).text, features='html.parser')
model_blocks = model_page.find_all('div', {'class': 'col-info-cell'})[1:]
for model_block in model_blocks:
product_base = {
'product_name': model_block.find('div', {'class': 'col-model'}).find('h3', {'class': 'product-name'}).find('a').text,
'price': float(f"{model_block.find('div', {'class': 'col-price'}).find('span', {'class': 'integer-part'}).text}.{model_block.find('div', {'class': 'col-price'}).find('span', {'class': 'decimal-part'}).text[1:-2]}"),
'url': urljoin(url, model_block.find('div', {'class': 'col-model'}).find('h3', {'class': 'product-name'}).find('a')['href'])
}
product_attributes = {
attribute_block.find('label', {'class': 'attribute-label'}).text: attribute_block.find('span', {'class': 'attribute-value'}).text
for attribute_block in model_block.find('div', {'class': 'col-model'}).find_all('p', {'class': 'attribute'})
}
products.append({**product_base, **product_attributes})
else:
print(f"Ignoring invalid URL: {url}")
return products
def watch_golfbidder():
while True:
for watch in watches:
products = get_all_products(watch['urls'])
print(f"Found {len(products)} products for watch {watch['name']}")
any_product_found = False
for product in products:
matches_conditions = True
for condition in watch['conditions']:
if condition['type'] == 'attribute':
if condition['label'] in product:
if product[condition['label']] not in condition['values']:
matches_conditions = False
break
elif condition['type'] == 'max_price':
if product['price'] > condition['value']:
matches_conditions = False
break
if matches_conditions:
print(f"Found product matching conditions: {product['url']}")
any_product_found = True
if not any_product_found:
print("Found no products matching conditions")
time.sleep(int(environ.get('SLEEP_INTERVAL', '60')))
if __name__ == '__main__':
watch_golfbidder()