-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpSync.py
354 lines (251 loc) · 9.67 KB
/
pSync.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# Original Author: Thomas Eady, Good Program Web Services 2018"
## ---------- DEPENDENCIES
import requests
import base64
import re
import pprint
import time
import os
import json
from colorama import Fore, Back, Style
from bs4 import BeautifulSoup as bs
from ebaysdk.finding import Connection as finding
from ebaysdk.shopping import Connection as shopping
## ---------- GV's
pp = pprint.PrettyPrinter(indent=4)
limit = 300
SLEEP_INTERVAL = 120 # 2 minutes
DEV_MODE = True
inputIndicator = "[ ?? ] "
runningIndicator = "[ .. ] "
errorIndicator = "[ !! ] "
message = "[ >> ] "
ebayUrl = ""
ebayDataRaw = None
ebayProducts = []
ebayStoreName = os.environ['ebayStoreName']
ebayApi = None
ebaySiteId = "EBAY-AU" #Change according to your locale
ebayAppId = os.environ['ebayAppId']
shopifyAPIKey = os.environ['shopifyAPIKey']
shopifyAPIPassword = os.environ['shopifyAPIPassword']
shopifyStoreName = os.environ['shopifyStoreName'] #https:// --> name <-- .myshopify.com/admin/products.json
## ---------- FUNCTIONS
def getExistingShopifyProducts():
""" Get all existing shopify products """
funId = "[ getShopifyProd ]"
if DEV_MODE: print(funId + message + 'Looking for existing Shopify products' + Style.RESET_ALL)
try:
auth = shopifyAPIKey + ":" + shopifyAPIPassword
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic {token}'.format(token=base64.b64encode(auth.encode()).decode('ascii'))
}
url = 'https://'+ shopifyStoreName +'.myshopify.com/admin/products.json?limit=' + limit
shopifyExistingProductsTemp = requests.get(url, headers=headers)
shopifyExistingProductsTemp = json.loads(shopifyExistingProductsTemp.text)
shopifyExistingProducts = []
count = 0
for row in shopifyExistingProductsTemp['products']:
productBlock = {}
productBlock['title'] = row['title']
productBlock['id'] = row['id']
productBlock['price'] = row['variants'][0]['price']
productBlock['variants'] = row['variants']
productBlock['body_html'] = row['body_html']
shopifyExistingProducts.append(productBlock)
if DEV_MODE: print(funId + message + 'Found ' + str(len(shopifyExistingProducts)) + ' products' + Style.RESET_ALL)
return shopifyExistingProducts
except Exception as e:
if DEV_MODE: print(funId + errorIndicator + 'Error getting shopify products: ' + str(e) + Style.RESET_ALL)
raise str(e)
def updateExistingShopifyProduct(product):
""" Update existing product on shopify if a match is found """
funId = "[ updateProd ]"
auth = shopifyAPIKey + ":" + shopifyAPIPassword
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic {token}'.format(token=base64.b64encode(auth.encode()).decode('ascii'))
}
url = 'https://' + shopifyStoreName + '.myshopify.com/admin/products/' + str(product['id']) + '.json'
data = {
"product": {
"id": str(product['id']),
"body_html": str(product['description']),
"variants": [
{
"id": product['variants'][0]['id'],
"price": product['price'],
"sku": product['variants'][0]['sku']
}
]
}
}
response = requests.put(url, headers=headers, data=json.dumps(data)) # Add new product
if DEV_MODE: print(funId + message + Fore.GREEN + "Done." + Style.RESET_ALL)
def addNewProduct(product):
""" Insert new product to shopify store """
funId = "[ addProd ]"
auth = shopifyAPIKey + ":" + shopifyAPIPassword
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic {token}'.format(token=base64.b64encode(auth.encode()).decode('ascii'))
}
url = 'https://' + shopifyStoreName + '.myshopify.com/admin/products.json'
images = []
for row in product["data"]["images"]:
images.append({
"src": row
})
data = {
"product": {
"images": images,
"title": product['name'],
"body_html": product['data']['description'],
"vendor": ebayStoreName,
"product_type": "Other",
"tags": "",
"published_scope": "global",
"variants": [
{
"price": product['data']['price'],
"inventory_quantity": int(product["data"]["quantity"]),
"inventory_management": "shopify",
"requires_shipping": True
}
],
"published": True
}
}
response = requests.post(url, headers=headers, data=json.dumps(data)) # Add new product
if DEV_MODE: print(funId + message + Fore.GREEN + "Done." + Style.RESET_ALL)
def getExtraData(productLink, itemId):
""" Get shopping data from from Ebay API (description & details) """
funId = "[ extraData ]"
data = {}
if DEV_MODE: print(funId + runningIndicator + "finding extra data for: " + productLink)
shoppingEbayApi = shopping(siteid = ebaySiteId, appid = ebayAppId, config_file=None)
shoppingEbayApi.execute('GetSingleItem', {'IncludeSelector': 'Description, Details', 'ItemID': str(itemId)})
item = shoppingEbayApi.response.dict()
prodData = item['Item']
#pp.pif DEV_MODE: print(prodData)
data['images'] = prodData['PictureURL']
data['description'] = prodData['Description']
data['quantity'] = prodData['Quantity']
data['price'] = prodData['CurrentPrice']['value']
data['price_currency'] = prodData['CurrentPrice']['_currencyID']
return data
def getEbayProductData():
""" Gather all ebay product data from given store """
funId = "[ ebayProdData ]"
if DEV_MODE: print(funId + runningIndicator + "Searching: " + ebayStoreName)
total = 0
try:
findingEbayApi = finding(siteid = ebaySiteId, appid = ebayAppId, config_file=None)
findingEbayApi.execute('findItemsIneBayStores', {'storeName': ebayStoreName, 'IncludeSelector':['Title']})
item = findingEbayApi.response.dict()
data = item["searchResult"]["item"]
total = str(len(data))
if DEV_MODE: print(funId + message + Fore.GREEN + "Successfully found " + total + " products." + Style.RESET_ALL)
except Exception as e:
if DEV_MODE: print(str(e))
return
if DEV_MODE: print(funId + runningIndicator + "Proccessing products...")
products = []
count = 0
for product in data:
if DEV_MODE: print(runningIndicator + str(count) + "/" + total)
productBlock = {}
productBlock["name"] = product['title']
productBlock["ebayLink"] = product['viewItemURL']
productBlock["data"] = getExtraData( productBlock["ebayLink"], product['itemId'] )
products.append(productBlock)
if len(productBlock["data"]["description"]) > 1:
desc = "desc found"
else:
desc = "desc not found"
if DEV_MODE: print(" |____ Name: " + str(productBlock["name"]) + "\n |____ Price: " + str(productBlock["data"]["price"]) + "\n |____ Description: " + desc + "\n |____ Images: " + str(productBlock["data"]["images"]) )
if count >= limit:
break
count += 1
if DEV_MODE: print(Style.RESET_ALL + funId + message + "Finished Cleaning Data")
return products
def syncAll(productData):
""" Synchronise all new products """
funId = "[ syncAll ]"
if DEV_MODE: print(runningIndicator + "Getting existing shopify products")
existingShopifyProducts = getExistingShopifyProducts()
if DEV_MODE: print(funId + 'FOUND ' + str(existingShopifyProducts) + " existing shopify products" )
if DEV_MODE: print(message + "Products Found: ")
for row in existingShopifyProducts:
if DEV_MODE: print(" |____ " + str(row['id']) + " " + row['title'])
if DEV_MODE: print(message + "Beginning product sync...")
count = 0
toProcess = {
}
toProcess['toAdd'] = []
toProcess['toUpdate'] = []
for product in productData:
if DEV_MODE: print(funId + runningIndicator + "Syncing: " + str(product['name']))
matchFound = False
updateShopifyProduct = {}
productTitle = product['name'].replace(" ", "").upper()
for row in existingShopifyProducts:
rowTitle = row['title'].replace(" ", "").upper()
if DEV_MODE: print(funId + 'TESTING IF: ' + productTitle + ' = ' + rowTitle + Style.RESET_ALL)
if rowTitle == productTitle:
if DEV_MODE: print(funId + Fore.GREEN + Style.BRIGHT + "Found" + Style.RESET_ALL)
matchFound = True
if float(row['price']) != float(product['data']['price']): #or str(row['body_html']) != str(product['data']['description']):
updateShopifyProduct = row
updateShopifyProduct['price'] = product['data']['price']
updateShopifyProduct['description'] = product['data']['description']
break
if not matchFound:
if DEV_MODE: print(funId + message + Fore.GREEN + "No match found, adding new product" + Style.RESET_ALL)
toProcess['toAdd'].append(product)
else:
if updateShopifyProduct:
toProcess['toUpdate'].append(updateShopifyProduct)
if DEV_MODE: print(funId + message + Fore.GREEN + "Match Found but prices out of sync...updating shopfiy price..." + Style.RESET_ALL)
else:
if DEV_MODE: print(funId + message + Fore.YELLOW + "Product title and price matches existing shopify title, moving on..." + Style.RESET_ALL)
count+= 1
if count >= limit:
break
# Add all new products
if DEV_MODE: print(funId + 'Products to add: ' + str(len(toProcess['toAdd'])))
for tAdd in toProcess['toAdd']:
if DEV_MODE: print(funId + tAdd['name'])
addNewProduct(tAdd)
# Update existing products
if DEV_MODE: print(funId + 'products to update: ' + str(len(toProcess['toUpdate'])))
for tUp in toProcess['toUpdate']:
if DEV_MODE: print(funId + tUp['title'])
updateExistingShopifyProduct(tUp)
def end():
""" Kill """
if DEV_MODE: print(Style.RESET_ALL)
exit()
def main():
""" Initialise sync and main while loop """
funId = "[ main ]"
print(Style.RESET_ALL)
print("Launching...")
errors = 0
while True:
try:
productData = getEbayProductData()
syncAll(productData)
print(funId + 'Full process complete, no errors')
except:
errors += 1
print(funId + 'Process error raised...restarting.')
print(funId + 'Total Errors: ' + str(errors))
print('Sleeping...')
time.sleep(SLEEP_INTERVAL)
print('Waking...')
end()
## ---------- LAUNCH
if __name__ == '__main__':
main()