Compare commits

..

No commits in common. "master" and "v1.0" have entirely different histories.
master ... v1.0

3 changed files with 64 additions and 63 deletions

View File

@ -3,10 +3,9 @@ import sqlite3
import sys
import time
import pyodbc
import json
from woocommerce import API
import all_from_woo
# import all_from_woo
conn = sqlite3.connect('imd.db') # intermediary db
c = conn.cursor()
@ -18,17 +17,15 @@ c.execute('''CREATE TABLE IF NOT EXISTS products (id INTEGER NOT NULL PRIMARY KE
# Connect to WooCommerce API
wcapi = API(
url="https://www.rasterdoo.com",
url="https://www.rasterdoo.com/",
consumer_key="ck_b3169b1723f6f39a965ed04ecfa77860fb89bbf5",
consumer_secret="cs_a83f0217ed8d6191ab7de9df06a0c2b652f6bd57",
wp_api=True,
timeout=360,
version="wc/v3"
version="wc/v2"
)
# Let's check if connection with WooCommerce is OK - list a sales report
# 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("Resting for for a bit...")
# time.sleep(50)
print("Connecting to MS SQL server")
'''
@ -84,34 +81,34 @@ print("Abacus row in works:"), row #
snc = 0
for row in cursor:
while row is not None:
# print("Abacus row entered for loop & it is not None")
# print("Abacus row entered for loop and it is not None")
# input("Press Enter to continue...") #
aid = row[0]
aprice = float(row[1])
aname = row[2]
asifra = str(row[3])
asifra = row[3]
aqty = int(row[4])
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
groupdict = {
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
}
# print("Coverted to Woocommerce group it becomes wgroup:", groupdict[agroup])
print("Coverted to Woocommerce group it becomes wgroup:", groupdict[agroup])
if agroup in groupdict:
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:
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
q = (asifra,)
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...") #
# We test SqlLite rows so that we update the table only with product that changed
# We test SqlLite rows so that we update the table only with products that changed
trow = c.fetchone()
if trow is not None:
tnaziv = trow[3]
@ -121,31 +118,30 @@ for row in cursor:
# print("There is an SQLite trow we need to check for an update: "), tnaziv, tprice, tqty, tgroup
if aprice != tprice:
snc = 1
print(aname, "has a bad price")
elif aqty != tqty:
print("bad price")
if aqty != tqty:
snc = 1
print(aname, "has a bad qty")
elif aname != tnaziv:
print("bad qty")
if aname != tnaziv:
snc = 1
print(aname, "has bad naziv: ", tnaziv)
elif wgroup != tgroup:
print("bad naziv")
if wgroup != tgroup:
snc = 1
print(wgroup, "has a bad tgroup: ", tgroup)
else:
snc = 0
print(wgroup, "bad tgroup: ", tgroup)
if snc == 1:
print(asifra, aname, snc)
# print("=======")
c.execute('''UPDATE products SET aid = ?, price = ?, qty =?, naziv = ?,
snc = ?, grupa = ? WHERE sifra = ?''',
(aid, aprice, aqty, aname, snc, wgroup, asifra))
conn.commit()
c.execute('SELECT * FROM products WHERE sifra=?', q)
trow = c.fetchone()
# print(trow)
# print("=======")
print(trow)
print("=======")
# input("Press Enter to continue...")
else:
print("If sqlite trow does not exist (is ", trow, "), then first create one in Woo", aid, asifra, aname, aprice, aqty, wgroup)
print("If sqlite trow does not exist (is ", trow, "), then first create one in Woo", aid, asifra, aname,
aprice, aqty, wgroup)
# input("Press Enter to continue...") #
aprice = str(aprice)
data = {
@ -164,6 +160,8 @@ for row in cursor:
],
}
w = wcapi.post("products", data).json()
# w = wcapi.get("products/categories").json()
class ListStream:
@ -191,7 +189,7 @@ for row in cursor:
(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)
conn.commit()
# rgpath = "\"product_batch/" + str(rogueone) + "\""
# rgpath = "\"products/" + str(rogueone) + "\""
# print rgpath
# rg = wcapi.get(rogueone).json()
# print("The rogueone:", rg)
@ -213,45 +211,49 @@ for row in cursor:
# print("Resting for for a bit...")
# time.sleep(5)
conn.close()
print("Wait before sending")
# time.sleep(10)
# Update Woo with our SQLite table
conn = sqlite3.connect('imd.db') # intermediary db
c = conn.cursor()
c.execute('''SELECT * FROM products WHERE snc = 1 ORDER BY naziv''')
grupa = 0
# Make a product_list
product_list = list()
trow = c.fetchone()
print("Selected for update:", trow)
# cleantrow is not None:
grupa = 0
while trow is not None:
# count += 1
# print("Selected for update:", trow)
tid = trow[0]
id = trow[0]
tnaziv = trow[3]
tprice = str(trow[4])
tqty = trow[5]
tgroup = trow[7]
# product = [tid, tqty, tnaziv, tprice, tgroup]
product = {'id': tid, 'manage_stock': 'true', 'stock_quantity': tqty, 'name': tnaziv, 'status': 'publish', 'regular_price': tprice, 'categories': [{'id': tgroup}]}
# print(product)
product_list.append(product)
trow = c.fetchone()
print(product_list)
# Make batches of products and send them to WooCommerce
l = len(product_list)
print("Lenght: ", l)
batch_size = 100
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("_________________________BATCH", i, "_________________________")
response = wcapi.post("products/batch", data).json()
print(response)
# print type(id), type(tnaziv), type(tprice), type(tqty), "Grupa:", type(tgroup), tgroup
# input("Press enter ...")
data = {
"id": id,
"manage_stock": "true",
"stock_quantity": tqty,
"name": tnaziv,
"status": "publish",
"regular_price": tprice,
"categories": [
{
"id": tgroup
}
],
}
# print data
uplink = "products/" + str(id)
# print uplink
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')
conn.commit()

View File

@ -19,8 +19,7 @@ def initSQLite():
consumer_key="ck_b3169b1723f6f39a965ed04ecfa77860fb89bbf5",
consumer_secret="cs_a83f0217ed8d6191ab7de9df06a0c2b652f6bd57",
wp_api=True,
timeout=360,
version="wc/v3"
version="wc/v2"
)
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
@ -32,7 +31,7 @@ def initSQLite():
r = wcapi.get(goto_page)
page_txt = r.json() # contents of one page ar stored in variable
# We fill our SQLite table with Woo Products
for product in page_txt: # first we go through product in this page
for product in page_txt: # first we go through products in this page
wsku = product.get('sku')
wid = product.get('id')
wname = product.get('name')
@ -42,9 +41,9 @@ def initSQLite():
wqty = 0
# FIX THIS! - It does not update - just inserts if new ...
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 = ?',
("", wsku, wname, wprice, wqty, 0, wid))
("", wsku, wname, wprice, wqty, 0, wid, ""))
print("Insert woocommerce data to SQLlite:", wid, "", wsku, wname, wprice, wqty, 0)
conn.commit()

BIN
imd.db

Binary file not shown.