/
shopify.py
164 lines (140 loc) · 4.45 KB
/
shopify.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
from urllib.parse import urljoin
from requests import get
from typing import Dict, List
class Shopify:
@staticmethod
def format_url(url: str) -> str:
"""
Remove URL query strings
"""
url = url.split("?")[0]
if not url.endswith("/"):
url += "/"
return url
@staticmethod
def get_collection(url: str) -> List[Dict[str, str]]:
"""
Get collection's products
"""
url = Shopify.format_url(url)
url = urljoin(url, "products.json")
content = get(url).json()
return [
{
"id": product["id"],
"title": product["title"],
"url": urljoin(url, "/products/" + product["handle"]),
"brand": product["vendor"],
"type": product["product_type"],
"price": product.get("variants", [])[0].get("price", "N/A"),
"variants": [
{
"id": variant["id"],
"title": variant["title"],
"price": variant["price"],
"available": Shopify.get_available_status(
variant.get("available", "N/A")
),
}
for variant in product.get("variants", [])
],
"image": product["images"][0]["src"] or "",
}
for product in content["products"]
]
@staticmethod
def get_available_status(value) -> int:
"""
Get available status
"""
if value == True:
return 1
elif value == False:
return 0
else:
return -1
@staticmethod
def get_product(url: str) -> Dict[str, str]:
"""
Get product information
"""
url = Shopify.format_url(url)
url = url.rstrip("/") + ".js"
product = get(url).json()
return {
"id": product["id"],
"title": product["title"],
"url": urljoin(url, "/products/" + product["handle"]),
"brand": product["vendor"],
"price": product["price"] / 100 or "N/A",
"type": product["type"],
"variants": [
{
"id": variant["id"],
"title": variant["title"],
"price": variant["price"] / 100 or "N/A",
"available": Shopify.get_available_status(
variant.get("available", "N/A")
),
}
for variant in product.get("variants", [])
],
"image": product["media"][0]["src"] or "",
}
@staticmethod
def get_search_results(url: str, query: str) -> List[Dict[str, str]]:
"""
Get search results
"""
params = "resources[type]=product&resources[options][unavailable_products]=hide"
url = Shopify.format_url(url)
url = urljoin(
url, "/search/suggest.json?q={q}&{params}".format(q=query, params=params)
)
content = get(url).json()
return [
{
"id": product["id"],
"title": product["title"],
"url": urljoin(url, "/products/" + product["handle"]),
"brand": product["vendor"],
"image": product["image"],
"price": product["price"],
}
for product in content["resources"]["results"]["products"]
]
@staticmethod
def get_shopify_config(url: str) -> Dict[str, str]:
"""
Get the Shopify configuration
"""
try:
url = urljoin(url, "/cart.js")
content = get(url).json()
return content
except:
return None
@staticmethod
def is_collection(url: str) -> bool:
"""
Check if the URL is a valid collection
"""
if not "/collections/" in url:
return False
try:
products = Shopify.get_collection(url)
return len(products) > 0
except:
return False
@staticmethod
def is_product(url: str) -> bool:
"""
Check if the URL is a valid product
"""
if not "/products/" in url:
return False
try:
Shopify.get_product(url)
return True
except:
return False