Speed enhancement. Changed the code to send products in batches.
This commit is contained in:
parent
d70da1d39d
commit
a129dddd8c
118
abacus2woo.py
118
abacus2woo.py
@ -3,9 +3,10 @@ import sqlite3
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import pyodbc
|
import pyodbc
|
||||||
|
import json
|
||||||
from woocommerce import API
|
from woocommerce import API
|
||||||
|
|
||||||
# import all_from_woo
|
import all_from_woo
|
||||||
|
|
||||||
conn = sqlite3.connect('imd.db') # intermediary db
|
conn = sqlite3.connect('imd.db') # intermediary db
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
@ -17,15 +18,17 @@ c.execute('''CREATE TABLE IF NOT EXISTS products (id INTEGER NOT NULL PRIMARY KE
|
|||||||
|
|
||||||
# Connect to WooCommerce API
|
# Connect to WooCommerce API
|
||||||
wcapi = API(
|
wcapi = API(
|
||||||
url="https://www.rasterdoo.com/",
|
url="https://www.rasterdoo.com",
|
||||||
consumer_key="ck_b3169b1723f6f39a965ed04ecfa77860fb89bbf5",
|
consumer_key="ck_b3169b1723f6f39a965ed04ecfa77860fb89bbf5",
|
||||||
consumer_secret="cs_a83f0217ed8d6191ab7de9df06a0c2b652f6bd57",
|
consumer_secret="cs_a83f0217ed8d6191ab7de9df06a0c2b652f6bd57",
|
||||||
wp_api=True,
|
wp_api=True,
|
||||||
timeout=360,
|
timeout=360,
|
||||||
version="wc/v2"
|
version="wc/v3"
|
||||||
)
|
)
|
||||||
print("Resting for for a bit...")
|
# Let's check if connection with WooCommerce is OK - list a sales report
|
||||||
# time.sleep(50)
|
# print(wcapi.get("reports/sales?date_min=2024-01-01&date_max=2024-02-04").json())
|
||||||
|
# print("Resting for for a bit...")
|
||||||
|
# time.sleep(5)
|
||||||
|
|
||||||
print("Connecting to MS SQL server")
|
print("Connecting to MS SQL server")
|
||||||
'''
|
'''
|
||||||
@ -81,34 +84,34 @@ print("Abacus row in works:"), row #
|
|||||||
snc = 0
|
snc = 0
|
||||||
for row in cursor:
|
for row in cursor:
|
||||||
while row is not None:
|
while row is not None:
|
||||||
# print("Abacus row entered for loop and it is not None")
|
# print("Abacus row entered for loop & it is not None")
|
||||||
# input("Press Enter to continue...") #
|
# input("Press Enter to continue...") #
|
||||||
aid = row[0]
|
aid = row[0]
|
||||||
aprice = float(row[1])
|
aprice = float(row[1])
|
||||||
aname = row[2]
|
aname = row[2]
|
||||||
asifra = row[3]
|
asifra = str(row[3])
|
||||||
aqty = int(row[4])
|
aqty = int(row[4])
|
||||||
agroup = row[5]
|
agroup = row[5]
|
||||||
print("MS Sql group value:", agroup)
|
# print("MS Sql group value:", agroup)
|
||||||
# This dictionary matches abacus product category id to equivalent id in woocommerce
|
# This dictionary matches abacus product category id to equivalent id in woocommerce
|
||||||
groupdict = {
|
groupdict = {
|
||||||
1: 21472, 2: 21473, 4: 21474, 5: 21475, 10: 21476, 11: 21477, 13: 21477, 15: 21477, 19: 21478, 24: 21479,
|
1: 21472, 2: 21473, 4: 21474, 5: 21475, 10: 21476, 11: 21477, 13: 21477, 15: 21477, 19: 21478, 24: 21479,
|
||||||
30: 21480, 32: 21481, 33: 21482, 37: 21483, 38: 21484, 44: 21485, 45: 21486, 47: 21477, 48: 21477
|
30: 21480, 32: 21481, 33: 21482, 37: 21483, 38: 21484, 44: 21485, 45: 21486, 47: 21477, 48: 21477
|
||||||
}
|
}
|
||||||
print("Coverted to Woocommerce group it becomes wgroup:", groupdict[agroup])
|
# print("Coverted to Woocommerce group it becomes wgroup:", groupdict[agroup])
|
||||||
if agroup in groupdict:
|
if agroup in groupdict:
|
||||||
wgroup = groupdict[agroup]
|
wgroup = groupdict[agroup]
|
||||||
print("Group in abacusu is from dictionary:", agroup, " - while woocommerce now holds:", wgroup) #
|
# print("Group in abacusu is from dictionary:", agroup, " - while woocommerce now holds:", wgroup) #
|
||||||
else:
|
else:
|
||||||
wgroup = 3557
|
wgroup = 3557
|
||||||
print("Group ", agroup, "does not exist in dictionary - we put it in woocommerce group -other-:", wgroup) #
|
# print("Group ", agroup, "does not exist in dictionary - we put it in woocommerce group -other-:", wgroup) #
|
||||||
|
|
||||||
# Test id Woo product exists in SQLite table
|
# Test id Woo product exists in SQLite table
|
||||||
q = (asifra,)
|
q = (asifra,)
|
||||||
c.execute('SELECT * FROM products WHERE sifra=?', q)
|
c.execute('SELECT * FROM products WHERE sifra=?', q)
|
||||||
print("Abacus data to be saved in SQLite:", aid, aprice, aname, asifra, aqty, wgroup)
|
# print("Abacus data to be saved in SQLite:", aid, aprice, aname, asifra, aqty, wgroup)
|
||||||
# input("Press Enter to continue...") #
|
# input("Press Enter to continue...") #
|
||||||
# We test SqlLite rows so that we update the table only with products that changed
|
# We test SqlLite rows so that we update the table only with product that changed
|
||||||
trow = c.fetchone()
|
trow = c.fetchone()
|
||||||
if trow is not None:
|
if trow is not None:
|
||||||
tnaziv = trow[3]
|
tnaziv = trow[3]
|
||||||
@ -118,30 +121,31 @@ for row in cursor:
|
|||||||
# print("There is an SQLite trow we need to check for an update: "), tnaziv, tprice, tqty, tgroup
|
# print("There is an SQLite trow we need to check for an update: "), tnaziv, tprice, tqty, tgroup
|
||||||
if aprice != tprice:
|
if aprice != tprice:
|
||||||
snc = 1
|
snc = 1
|
||||||
print("bad price")
|
print(aname, "has a bad price")
|
||||||
if aqty != tqty:
|
elif aqty != tqty:
|
||||||
snc = 1
|
snc = 1
|
||||||
print("bad qty")
|
print(aname, "has a bad qty")
|
||||||
if aname != tnaziv:
|
elif aname != tnaziv:
|
||||||
snc = 1
|
snc = 1
|
||||||
print("bad naziv")
|
print(aname, "has bad naziv: ", tnaziv)
|
||||||
if wgroup != tgroup:
|
elif wgroup != tgroup:
|
||||||
snc = 1
|
snc = 1
|
||||||
print(wgroup, "bad tgroup: ", tgroup)
|
print(wgroup, "has a bad tgroup: ", tgroup)
|
||||||
|
else:
|
||||||
|
snc = 0
|
||||||
if snc == 1:
|
if snc == 1:
|
||||||
# print("=======")
|
print(asifra, aname, snc)
|
||||||
c.execute('''UPDATE products SET aid = ?, price = ?, qty =?, naziv = ?,
|
c.execute('''UPDATE products SET aid = ?, price = ?, qty =?, naziv = ?,
|
||||||
snc = ?, grupa = ? WHERE sifra = ?''',
|
snc = ?, grupa = ? WHERE sifra = ?''',
|
||||||
(aid, aprice, aqty, aname, snc, wgroup, asifra))
|
(aid, aprice, aqty, aname, snc, wgroup, asifra))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
c.execute('SELECT * FROM products WHERE sifra=?', q)
|
c.execute('SELECT * FROM products WHERE sifra=?', q)
|
||||||
trow = c.fetchone()
|
trow = c.fetchone()
|
||||||
print(trow)
|
# print(trow)
|
||||||
print("=======")
|
# print("=======")
|
||||||
# input("Press Enter to continue...")
|
# input("Press Enter to continue...")
|
||||||
else:
|
else:
|
||||||
print("If sqlite trow does not exist (is ", trow, "), then first create one in Woo", aid, asifra, aname,
|
print("If sqlite trow does not exist (is ", trow, "), then first create one in Woo", aid, asifra, aname, aprice, aqty, wgroup)
|
||||||
aprice, aqty, wgroup)
|
|
||||||
# input("Press Enter to continue...") #
|
# input("Press Enter to continue...") #
|
||||||
aprice = str(aprice)
|
aprice = str(aprice)
|
||||||
data = {
|
data = {
|
||||||
@ -160,8 +164,6 @@ for row in cursor:
|
|||||||
],
|
],
|
||||||
}
|
}
|
||||||
w = wcapi.post("products", data).json()
|
w = wcapi.post("products", data).json()
|
||||||
|
|
||||||
|
|
||||||
# w = wcapi.get("products/categories").json()
|
# w = wcapi.get("products/categories").json()
|
||||||
|
|
||||||
class ListStream:
|
class ListStream:
|
||||||
@ -189,7 +191,7 @@ for row in cursor:
|
|||||||
(rogueone, aid, asifra, aname, aprice, aqty, 1, wgroup)) # if no product it will create
|
(rogueone, aid, asifra, aname, aprice, aqty, 1, wgroup)) # if no product it will create
|
||||||
print("Inserting rogueone in SQLlite data:", rogueone, aid, asifra, aname, aprice, aqty, 1, wgroup)
|
print("Inserting rogueone in SQLlite data:", rogueone, aid, asifra, aname, aprice, aqty, 1, wgroup)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
# rgpath = "\"products/" + str(rogueone) + "\""
|
# rgpath = "\"product_batch/" + str(rogueone) + "\""
|
||||||
# print rgpath
|
# print rgpath
|
||||||
# rg = wcapi.get(rogueone).json()
|
# rg = wcapi.get(rogueone).json()
|
||||||
# print("The rogueone:", rg)
|
# print("The rogueone:", rg)
|
||||||
@ -211,49 +213,45 @@ for row in cursor:
|
|||||||
# print("Resting for for a bit...")
|
# print("Resting for for a bit...")
|
||||||
# time.sleep(5)
|
# time.sleep(5)
|
||||||
conn.close()
|
conn.close()
|
||||||
|
print("Wait before sending")
|
||||||
|
# time.sleep(10)
|
||||||
|
|
||||||
# Update Woo with our SQLite table
|
# Update Woo with our SQLite table
|
||||||
conn = sqlite3.connect('imd.db') # intermediary db
|
conn = sqlite3.connect('imd.db') # intermediary db
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
c.execute('''SELECT * FROM products WHERE snc = 1 ORDER BY naziv''')
|
c.execute('''SELECT * FROM products WHERE snc = 1 ORDER BY naziv''')
|
||||||
trow = c.fetchone()
|
|
||||||
|
|
||||||
print("Selected for update:", trow)
|
|
||||||
# cleantrow is not None:
|
|
||||||
grupa = 0
|
grupa = 0
|
||||||
|
|
||||||
|
# Make a product_list
|
||||||
|
product_list = list()
|
||||||
|
trow = c.fetchone()
|
||||||
while trow is not None:
|
while trow is not None:
|
||||||
id = trow[0]
|
# count += 1
|
||||||
|
# print("Selected for update:", trow)
|
||||||
|
tid = trow[0]
|
||||||
tnaziv = trow[3]
|
tnaziv = trow[3]
|
||||||
tprice = str(trow[4])
|
tprice = str(trow[4])
|
||||||
tqty = trow[5]
|
tqty = trow[5]
|
||||||
tgroup = trow[7]
|
tgroup = trow[7]
|
||||||
# print type(id), type(tnaziv), type(tprice), type(tqty), "Grupa:", type(tgroup), tgroup
|
# product = [tid, tqty, tnaziv, tprice, tgroup]
|
||||||
# input("Press enter ...")
|
product = {'id': tid, 'manage_stock': 'true', 'stock_quantity': tqty, 'name': tnaziv, 'status': 'publish', 'regular_price': tprice, 'categories': [{'id': tgroup}]}
|
||||||
data = {
|
# print(product)
|
||||||
"id": id,
|
product_list.append(product)
|
||||||
"manage_stock": "true",
|
trow = c.fetchone()
|
||||||
"stock_quantity": tqty,
|
print(product_list)
|
||||||
"name": tnaziv,
|
# Make batches of products and send them to WooCommerce
|
||||||
"status": "publish",
|
l = len(product_list)
|
||||||
"regular_price": tprice,
|
print("Lenght: ", l)
|
||||||
"categories": [
|
batch_size = 100
|
||||||
{
|
|
||||||
"id": tgroup
|
for i in range(0, l, batch_size):
|
||||||
}
|
one_product_batch = product_list[i:i + batch_size]
|
||||||
],
|
data = {'update': one_product_batch}
|
||||||
}
|
# print(one_product_batch)
|
||||||
# print data
|
print("_________________________BATCH", i, "_________________________")
|
||||||
uplink = "products/" + str(id)
|
response = wcapi.post("products/batch", data).json()
|
||||||
# print uplink
|
print(response)
|
||||||
up = wcapi.put((uplink), data).json()
|
|
||||||
print(up)
|
|
||||||
# input("Press enter ...")
|
|
||||||
if trow is None:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
trow = c.fetchone()
|
|
||||||
print("___________________________________________")
|
|
||||||
print("Selected next for update:", trow)
|
|
||||||
c.execute('UPDATE products SET snc = 0')
|
c.execute('UPDATE products SET snc = 0')
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|||||||
@ -19,7 +19,8 @@ def initSQLite():
|
|||||||
consumer_key="ck_b3169b1723f6f39a965ed04ecfa77860fb89bbf5",
|
consumer_key="ck_b3169b1723f6f39a965ed04ecfa77860fb89bbf5",
|
||||||
consumer_secret="cs_a83f0217ed8d6191ab7de9df06a0c2b652f6bd57",
|
consumer_secret="cs_a83f0217ed8d6191ab7de9df06a0c2b652f6bd57",
|
||||||
wp_api=True,
|
wp_api=True,
|
||||||
version="wc/v2"
|
timeout=360,
|
||||||
|
version="wc/v3"
|
||||||
)
|
)
|
||||||
r = wcapi.get("products") # get woo web page where products are
|
r = wcapi.get("products") # get woo web page where products are
|
||||||
h = int(r.headers['X-WP-TotalPages']) # in the header we see number of tot pages
|
h = int(r.headers['X-WP-TotalPages']) # in the header we see number of tot pages
|
||||||
@ -31,7 +32,7 @@ def initSQLite():
|
|||||||
r = wcapi.get(goto_page)
|
r = wcapi.get(goto_page)
|
||||||
page_txt = r.json() # contents of one page ar stored in variable
|
page_txt = r.json() # contents of one page ar stored in variable
|
||||||
# We fill our SQLite table with Woo Products
|
# We fill our SQLite table with Woo Products
|
||||||
for product in page_txt: # first we go through products in this page
|
for product in page_txt: # first we go through product in this page
|
||||||
wsku = product.get('sku')
|
wsku = product.get('sku')
|
||||||
wid = product.get('id')
|
wid = product.get('id')
|
||||||
wname = product.get('name')
|
wname = product.get('name')
|
||||||
@ -41,9 +42,9 @@ def initSQLite():
|
|||||||
wqty = 0
|
wqty = 0
|
||||||
# FIX THIS! - It does not update - just inserts if new ...
|
# FIX THIS! - It does not update - just inserts if new ...
|
||||||
c.execute('INSERT INTO products VALUES (?,?,?,?,?,?,?,?)',
|
c.execute('INSERT INTO products VALUES (?,?,?,?,?,?,?,?)',
|
||||||
(wid, "", wsku, wname, wprice, wqty, 0)) # if no product it will create
|
(wid, "", wsku, wname, wprice, wqty, 0, "")) # if no product it will create
|
||||||
c.execute('UPDATE products SET aid = ?, sifra = ?, naziv = ?, price = ?, qty = ?, snc = ? WHERE id = ?',
|
c.execute('UPDATE products SET aid = ?, sifra = ?, naziv = ?, price = ?, qty = ?, snc = ? WHERE id = ?',
|
||||||
("", wsku, wname, wprice, wqty, 0, wid, ""))
|
("", wsku, wname, wprice, wqty, 0, wid))
|
||||||
print("Insert woocommerce data to SQLlite:", wid, "", wsku, wname, wprice, wqty, 0)
|
print("Insert woocommerce data to SQLlite:", wid, "", wsku, wname, wprice, wqty, 0)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user