Skip to content

Instantly share code, notes, and snippets.

@aniav
Last active January 25, 2021 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aniav/1178443 to your computer and use it in GitHub Desktop.
Save aniav/1178443 to your computer and use it in GitHub Desktop.
My first python code ever written.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from suds.client import Client
import pymssql
import MySQLdb
import sys
from time import strftime, sleep
import re
import htmlentitydefs
import gc
global lastObjectsDict
conn = pymssql.connect(host="test", user='test',
password='test', database='test')
cur = conn.cursor()
mysqldb = MySQLdb.connect(host="test", user="test", passwd="test", db="")
mysqlcur = mysqldb.cursor()
url = "http://i-serwis2.action.pl/ADE8/DataExchange.asmx?WSDL"
params = {'CustomerID':'00000', 'UserName':'test', 'UserPassword':'test'}
class MyImporterAction:
def _unescape(self,text):
def fixup(m):
text = m.group(0)
if text[:2] == "&#":
# character reference
try:
if text[:3] == "&#x":
return unichr(int(text[3:-1], 16))
else:
return unichr(int(text[2:-1]))
except ValueError:
pass
else:
# named entity
try:
if text[1:-1] == "amp":
text = " & "
elif text[1:-1] == "gt":
text = " > "
elif text[1:-1] == "lt":
text = " < "
else:
text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
except KeyError:
pass
return text # leave as is
text = re.sub("&#?\w+;", fixup, text)
text = re.sub("(?<=\S)\/(?<=\S)"," / ",text)
text = re.sub("(?<=\S)\+(?<=\S)"," + ",text)
text = re.sub("(?<=\S)\-(?<=\S)"," - ",text)
text = re.sub("(?<=\S)\_(?<=\S)"," _ ",text)
text = text.encode('utf-8').replace("'","''")
return " ".join(text.split())
def products_get(self):
# reset updated status for all products
cur.execute("UPDATE action_products SET updated = 0 WHERE 1=1")
conn.commit()
# get products and perform UPDATES/INSERTS
client = Client(url, cache=None)
for product in client.service.Products_Get(**params)[0]:
cur.execute("SELECT count(0) as count, 'price' FROM \
action_products WHERE productID = '%s'" % product['productID'])
isproduct = cur.fetchone()
try:
product['deliveryDate'] = product['deliveryDate'].strftime("%Y-%m-%d %H:%M:%S")
except(ValueError):
product['deliveryDate'] = '1990-01-01 00:00:00'
if(product['productName'] is None):
product['productName'] = " "
if(product['warranty']['warrantyType'] is None):
product['warranty']['warrantyType'] = " "
if(product['productManager'] is None):
product['productManager'] = " "
if(isproduct[0] > 0):
#print "UPDATE %s" % product['productID']
cur.execute(u"UPDATE action_products SET productName='%s', \
producerID='%s', groupID='%s', subGroupID='%s', large=%f, \
warrantyType='%s', warrantyLength=%d, quantity=%d, price=%f, onOrder='%s', \
specialOffer='%s', showDate='%s', modificationDate='%s', detalPrice=%f, \
deliveryDate='%s', productManager='%s', productStore='%s', warehouseBlocked='%s', \
updated=1 where productID = '%s'".encode('utf-8') % (
self._unescape(product['productName']),
product['producerID'].encode('utf-8'),
product['groupID'].encode('utf-8'),
product['subGroupID'].encode('utf-8'),
product['large'],
product['warranty']['warrantyType'].encode('utf-8'),
product['warranty']['warrantyLength'],
product['quantity'],
product['price'],
product['onOrder'],
product['specialOffer'],
product['showDate'].strftime("%Y-%m-%d %H:%M:%S"),
product['modificationDate'].strftime("%Y-%m-%d %H:%M:%S"),
product['detalPrice'],
product['deliveryDate'],
product['productManager'].encode('utf-8').replace("'","''"),
product['productStore'].encode('utf-8'),
product['warehouseBlocked'],
product['productID'].encode('utf-8')))
else:
#print "INSERT %s" % product['productID']
cur.execute(u"INSERT INTO action_products \
(productID, productName, producerID, groupID, subGroupID, large, \
warrantyType, warrantyLength, quantity, price, onOrder, specialOffer, \
showDate, modificationDate, detalPrice, deliveryDate, productManager, \
productStore, warehouseBlocked, updated) \
VALUES('%s', '%s', '%s', '%s', '%s', %f, '%s', %d, %d, %f, '%s', \
'%s', '%s', '%s', %f, '%s', '%s', '%s', '%s', 1)".encode('utf-8') % \
(product['productID'].encode('utf-8'),
self._unescape(product['productName']),
product['producerID'].encode('utf-8'),
product['groupID'].encode('utf-8'),
product['subGroupID'].encode('utf-8'),
product['large'],
product['warranty']['warrantyType'].encode('utf-8'),
product['warranty']['warrantyLength'],
product['quantity'],
product['price'],
product['onOrder'],
product['specialOffer'],
product['showDate'].strftime("%Y-%m-%d %H:%M:%S"),
product['modificationDate'].strftime("%Y-%m-%d %H:%M:%S"),
product['detalPrice'],
product['deliveryDate'],
product['productManager'].encode('utf-8').replace("'","''"),
product['productStore'].encode('utf-8'),
product['warehouseBlocked']))
if(isproduct[1] != product['price']):
cur.execute("INSERT INTO action_products_price_changes (productID, price, \
detalPrice, modificationDate) VALUES ('%s', %f, %f, '%s')" % \
(product['productID'].encode('utf-8'), product['price'],
product['detalPrice'], product['modificationDate'].strftime("%Y-%m-%d %H:%M:%S")))
conn.commit()
del isproduct
del product
del client
# update quantity to 0 for nonupdated elements
cur.execute("SELECT productID FROM action_products WHERE updated = 0")
rows = cur.fetchall()
for row in rows:
cur.execute("UPDATE action_products SET quantity=0 where productID = '%s'" % row[0])
conn.commit()
del rows
def product_groups_get(self):
# get all the groups
client = Client(url, cache=None)
for group in client.service.Product_Groups_Get(**params)[0]:
#print group['groupID']
cur.execute("SELECT count(0) as count FROM action_products_groups WHERE groupID = '%s' AND subGroupID='%s'"
% (group['groupID'], group['subGroupID']))
isgroup = cur.fetchone()
if(isgroup[0] > 0):
#print "UPDATE %s : %s" % (group['subGroupName'].encode('utf-8'), group['subGroupID'].encode('utf-8'))
cur.execute("UPDATE action_products_groups SET groupName = '%s', subGroupName = '%s' \
WHERE groupID = '%s' AND subGroupID = '%s'" % (group['groupName'].encode('utf-8'),
group['subGroupName'].encode('utf-8'),group['groupID'].encode('utf-8'),
group['subGroupID'].encode('utf-8')))
else:
#print "INSERT %s \n" % group['groupID']
cur.execute("INSERT INTO action_products_groups VALUES('%s','%s','%s','%s')" %
(group['groupID'].encode('utf-8'), group['groupName'].encode('utf-8'),
group['subGroupID'].encode('utf-8'), group['subGroupName'].encode('utf-8')))
conn.commit()
def product_producers_get(self):
# get producers
client = Client(url, cache=None)
for producer in client.service.Product_Producers_Get(**params)[0]:
if(producer['description'] is None):
producer['description'] = " "
cur.execute("SELECT count(0) as count FROM action_products_producers \
WHERE producerID = '%s'" % producer['producerID'].encode('utf-8'))
isproducer = cur.fetchone()
if(isproducer[0] > 0):
#print "UPDATE %s" % producer['producerID'].encode('utf-8')
cur.execute("UPDATE action_products_producers SET name = '%s', \
description = '%s' WHERE producerID = '%s'" %
(producer['name'].encode('utf-8'),
producer['description'].encode('utf-8'),
producer['producerID'].encode('utf-8')))
else:
#print "INSERT %s \n" % producer['producerID'].encode('utf-8')
cur.execute("INSERT INTO action_products_producers VALUES('%s','%s','%s')" %
(producer['producerID'].encode('utf-8'),
producer['name'].encode('utf-8'),
producer['description'].encode('utf-8')))
conn.commit()
def products_get_debug(self):
# get products and perform UPDATES/INSERTS
objects = gc.get_objects()
client = Client(url, cache=None)
products = client.service.Products_Get(**params)[0]
client.__dict__.clear()
del products, client
gc.collect()
while 1:
try:
sia = MyImporterAction()
#sia.products_get_debug()
#sia.__dict__.clear()
#print ("done")
#sleep(3600)
#sys.exit()
sia.products_get()
print ("products done")
#del sia
sia.product_groups_get()
print "products groups done"
sia.product_producers_get()
print "products producers get"
mysqlcur.execute("UPDATE other.nadzorca_daemonow \
SET czas_aktualizacji = NOW() WHERE id=18")
mysqldb.commit()
print ("Success")
except:
print "Unexpected error:", sys.exc_info()[0]
sleep(3600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment