Commit a8f1faf6 by lzzzzl

更新attr_values mongo处理

parent f7773e9a
Showing with 32 additions and 22 deletions
......@@ -4,11 +4,13 @@ import traceback
import pymongo
from urllib import parse
from utils.mysql_handler import MysqlHandler
from fun.param_thread import ParamThread
# 转义用户名和密码
user = parse.quote_plus("ichunt")
# pwd = parse.quote_plus("huntmon6699")
pwd = parse.quote_plus("huntmon66499")
pwd = parse.quote_plus("huntmon6699")
# pwd = parse.quote_plus("huntmon66499")
myclient = pymongo.MongoClient("mongodb://{0}:{1}@192.168.1.237:27017/?authSource=ichunt&authMechanism=SCRAM-SHA-1".format(user, pwd))
# myclient = pymongo.MongoClient("mongodb://{0}:{1}@172.18.137.23:27017/?authSource=ichunt&authMechanism=SCRAM-SHA-1".format(user, pwd))
......@@ -20,6 +22,15 @@ class ParamMongo(object):
def __init__(self):
self.mysql_handler = MysqlHandler('liexin_spu')
self.data_dict = {0: [], 1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [], 9: []}
"""
初始化数据
"""
def init_data(self):
# 遍历SKU数据
for i in range(0, 10):
self.data_dict[i] = self.mysql_handler.get_spu(i)
def search_mongo(self, spu_id, data):
res = mycol.find({"spu_id": spu_id}).limit(1)
......@@ -32,29 +43,28 @@ class ParamMongo(object):
mg_dt['attrs_extend'] = data
mycol.save(mg_dt)
def update_mongo(self, data):
for row in data:
try:
spu_id = row[0]
attr_values = json.loads(row[1])
new_av_list = list()
for vals in attr_values:
val = str(vals).split(",")
attr_name = self.mysql_handler.get_attr_name(val[0])
attr_value = self.mysql_handler.get_attr_value(val[1])
attr_unit = self.mysql_handler.get_attr_unit(val[2])
if attr_name and attr_value and attr_unit:
new_av_list.append({'attr_name': attr_name, 'attr_value': attr_value, 'attr_unit': attr_unit})
self.search_mongo(spu_id, new_av_list)
except:
traceback.print_exc()
def run(self):
for index in range(0, 10):
data = self.mysql_handler.get_spu(index)
for row in data:
try:
spu_id = row[0]
attr_values = json.loads(row[1])
new_av_list = list()
for vals in attr_values:
val = str(vals).split(",")
attr_name = self.mysql_handler.get_attr_name(val[0])
attr_value = self.mysql_handler.get_attr_value(val[1])
attr_unit = self.mysql_handler.get_attr_unit(val[2])
if attr_name and attr_value and attr_unit:
new_av_list.append({'attr_name': attr_name, 'attr_value': attr_value, 'attr_unit': attr_unit})
self.search_mongo(spu_id, new_av_list)
except:
traceback.print_exc()
self.init_data()
ParamThread.thread_func_ten(self.update_mongo, self.data_dict)
if __name__ == '__main__':
param_mongo = ParamMongo()
param_mongo.run()
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