Commit 8c69746b by 岳巧源

add pymysql

parent 422acdad
Showing with 89 additions and 20 deletions
...@@ -17,6 +17,13 @@ ...@@ -17,6 +17,13 @@
"port": 2111, "port": 2111,
"password": "rs2024Sl00293sOoj", "password": "rs2024Sl00293sOoj",
"username": "rs" "username": "rs"
},
"mysql": {
"host": "192.168.1.238",
"user": "europa_erp",
"password": "europa_erp#zsyM",
"port": 3306,
"database": "europa_erp"
} }
}, },
"prod": { "prod": {
...@@ -37,6 +44,13 @@ ...@@ -37,6 +44,13 @@
"port": 2111, "port": 2111,
"password": "rs2024Sl00293sOoj", "password": "rs2024Sl00293sOoj",
"username": "rs" "username": "rs"
},
"mysql": {
"host": "erp.chiplinks.db",
"user": "antXST",
"password": "Antxs@#$paf2iOjsft",
"port": 3306,
"database": "europa_erp"
} }
}, },
"local": { "local": {
...@@ -57,6 +71,13 @@ ...@@ -57,6 +71,13 @@
"port": 2111, "port": 2111,
"password": "rs2024Sl00293sOoj", "password": "rs2024Sl00293sOoj",
"username": "rs" "username": "rs"
},
"mysql": {
"host": "192.168.1.238",
"user": "europa_erp",
"password": "europa_erp#zsyM",
"port": 3306,
"database": "europa_erp"
} }
} }
} }
\ No newline at end of file
...@@ -2,20 +2,12 @@ import csv ...@@ -2,20 +2,12 @@ import csv
import json import json
import os.path import os.path
import sys import sys
import pymysql
import config import config
from utils import ftp from utils import ftp
from utils import file_handle from utils import file_handle
from utils import rabbit_mq
global_csv_title = ['DistributorPartNumber', 'ManufacturerPartNumber', 'Manufacturer', 'Description', 'ROHS', 'ProductUrl',
'QuantityAvailable', 'PriceQuantity', 'IMAGE_URL', 'PRODUCT_TYPE',
'BASE_UOM', 'SALES_UOM', 'PACK_SIZE', 'HKD_1_Price', 'HKD_2_Price', 'HKD_3_Price', 'HKD_4_Price', 'HKD_5_Price',
'ATTRIBUTE_1_NAME', 'ATTRIBUTE_2_NAME', 'ATTRIBUTE_3_NAME',
'ATTRIBUTE_4_NAME', 'ATTRIBUTE_5_NAME', 'ATTRIBUTE_6_NAME', 'ATTRIBUTE_7_NAME', 'ATTRIBUTE_8_NAME', 'ATTRIBUTE_9_NAME',
'ATTRIBUTE_10_NAME', 'ATTRIBUTE_11_NAME', 'ATTRIBUTE_12_NAME',
'ATTRIBUTE_13_NAME', 'ATTRIBUTE_14_NAME', 'ATTRIBUTE_15_NAME', 'ATTRIBUTE_1_VALUE', 'ATTRIBUTE_2_VALUE', 'ATTRIBUTE_3_VALUE', 'ATTRIBUTE_4_VALUE',
'ATTRIBUTE_5_VALUE', 'ATTRIBUTE_6_VALUE', 'ATTRIBUTE_7_VALUE', 'ATTRIBUTE_8_VALUE', 'ATTRIBUTE_9_VALUE', 'ATTRIBUTE_10_VALUE', 'ATTRIBUTE_11_VALUE',
'ATTRIBUTE_12_VALUE', 'ATTRIBUTE_13_VALUE', 'ATTRIBUTE_14_VALUE', 'ATTRIBUTE_15_VALUE', 'STOCK_QTY_GB', 'SHIPPING_WEIGHT']
...@@ -35,14 +27,25 @@ conf = config.load_config_map() ...@@ -35,14 +27,25 @@ conf = config.load_config_map()
def handle_hk_csv(file_name): def handle_hk_csv(file_name, conf: dict):
"""handle the HK_cn file""" """handle the HK_cn file"""
p = rabbit_mq.Producer(
user=conf["rabbit_mq"]["producer"]["user"],
password=conf["rabbit_mq"]["producer"]["password"],
host=conf["rabbit_mq"]["producer"]["host"],
port=conf["rabbit_mq"]["producer"]["port"],
queue_name=conf["rabbit_mq"]["producer"]["queue_name"],
durable=True,
exchange=conf["rabbit_mq"]["producer"]["rabbit_mq_exchange"],
routing_key=conf["rabbit_mq"]["producer"]["rabbit_mq_routing_key"]
)
with open(file_name, errors='ignore') as f: with open(file_name, errors='ignore') as f:
reader = csv.reader(f) reader = csv.reader(f)
title = [] title = []
for index, row in enumerate(reader): for index, row in enumerate(reader):
# TODO remember to delete. # TODO remember to delete.
if index > 10: if index > 1:
break break
if index == 0: if index == 0:
title = row title = row
...@@ -88,19 +91,31 @@ def handle_hk_csv(file_name): ...@@ -88,19 +91,31 @@ def handle_hk_csv(file_name):
message_map["ladder_price"] = ladder_price message_map["ladder_price"] = ladder_price
message_map["price_is_us"] = True message_map["price_is_us"] = True
json_str = json.dumps(message_map) json_str = json.dumps(message_map)
print(json_str) p.push(json_str, exchange=conf["rabbit_mq"]["producer"]["rabbit_mq_exchange"],
routing_key=conf["rabbit_mq"]["producer"]["rabbit_mq_routing_key"])
p.close()
def handle_cn_csv(file_name): def handle_cn_csv(file_name, conf: dict):
"""handle the RMB file""" """handle the RMB file"""
p = rabbit_mq.Producer(
user=conf["rabbit_mq"]["producer"]["user"],
password=conf["rabbit_mq"]["producer"]["password"],
host=conf["rabbit_mq"]["producer"]["host"],
port=conf["rabbit_mq"]["producer"]["port"],
queue_name=conf["rabbit_mq"]["producer"]["queue_name"],
durable=True,
exchange=conf["rabbit_mq"]["producer"]["rabbit_mq_exchange"],
routing_key=conf["rabbit_mq"]["producer"]["rabbit_mq_routing_key"]
)
with open(file_name, mode='r', errors='ignore') as f: with open(file_name, mode='r', errors='ignore') as f:
reader = f.readlines() reader = f.readlines()
title = [] title = []
for j in range(len(reader)): for j in range(len(reader)):
# TODO remember to delete this code. # TODO remember to delete this code.
if j > 10: if j > 1:
break break
data = reader[j] data = reader[j]
data = data.replace("\n", '') data = data.replace("\n", '')
row = data.split(sep='\t') row = data.split(sep='\t')
...@@ -146,7 +161,36 @@ def handle_cn_csv(file_name): ...@@ -146,7 +161,36 @@ def handle_cn_csv(file_name):
message_map["ladder_price"] = ladder_price message_map["ladder_price"] = ladder_price
message_map["price_is_us"] = False message_map["price_is_us"] = False
json_str = json.dumps(message_map) json_str = json.dumps(message_map)
print(json_str) p.push(json_str, exchange=conf["rabbit_mq"]["producer"]["rabbit_mq_exchange"],
routing_key=conf["rabbit_mq"]["producer"]["rabbit_mq_routing_key"])
p.close()
def remove(path):
if os.path.exists(path):
os.remove(path)
def get_supplier_id(supplier_name: str, conf: dict) -> int:
db = pymysql.connect(host=conf["mysql"]["host"],
user=conf["mysql"]["user"],
password=conf["mysql"]["password"],
database=conf["mysql"]["database"],
port=conf["mysql"]["port"]
)
cursor = db.cursor()
query = "select * from erp_supplier where supplier_name = %s"
param = (supplier_name,)
supplier_id = -1
try:
cursor.execute(query, args=param)
results = cursor.fetchall()
row = results[0]
supplier_id = int(row[0])
except Exception as e:
pass
finally:
db.close()
return supplier_id
...@@ -177,12 +221,15 @@ if __name__ == '__main__': ...@@ -177,12 +221,15 @@ if __name__ == '__main__':
# file_handle.unzip(conf_environment["tmp_file_path"] + local_file_name_3, extract_dir) # file_handle.unzip(conf_environment["tmp_file_path"] + local_file_name_3, extract_dir)
"""handle the file and then post to rabbitmq""" """handle the file and then post to rabbitmq"""
# handle_hk_csv(extract_dir + handle_file_name_1) # handle_hk_csv(extract_dir + handle_file_name_1, conf_environment)
handle_cn_csv(extract_dir + handle_file_name_3) handle_cn_csv(extract_dir + handle_file_name_3, conf_environment)
"""remove the temp file, close the connection""" """remove the temp file, close the connection"""
# remove(extract_dir + handle_file_name_1)
# remove(extract_dir + handle_file_name_3)
# remove(conf_environment["tmp_file_path"] + local_file_name_1)
# remove(conf_environment["tmp_file_path"] + local_file_name_3)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment