-
Notifications
You must be signed in to change notification settings - Fork 2
/
product.py
75 lines (59 loc) · 2.23 KB
/
product.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
import json
import webbrowser
import requests
from helpers import format_size
class Product:
"""
Holds and retrieves all required product information
@param product: basic product information
"""
def __init__(self, product):
self.name = ''
self.url = product['Product URL']
self.size = format_size(product['Size'])
self.colour = product['Colour']
self.profile = product['Profile']
self.phone = product['Phone']
self.product_id = ''
self.variant_id = ''
self.cart = ''
self.get_details()
# Gets all of the required information to watch product
def get_details(self) -> None:
r = requests.get(self.url + '.json')
product = json.loads(r.text)['product']
self.product_id = product['id']
self.name = f"{product['title']} {'- ' + self.size if self.size else ''}".upper()
variants = product['variants']
for variant in variants:
if format_size(variant['title']) == self.size:
self.variant_id = variant['id']
self.generate_cart_url()
# Generates the cart URL for the product
def generate_cart_url(self) -> None:
url = self.url.split('/')
for x in url:
if '.' in x:
domain = x
self.cart = f'https://{domain}/cart/{self.variant_id}:1'
# Checks product availability
def is_available(self) -> bool:
catalog = f"{self.cart.split('/cart/')[0]}/products.json?limit=5000"
r = requests.get(catalog)
products = json.loads(r.text)['products']
for product in products:
if product['id'] == self.product_id:
variants = product['variants']
for variant in variants:
if variant['id'] == self.variant_id:
return variant['available']
return False
# Opens checkout page in browser
def open_in_browser(self) -> None:
try:
webbrowser.open(self.cart)
except:
print('Error occurred - could not open in browser.')
# Accessor method
def get_info(self) -> tuple[str]:
return self.name, self.variant_id, self.cart, self.profile, self.phone