-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintify.py
More file actions
146 lines (124 loc) · 3.97 KB
/
Copy pathprintify.py
File metadata and controls
146 lines (124 loc) · 3.97 KB
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
import os
import requests
import json
import base64
def check_shops_id(API_KEY_PRINTIFY):
printify_headers = {
'Authorization': f'Bearer {API_KEY_PRINTIFY}',
'Content-Type': 'application/json'
}
response = requests.get(
'https://api.printify.com/v1/shops.json',
headers=printify_headers
)
return response.json()
def publish_product(shop_id , product_id, API_KEY_PRINTIFY):
# Set up Printify API headers
printify_headers = {
'Authorization': f'Bearer {API_KEY_PRINTIFY}',
'Content-Type': 'application/json'
}
payload = {
"title": True,
"description": True,
"images": True,
"variants": True,
"tags": True,
"keyFeatures": True,
"shipping_template": True
}
# Upload the image to Printify
response = requests.post(
f'https://api.printify.com/v1/shops/{shop_id}/products/{product_id}/publish.json',
headers=printify_headers,
json=payload
)
if response.status_code != 200:
raise Exception(f'{response.status_code} Failed to publish {response.json()}')
published_product = response.json()
return published_product
def upload_image_to_printify(image_path, API_KEY_PRINTIFY):
# Read image data
with open(image_path, 'rb') as img_file:
image_data = img_file.read()
# Encode image data to Base64
base64_image_data = base64.b64encode(image_data).decode('utf-8')
# Set up Printify API headers
printify_headers = {
'Authorization': f'Bearer {API_KEY_PRINTIFY}',
'Content-Type': 'application/json'
}
imageName = os.path.basename(image_path)
# Upload the image to Printify
response = requests.post(
'https://api.printify.com/v1/uploads/images.json',
headers=printify_headers,
json={'file_name': imageName, "contents": base64_image_data}
)
if response.status_code != 200:
raise Exception(f'Failed to upload image: {response.json()}')
uploaded_image = response.json()
return uploaded_image['id']
def create_product_printify(image_id, metadata, shop_id, API_KEY_PRINTIFY):
# Set up Printify API headers
printify_headers = {
'Authorization': f'Bearer {API_KEY_PRINTIFY}',
'Content-Type': 'application/json'
}
product_data_01 = {
"title": metadata['name'],
"description": metadata['description'] + "\n\n" + metadata['hashtags'],
"blueprint_id": 725,
"print_provider_id": 39,
"variants": [
{
"id": 73871,
"price": 1499,
"is_enabled": True
},
{
"id": 73873,
"price": 1499,
"is_enabled": True
},
{
"id": 73875,
"price": 1499,
"is_enabled": False
},
{
"id": 73877,
"price": 1499,
"is_enabled": False
}
],
"print_areas": [
{
"variant_ids": [73871, 73873, 73875, 73877],
"placeholders": [
{
"position": "front",
"images": [
{
"id": image_id,
"x": 0.5,
"y": 0.5,
"scale": 1,
"angle": 0
}
]
}
]
}
]
}
# Upload the image to Printify
response = requests.post(
f'https://api.printify.com/v1/shops/{shop_id}/products.json',
headers=printify_headers,
json=product_data_01
)
if response.status_code != 200:
raise Exception(f'{response.status_code} Failed to create product: {response.json()}')
uploaded_product = response.json()
return uploaded_product