enthusiast-source-shopify 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- enthusiast_source_shopify-0.1.0/LICENSE.md +8 -0
- enthusiast_source_shopify-0.1.0/PKG-INFO +20 -0
- enthusiast_source_shopify-0.1.0/README.md +3 -0
- enthusiast_source_shopify-0.1.0/enthusiast_source_shopify/__init__.py +1 -0
- enthusiast_source_shopify-0.1.0/enthusiast_source_shopify/source.py +129 -0
- enthusiast_source_shopify-0.1.0/pyproject.toml +16 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Copyright 2024 Upside Lab sp. z o.o.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
8
|
+
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: enthusiast-source-shopify
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary:
|
|
5
|
+
Author: Rafal Cymerys
|
|
6
|
+
Author-email: rafal@upsidelab.io
|
|
7
|
+
Requires-Python: >=3.9,<4.0
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Requires-Dist: enthusiast-common (>=0.1.0,<0.2.0)
|
|
14
|
+
Requires-Dist: shopifyapi (>=12.7.0,<13.0.0)
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# Enthusiast Source Shopify
|
|
18
|
+
|
|
19
|
+
This Enthusiast plugin enables the import of product data directly from Shopify.
|
|
20
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .source import ShopifyProductSource as ShopifyProductSource
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import shopify
|
|
3
|
+
|
|
4
|
+
from enthusiast_common import ProductDetails, BaseProductSourcePlugin
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ShopifyProductSource(BaseProductSourcePlugin):
|
|
8
|
+
def __init__(self, data_set_id, config: dict):
|
|
9
|
+
"""
|
|
10
|
+
Initialize the plugin with the parameters to access shop.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
data_set_id (int): identifier of a data set that products are assigned to.
|
|
14
|
+
config (dict): Parameters such as shop url or access token to configure a plugin.
|
|
15
|
+
"""
|
|
16
|
+
super().__init__(data_set_id, config)
|
|
17
|
+
|
|
18
|
+
self.shop_url = config.get("shop_url")
|
|
19
|
+
self.access_token = config.get("access_token")
|
|
20
|
+
self.session = shopify.Session(self.shop_url, "2024-10", self.access_token)
|
|
21
|
+
shopify.ShopifyResource.activate_session(self.session)
|
|
22
|
+
|
|
23
|
+
def get_query_template(self):
|
|
24
|
+
"""Prepares template for querying shopify platform.
|
|
25
|
+
|
|
26
|
+
Template is used to run queries to loop through all pages returned by Shopify.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
return """query ($first: Int!, $after: String) {
|
|
30
|
+
products(first: $first, after: $after) {
|
|
31
|
+
edges {
|
|
32
|
+
node {
|
|
33
|
+
id
|
|
34
|
+
title
|
|
35
|
+
description
|
|
36
|
+
productType
|
|
37
|
+
tags
|
|
38
|
+
handle
|
|
39
|
+
productType
|
|
40
|
+
category {fullName}
|
|
41
|
+
variants(first: 50) {
|
|
42
|
+
edges {
|
|
43
|
+
node {
|
|
44
|
+
title
|
|
45
|
+
price
|
|
46
|
+
sku
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
pageInfo {
|
|
53
|
+
hasNextPage
|
|
54
|
+
endCursor
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
def get_product(self, shopify_product) -> ProductDetails:
|
|
61
|
+
"""Translates product definition received from Shopify into ECL product.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
shopify_product: a product returned by Shopify API
|
|
65
|
+
Returns:
|
|
66
|
+
ProductDetails: product definition used by ECL to sync a product.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
product = ProductDetails(
|
|
70
|
+
entry_id=shopify_product["id"],
|
|
71
|
+
name=shopify_product["title"],
|
|
72
|
+
slug=shopify_product["handle"],
|
|
73
|
+
description=shopify_product["description"],
|
|
74
|
+
# SKUs and prices are defined at a variant's level.
|
|
75
|
+
sku="",
|
|
76
|
+
price=0,
|
|
77
|
+
# Combine several attributes to create meaningful properties.
|
|
78
|
+
properties=str(
|
|
79
|
+
{
|
|
80
|
+
"productType": shopify_product.get("productType", ""),
|
|
81
|
+
"tags": shopify_product.get("tags", ""),
|
|
82
|
+
}
|
|
83
|
+
),
|
|
84
|
+
categories=shopify_product.get("category", "")
|
|
85
|
+
)
|
|
86
|
+
# Collect attributes from variants level.
|
|
87
|
+
prices = []
|
|
88
|
+
skus = []
|
|
89
|
+
for variant_edge in shopify_product.get("variants", {}).get("edges", []):
|
|
90
|
+
prices.append(float(variant_edge["node"].get("price", 0)))
|
|
91
|
+
skus.append(str(variant_edge["node"].get("sku", "")))
|
|
92
|
+
|
|
93
|
+
product.price = min(prices)
|
|
94
|
+
product.sku = ", ".join(skus)
|
|
95
|
+
|
|
96
|
+
return product
|
|
97
|
+
|
|
98
|
+
def fetch(self) -> list[ProductDetails]:
|
|
99
|
+
"""Fetch product list.
|
|
100
|
+
|
|
101
|
+
Returns:
|
|
102
|
+
list[ProductDetails]: A list of products.
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
query = self.get_query_template()
|
|
106
|
+
products = []
|
|
107
|
+
has_next_page = True
|
|
108
|
+
cursor = None
|
|
109
|
+
|
|
110
|
+
while has_next_page:
|
|
111
|
+
variables = {
|
|
112
|
+
"first": 50,
|
|
113
|
+
"after": cursor
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
response = shopify.GraphQL().execute(query, variables=variables)
|
|
117
|
+
|
|
118
|
+
result_data = json.loads(response)
|
|
119
|
+
products_data = result_data.get("data", {}).get("products", {})
|
|
120
|
+
product_edges = products_data.get("edges", [])
|
|
121
|
+
page_info = products_data.get("pageInfo", {})
|
|
122
|
+
|
|
123
|
+
for product_edge in product_edges:
|
|
124
|
+
products.append(self.get_product(product_edge["node"]))
|
|
125
|
+
|
|
126
|
+
has_next_page = page_info.get("hasNextPage", False)
|
|
127
|
+
cursor = page_info.get("endCursor")
|
|
128
|
+
|
|
129
|
+
return products
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "enthusiast-source-shopify"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = ""
|
|
5
|
+
authors = ["Rafal Cymerys <rafal@upsidelab.io>"]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
|
|
8
|
+
[tool.poetry.dependencies]
|
|
9
|
+
python = "^3.9"
|
|
10
|
+
enthusiast-common = "^0.1.0"
|
|
11
|
+
shopifyapi = "^12.7.0"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
[build-system]
|
|
15
|
+
requires = ["poetry-core"]
|
|
16
|
+
build-backend = "poetry.core.masonry.api"
|