· 9 years ago · Jan 25, 2017, 11:26 PM
1secret_key = 'secret_F9jmskJ5nLRHTPnBtf915Sh21hwfTmvm'
2postman_token = '70d5e1c3-bf58-274a-9e64-cad8f0016fef'
3
4dataset = ['name','id','sku','quantity','price','compareToPrice','unlimited','isShippingRequired','weight','enabled','categoryIds','warningLimit','fixedShippingRateOnly','fixedShippingRate','imageUrl','defaultCategoryId','seoTitle','seoDescription','options']
5dataset_combos = ['id','sku','imageUrl','quantity','unlimited','price','weight','warningLimit','options']
6
7def write_header(filew,dataset,combos):
8 write_data = []
9 if combos == True:
10 write_data.append('product_id')
11 for data in dataset:
12 write_data.append(data)
13 filew.writerow(write_data)
14
15def get_products(store_id,secret_key,postman_token,offset):
16 url = "https://app.ecwid.com/api/v3/" + str(store_id) + "/products?offset="+ str(offset)
17 querystring = {"token": secret_key}
18 headers = {
19 'content-type': "application/json;charset=utf-8",
20 'cache-control': "no-cache",
21 'postman-token': postman_token
22 }
23 response = requests.request("GET", url, headers=headers, params=querystring)
24 return response.content.decode()
25
26def get_current_ids(store_id,secret_key,postman_token):
27 offset = 0
28 data = []
29 running = True
30 while running:
31 products = json.loads(get_products(store_id,secret_key,postman_token,offset))
32 for product in products['items']:
33 data.append(product['id'])
34 if len(products['items'])<100:
35 running = False
36 offset = offset+100
37 return data
38
39def create_product(store_id,secret_key,postman_token, payload):
40 url = 'https://app.ecwid.com/api/v3/'+store_id+'/products'
41 querystring = {"token": secret_key}
42 headers = {
43 'content-type': "application/json;charset=utf-8",
44 'cache-control': "no-cache",
45 'postman-token': postman_token
46 }
47 response = requests.request("POST", url, data=json.dumps(payload), headers=headers, params=querystring)
48 if response.status_code == 200:
49 content = json.loads(response.text)
50 new_id = content['id']
51 return new_id
52 return
53
54def update_product(store_id,secret_key,postman_token, product_id, payload):
55 url = 'https://app.ecwid.com/api/v3/' + str(store_id) + '/products/' + str(product_id)
56 querystring = {"token": secret_key}
57 headers = {
58 'content-type': "application/json;charset=utf-8",
59 'cache-control': "no-cache",
60 'postman-token': postman_token
61 }
62 response = requests.request("PUT", url, data=json.dumps(payload), headers=headers, params=querystring)
63 if response.status_code == 200:
64 return product_id
65 return
66
67def upload_product_image(store_id,secret_key,postman_token, product_id, payload):
68 url = 'https://app.ecwid.com/api/v3/'+ store_id + '/products/' + str(product_id) + '/image'
69 querystring = {"token": secret_key}
70 headers = {
71 'content-type': "image/jpeg",
72 'cache-control': "no-cache",
73 'postman-token': postman_token
74 }
75 img = requests.get(payload, stream=True)
76 response = requests.post(url, data=img, headers=headers, params=querystring)
77 return response
78
79def get_combos(store_id,secret_key,postman_token,product_id):
80 url = "https://app.ecwid.com/api/v3/" + str(store_id) + "/products/"+ str(product_id) +"/combinations"
81 querystring = {"token": secret_key}
82 headers = {
83 'content-type': "application/json;charset=utf-8",
84 'cache-control': "no-cache",
85 'postman-token': postman_token
86 }
87 response = requests.request("GET", url, headers=headers, params=querystring)
88 data = response.content.decode()
89 data = json.loads(data)
90 data_out = []
91 for d in data:
92 print(d)
93 data_out.append(d['id'])
94 return data_out
95
96def create_combination(store_id,secret_key,postman_token, product_id,payload):
97 url = 'https://app.ecwid.com/api/v3/'+str(store_id)+'/products/' + str(product_id) + '/combinations'
98 querystring = {"token": secret_key}
99 headers = {
100 'content-type': "application/json;charset=utf-8",
101 'cache-control': "no-cache",
102 'postman-token': postman_token
103 }
104 response = requests.request("POST", url, data=json.dumps(payload), headers=headers, params=querystring)
105 return response
106
107def update_combination(store_id,secret_key,postman_token, product_id, combo_id, payload):
108 url = 'https://app.ecwid.com/api/v3/'+str(store_id)+'/products/' + str(product_id) + '/combinations/'+str(combo_id)
109 querystring = {"token": secret_key}
110 headers = {
111 'content-type': "application/json;charset=utf-8",
112 'cache-control': "no-cache",
113 'postman-token': postman_token
114 }
115 response = requests.request("PUT", url, data=json.dumps(payload), headers=headers, params=querystring)
116 return response
117
118def upload_combination_image(store_id,secret_key,postman_token, product_id, combo_id, payload):
119 url = 'https://app.ecwid.com/api/v3/'+ store_id + '/products/' + product_id + '/combinations/' + combo_id + '/image'
120 querystring = {"token": secret_key}
121 headers = {
122 'content-type': "image/jpeg",
123 'cache-control': "no-cache",
124 'postman-token': postman_token
125 }
126 img = requests.get(payload, stream=True)
127 response = requests.post(url, data=img, headers=headers, params=querystring)
128 return response
129
130def get_data():
131 root = tk.Tk()
132 root.withdraw()
133 fetch = []
134 product_file = filedialog.askopenfilename()
135 with open(product_file, 'r') as file:
136 file_reader = csv.reader(file, delimiter=',')
137 for row in file_reader:
138 row_list= []
139 for col in row:
140 row_list.append(col)
141 fetch.append(row_list)
142 file.close()
143 return fetch
144
145def import_product(store_id,secret_key,postman_token,payload,product_id=0):
146 if int(product_id) in get_current_ids(store_id,secret_key,postman_token):
147 return update_product(store_id,secret_key,postman_token,product_id,payload)
148 else:
149 return create_product(store_id,secret_key,postman_token, payload)
150
151def export_products(store_id,secret_key,postman_token):
152 offset = 0
153 running = True
154 while running:
155 print("exporting products " + str(offset)+":"+str(offset+100))
156 products = json.loads(get_products(store_id,secret_key,postman_token,offset))
157 with open("products" + str(datetime.date.today())+ "-"+ str(offset)+ ".csv", "w", newline='', encoding='utf-8') as file, open("combinations" + str(datetime.date.today())+ "-"+ str(offset)+ ".csv", "w",newline='') as file2:
158 filew = csv.writer(file, delimiter=',', quoting=csv.QUOTE_MINIMAL)
159 filew2 = csv.writer(file2, delimiter=',', quoting=csv.QUOTE_MINIMAL)
160 write_header(filew,dataset, False)
161 write_header(filew2,dataset_combos, True)
162 for product in products['items']:
163 write_data = []
164 for data in dataset:
165 try:
166 if data != 'categoryIds' and data != 'options':
167 write_data.append(str(product[data]))
168 elif data == 'categoryIds':
169 categorylist = []
170 for category in product['categories']:
171 categorylist.append(category['id'])
172 if len(categoryList):
173 write_data.append(categorylist)
174 except:
175 write_data.append('')
176 data = {'options':[]}
177 data['options'] = product['options']
178 write_data.append(json.dumps(data))
179 filew.writerow(write_data)
180 for combination in product['combinations']:
181 write_data = []
182 write_data.append(product['id'])
183 for data in dataset_combos:
184 try:
185 if data != 'options':
186 write_data.append(str(combination[data]))
187 except:
188 write_data.append('')
189 for option in combination['options']:
190 write_data.append(option['name'])
191 write_data.append(option['value'].replace('"','in'))
192 filew2.writerow(write_data)
193 file.close()
194 file2.close()
195 running = False
196 if len(products['items']) > 99:
197 running = True
198 offset = offset +100
199
200def import_products(store_id,secret_key,postman_token):
201 data = get_data()
202 for row_index, row in enumerate(data[1:]):
203 payload = {}
204 payload['name'] = row[0]
205 product_id = int(row[1])
206 payload['sku'] = row[2]
207 payload['quantity'] = row[3]
208 payload['price'] = row[4]
209 payload['compareToPrice'] = row[5]
210 payload['unlimited'] = row[6]
211 payload['isShippingRequired'] = row[7]
212 payload['weight'] = row[8]
213 payload['enabled'] = row[9]
214 payload['categoryIds'] = row[10]
215 payload['warningLimit'] = row[11]
216 payload['fixedShippingRateOnly'] = row[12]
217 payload['fixedShippingRate'] = row[13]
218 image_url = row[14]
219 payload['defaultCategoryId'] = row[15]
220 payload['seoTitle'] = row[16]
221 payload['seoDescription'] = row[17]
222 literals = ['sku','unlimited','isShippingRequired','enabled','fixedShippingRateOnly','options']
223 dels = []
224 for key in payload:
225 if key not in literals:
226 try:
227 payload[key] = literal_eval(payload[key])
228 except:
229 pass
230 else:
231 if payload[key].lower() == "false":
232 payload[key] = False
233 elif payload[key].lower() == "true":
234 payload[key] = True
235 if payload[key] == '' or payload[key] == '[]':
236 dels.append(key)
237 for dela in dels:
238 del payload[dela]
239 id1 = import_product(store_id,secret_key,postman_token,payload,product_id)
240 print(id1)
241 if id1:
242 response = upload_product_image(store_id,secret_key,postman_token, id1, image_url)
243 print(response)
244def import_options(store_id,secret_key,postman_token):
245 data = get_data()
246 payload = {}
247 for row_index, row in enumerate(data[1:]):
248 product_id = row[1]
249 payload = json.loads(row[18])
250 print(product_id)
251 print(update_product(store_id,secret_key,postman_token, product_id, payload))
252
253def import_combos(store_id,secret_key,postman_token):
254 root = tk.Tk()
255 root.withdraw()
256 data = get_data()
257 for row_index, row in enumerate(data[1:]):
258 ids = get_combos(store_id,secret_key,postman_token,row[0])
259 print('attempting to update row 1 for'+str(row[0]))
260 payload = {}
261 product_id = row[0]
262 combo_id = row[1]
263 payload['sku'] = row[2]
264 image_url = row[3]
265 payload['quantity'] = row[4]
266 payload['unlimited'] = row[5]
267 payload['price'] = row[6]
268 payload['weight'] = row[7]
269 payload['warningLimit'] = row[8]
270 literals = ['sku','unlimited','isShippingRequired','enabled','fixedShippingRateOnly','options']
271 dels = []
272 for key in payload:
273 if key not in literals:
274 try:
275 payload[key] = literal_eval(payload[key])
276 except:
277 pass
278 else:
279 if payload[key].lower() == "false":
280 payload[key] = False
281 elif payload[key].lower() == "true":
282 payload[key] = True
283 if payload[key] == '' or payload[key] == '[]':
284 dels.append(key)
285 for dela in dels:
286 del payload[dela]
287
288 options = []
289 x = 9
290 while x+1 < len(row):
291 option = {}
292 option['name'] = row[x]
293 option['value'] = row[x+1]
294 options.append(option)
295 x += 2
296 payload['options'] = options
297
298 if combo_id:
299 if int(combo_id) in ids:
300 print(update_combination(store_id,secret_key,postman_token, product_id,combo_id,payload).text)
301 else:
302 print(create_combination(store_id,secret_key,postman_token, product_id, payload).text)
303 if image_url:
304 print(update_combination_image(store_id,secret_key,postman_token, product_id, combo_id, image_url))
305 else:
306 print(create_combination(store_id,secret_key,postman_token, product_id, payload).text)
307 if image_url:
308 update_combination_image(store_id,secret_key,postman_token, product_id, combo_id, image_url)
309
310def delete_combos(store_id,secret_key,postman_token):
311 data = get_data()
312 cur_id = 0
313 for row_index, row in enumerate(data[1:]):
314 product_id = row[0]
315 if product_id == cur_id:
316 continue
317 cur_id = product_id
318 url = 'https://app.ecwid.com/api/v3/'+ store_id + '/products/' + str(product_id) + '/combinations'
319 querystring = {"token": secret_key}
320
321 headers = {
322 'content-type': "application/json;charset=utf-8",
323 'cache-control': "no-cache",
324 'postman-token': postman_token
325 }
326 response = requests.delete(url,headers=headers,params=querystring)
327 print(str(product_id) +str(response.status_code))
328
329def select():
330 command = var.get()
331 if command == 'export data':
332 export_products(store_id,secret_key,postman_token)
333 messagebox.showinfo("Export complete", "Export completed!")
334 if command == 'import products':
335 import_products(store_id,secret_key,postman_token)
336 if command == 'import options':
337 import_options(store_id,secret_key,postman_token)
338 if command == 'import combinations':
339 import_combos(store_id,secret_key,postman_token)
340 if command == 'delete_combos':
341 delete_combos(store_id,secret_key,postman_token)
342
343root = tk.Tk()
344# use width x height + x_offset + y_offset (no spaces!)
345root.geometry("%dx%d+%d+%d" % (330, 80, 200, 150))
346root.title("ComboTool")
347var = tk.StringVar(root)
348var.set('select action')
349choices = ['export data', 'import products', 'import options', 'import combinations']
350option = tk.OptionMenu(root, var, *choices)
351option.pack(side='left', padx=10, pady=10)
352button = tk.Button(root, text="start", command=select)
353mylabel = tk.Label(text="Example")
354mylabel.visible = True
355mylabel.place(x=20, y=50)
356button.configure(bg = "#000")
357button.pack(side='left', padx=20, pady=10)
358root.mainloop()
359
360
361# def write_combos():
362
363# for id1 in get_current_ids(store_id,secret_key,postman_token):
364# payload = {}
365# options = [{'name':'Canvas and Metal Prints','value':'Metal Print'},{'name':'Size','value':'Xtra Large 40in x 30in'}]
366# payload['options'] = options
367# payload['price'] = 500
368# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
369# payload = {}
370# options = [{'name':'Canvas and Metal Prints','value':'Canvas Wrap'},{'name':'Size','value':'Xtra Large 40in x 30in'}]
371# payload['options'] = options
372# payload['price'] = 425
373# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
374# payload = {}
375# options = [{'name':'Canvas and Metal Prints','value':'Un-stretched'},{'name':'Size','value':'Xtra Large 40in x 30in'}]
376# payload['options'] = options
377# payload['price'] = 300
378# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
379
380
381# payload = {}
382# options = [{'name':'Canvas and Metal Prints','value':'Metal Print'},{'name':'Size','value':'Large 24in x 30in'}]
383# payload['options'] = options
384# payload['price'] = 400
385# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
386# payload = {}
387# options = [{'name':'Canvas and Metal Prints','value':'Canvas Wrap'},{'name':'Size','value':'Large 24in x 30in'}]
388# payload['options'] = options
389# payload['price'] = 325
390# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
391# payload = {}
392# options = [{'name':'Canvas and Metal Prints','value':'Un-stretched'},{'name':'Size','value':'Large 24in x 30in'}]
393# payload['options'] = options
394# payload['price'] = 250
395# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
396
397# payload = {}
398# options = [{'name':'Canvas and Metal Prints','value':'Metal Print'},{'name':'Size','value':'Medium 16in x 20in'}]
399# payload['options'] = options
400# payload['price'] = 225
401# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
402# payload = {}
403# options = [{'name':'Canvas and Metal Prints','value':'Canvas Wrap'},{'name':'Size','value':'Medium 16in x 20in'}]
404# payload['options'] = options
405# payload['price'] = 185
406# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
407# payload = {}
408# options = [{'name':'Canvas and Metal Prints','value':'Un-stretched'},{'name':'Size','value':'Medium 16in x 20in'}]
409# payload['options'] = options
410# payload['price'] = 130
411# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
412
413# payload = {}
414# options = [{'name':'Canvas and Metal Prints','value':'Metal Print'},{'name':'Size','value':'Small 10in x 14in'}]
415# payload['options'] = options
416# payload['price'] = 108
417# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
418# payload = {}
419# options = [{'name':'Canvas and Metal Prints','value':'Canvas Wrap'},{'name':'Size','value':'Small 10in x 14in'}]
420# payload['options'] = options
421# payload['price'] = 98
422# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
423# payload = {}
424# options = [{'name':'Canvas and Metal Prints','value':'Un-stretched'},{'name':'Size','value':'Small 10in x 14in'}]
425# payload['options'] = options
426# payload['price'] = 85
427# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
428
429# payload = {}
430# options = [{'name':'Canvas and Metal Prints','value':'Metal Print'},{'name':'Size','value':'Petite 8in x 10in'}]
431# payload['options'] = options
432# payload['price'] = 80
433# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
434# payload = {}
435# options = [{'name':'Canvas and Metal Prints','value':'Canvas Wrap'},{'name':'Size','value':'Petite 8in x 10in'}]
436# payload['options'] = options
437# payload['price'] = 75
438# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
439# payload = {}
440# options = [{'name':'Canvas and Metal Prints','value':'Un-stretched'},{'name':'Size','value':'Petite 8in x 10in'}]
441# payload['options'] = options
442# payload['price'] = 50
443# print(create_combination(store_id,secret_key,postman_token, id1, payload).text)
444
445# write_combos()