def crawler_similar(goods_id): res = client.tbk_goods_recommend(goods_id) or {} response = res['tbk_item_recommend_get_response'] if response.get("results") is None: return goods_list = response['results'].get('n_tbk_item', []) similar_ids = [] for goods in goods_list: num_iid = goods['num_iid'] title = goods['title'] similar_goods = _search_by_id(num_iid, title) if similar_goods is None: continue similar_ids.append(num_iid) similar_goods.update({'source': 'similar'}) goods_instance = TbkGoods(**similar_goods) goods_instance.save() loop_ids = copy.deepcopy(similar_ids) for num_iid in loop_ids: goods_instance = TbkGoods(num_id=num_iid) goods_info = goods_instance.find_goods_by_id() if goods_info: ori_similar_ids = goods_info.get("similar_goods", []) if ori_similar_ids is not None: similar_ids.extend(ori_similar_ids) goods_instance.update({'similar_goods': list(set(similar_ids))}) similar_ids = copy.deepcopy(loop_ids) return similar_ids
def get_send_goods(cat_id=None): goods = get_one_goods(cat_id) if not goods: return title = goods['title'] price = goods['price'] coupon_amount = goods['coupon_amount'] coupon_url = goods['coupon_share_url'] pic_url = goods['pic_url'] tpw = generate_tpw(coupon_url, title, pic_url) goods_obj = TbkGoods(num_id=goods['num_id']) goods_obj.update({"sended": 1}) if not tpw: return new_price = round(float(float(price) - coupon_amount), 2) commssion_rate = goods['commssion_rate'] commssion_fee = round(new_price * float(commssion_rate) * 0.2, 2) msg = TEXT_MSG % (title, price, coupon_amount, new_price, commssion_fee, tpw) return {'pic_url': pic_url, 'msg': msg, 'goods': goods}
def update_similar(): page = 6 count = 100 have_data = True goods_obj = TbkGoods() while have_data: have_data = False goods_list = goods_obj.find_goods_by_cond({}, page, count) for goods in goods_list: have_data = True if goods.get('similar_goods'): continue _id = goods['num_id'] similar_ids = crawler_similar(_id) if similar_ids is None: continue goods_instance = TbkGoods(num_id=_id) goods_instance.update({'similar_goods': similar_ids}) page += 1 print page