import json import os import jsonpath import newsetting as ten tent2 = ten.All_Setting(host='192.168.5.2', port=3307,db='mt_newretail') def get_path2(path): li = [] for a,b,c in os.walk(path): if len(c)>0: for d in c: if 'food' in d: li.append(os.path.join(a,d)) return li def choose_27(tags): for i in tags: type = i['type'] if type == 27: return True return False def rec_food(req,workid,business_code,mt_org_id,prox,lat,lng,parent_id): # ["data"]["ext"]["common_info"]["v9_data"]["food_spu_tags"][8]["tags"][0]["tag"] print(req) try: print('返回响应格式1') poi_info = req['data']['poi_info'] except: print('返回响应格式2') poi_info = req["data"]["ext"]["common_info"]["v9_data"]["poi_info"] wm_poi_id = poi_info['id'] poi_id_str = poi_info['poi_id_str'] shop_name = poi_info['name'] address = poi_info['address'] poi_coupon = poi_info.get('poi_coupon') discounts2 = poi_info['discounts2'] idd = poi_info['id'] discounts_list='' poi_id_str = poi_info['poi_id_str'] shipping_fee_tip = poi_info['shipping_fee_tip'] min_price_tip = poi_info['min_price_tip'] packet_fee_tip = poi_info.get('packing_fee') shipping_time = poi_info['shipping_time'] def get_coupon(): li = [] str_li = '' coupon_list = poi_coupon['coupon_list'] for i in coupon_list: coupon_id = i.get('coupon_id') coupon_name = i.get('coupon_name') new_limit_price = i.get('new_limit_price') coupon_type_label = i.get('coupon_type_label') tabName = i.get('tabName') product_num = i.get('product_num') label_doc = jsonpath.jsonpath(i, '$..label_doc') # da = (poi_id_str, coupon_id, coupon_name, new_limit_price, coupon_type_label, tabName, product_num, # label_doc) # li.append(da) str_li = str_li + f'{coupon_name}:{new_limit_price}:{coupon_type_label},' return str_li def get_discounts2(): li = [] str_li = '' for i in discounts2: id = i.get('id') info = i.get('info') # da = (poi_id_str, id, info) # li.append(da) str_li = str_li + f'{info},' return str_li coupon_list = '' discounts_list = '' if bool(poi_coupon): coupon_list = get_coupon() if bool(discounts2): discounts_list = get_discounts2() da = (workid ,parent_id,business_code, mt_org_id, poi_id_str,'', shop_name, shipping_fee_tip, min_price_tip, packet_fee_tip, shipping_time, address,coupon_list,discounts_list) insert_food_table = """ insert into food_table (workid ,parent_id,business_code, mt_org_id, poi_id_str,mt_org_name, shop_name, shipping_fee_tip, min_price_tip, packet_fee_tip, shipping_time, address,coupon_list,discounts_list) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s); """ tent2.write_data_one(insert_food_table, da) if bool(poi_coupon): coupon_list = get_coupon() insert_coupon = """ insert into coupon_table2 (workid, coupon_id, coupon_name,new_limit_price,coupon_type_label,tabName,product_num,label_doc) values (%s,%s,%s,%s,%s,%s,%s,%s) """ try: tent2.write_data(insert_coupon, coupon_list) except: pass # if bool(discounts2): # discounts_list = get_discounts2() # insert_discounts_list = """ # insert into discounts_table (workid, coupon_id, coupon_name) values (%s,%s,%s) # """ # tent2.write_data(insert_discounts_list, discounts_list) da3 = (prox,lat,lng,workid,parent_id,business_code,mt_org_id,poi_id_str,shop_name,address) need1 = ['tag','name','tags','type','has_next_page','product_count','allSortedSpuId'] try: print('返回响应格式1') da = req['data']['food_spu_tags'] except: print('返回响应格式2') da = req["data"]["ext"]["common_info"]["v9_data"]["food_spu_tags"] # try: for i in da: for b in list(i.keys()): if b not in need1: i.pop(b) categoryList = [] for i in da: tag1 = i['tag'] name1 = i['name'] tags = (i['tags']) tag_type = i.get('type') has_next_page = i.get('has_next_page') product_count = i.get('product_count') allSortedSpuId = json.dumps(i.get('allSortedSpuId')) have_27 = choose_27(tags) print(f'{name1}==>{have_27}') if len(tags) == 0: stype = 27 da2 = (tag1, name1, tag1, name1, stype, tag_type) print(f'len(tags) == 0 ==>{da2}') da4 = da3 + da2 + (None, has_next_page, product_count, allSortedSpuId) print(da4) print(len(da4)) categoryList.append(da4) if have_27: for b in tags: tag2 = b['tag'] name2 = b['name'] tag_type = b['type'] if tag_type == 27: stype = 27 else: stype = -1 da2 = (tag1, name1, tag2, name2, stype, tag_type) print(f'have27 = True ==>{da2}') da4 = da3 + da2 + (None, has_next_page, product_count, allSortedSpuId) print(da4) print(len(da4)) categoryList.append(da4) else: for b in tags: tag2 = b['tag'] name2 = b['name'] tag_type = b['type'] stype = 27 da2 = (tag1, name1, tag2, name2, stype, tag_type) print(f'have27 = False ==>{da2}') da4 = da3 + da2 + (None, has_next_page, product_count, allSortedSpuId) print(da4) print(len(da4)) categoryList.append(da4) # # if len(tags) > 0: # for b in tags: # tag2 = b['tag'] # name2 = b['name'] # tag_type = b['type'] # if tag_type == 27: # stype = 27 # else: # stype = -1 # da2 = (tag1,name1,tag2,name2,stype,tag_type) # da4 = da3+da2+(None,has_next_page,product_count,allSortedSpuId) # print(da4) # print(len(da4)) # categoryList.append(da4) # else: # stype = 27 # da2 = (tag1,name1,tag1,name1,stype,tag_type) # da4 = da3 + da2+(None,has_next_page,product_count,allSortedSpuId) # print(da4) # categoryList.append(da4) insertsql = """ insert into categoryList (prox_data,lat,lng,workid,parent_id,business_code,mt_org_id,poi_id_str,shop_name,address,tag1,tag1_name,tag2,tag2_name,stype,tag_type,is_ok,has_next_page,product_count,allSortedSpuId) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) """ tent2.write_data(insertsql,categoryList)