Exemplo n.º 1
0
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
Exemplo n.º 2
0
def crawler(keyword, page, count, cat_list=''):
    if cat_list and isinstance(cat_list, list):
        cat = ','.join(cat_list)
    else:
        cat = ''
    goods_list = _crawler(keyword=keyword, page=page, count=count, cat=cat)
    if goods_list is None:
        return []
    result = []
    for goods in goods_list:
        tmp = _ship_goods_supers(goods)
        if not tmp:
            continue
        tmp.update({'table': 'goods'})
        cat_obj = Category(id=tmp['category_id'], name=tmp['category_name'])
        cat_obj.save_category()
        if tmp.get("sub_category_id"):
            cat_obj = SubCategory(id=tmp['sub_category_id'],
                                  name=tmp.get('sub_category_name', ''),
                                  parent=tmp['category_id'])
            cat_obj.save_category()
        source = keyword if keyword else 'crawler'
        tmp.update({'source': source})
        goods_instance = TbkGoods(**tmp)
        if goods_instance.check_save():
            goods_info = goods_instance.find_goods_by_id()
            if not goods_info:
                similar_ids = crawler_similar(tmp['num_id'])
                goods_instance.similar_goods = similar_ids
                result.append(tmp)
            ret = goods_instance.save()
            searcher.update_index(tmp)
            LOG.debug(ret)
    return result
Exemplo n.º 3
0
def goods_detail(goods_id):
    goods = get_goods_info_by_id(goods_id)
    if not goods:
        goods_instance = TbkGoods(num_id=goods_id)
        goods = goods_instance.find_goods_by_id()
        if not goods:
            return False, u"找不到该商品"
    return True, _ship_db_goods(goods)
Exemplo n.º 4
0
def miniapp_goods_detail(gid, mid):
    res = {'errcode': -1}
    gid = int(gid)
    goods = get_goods_info_by_id(gid)
    if goods:
        data = _ship_miniapp_detail(goods)
        res.update({'errcode': 0, 'data': data})
        return res
    goods_obj = TbkGoods(num_id=gid)
    if mid and mid.isdigit():
        table = get_table_by_mid(mid)
        if table:
            goods_obj.__table__ = table
    goods = goods_obj.find_goods_by_id()
    if not goods:
        res.update({'errmsg': u"找不到商品"})
        return res
    data = _ship_miniapp_detail(goods)
    res.update({'errcode': 0, 'data': data})
    return res
Exemplo n.º 5
0
def similar_goods(goods_id):
    goods = get_goods_info_by_id(goods_id)
    if not goods:
        goods_instance = TbkGoods(num_id=goods_id)
        goods = goods_instance.find_goods_by_id()
        if not goods:
            return True, []
    similar_goods_ids = goods.get("similar_goods", [])
    goods_obj = TbkGoods()
    if similar_goods_ids:
        cond = {'num_id': {'$in': similar_goods_ids}, 'coupon_expire': 0}
    else:
        cond = {"coupon_expire": 0}
    goods_list = goods_obj.find_goods_by_cond(
            cond, 1, 100).sort([('sales', -1)]).limit(20)
    result = []
    for goods in goods_list:
        tmp = _ship_db_goods(goods)
        if tmp['is_tmall']:
            tmp['is_tmall'] = 'inline'
        else:
            tmp['is_tmall'] = 'none'
        result.append(tmp)
    return True, result
Exemplo n.º 6
0
def goods_tpw(goods_id):
    cache_key = goods_id
    data = get_cache_coupon(cache_key)
    if data:
        return {"errcode": 0, 'data': data}
    res = {'errcode': -1}
    goods = get_goods_info_by_id(goods_id)
    if not goods:
        goods_instance = TbkGoods(num_id=goods_id)
        goods = goods_instance.find_goods_by_id()
    if goods:
        msg = _ship_coupon_goods(goods)
        cache_coupon_info(cache_key, msg, 3600)
        res.update({'errcode': 0, 'data': msg})
    else:
        search_res = search_coupon_by_id(goods_id)
        if search_res.get("errcode") == 0:
            msg = search_res['data']
            cache_coupon_info(cache_key, msg, 3600)
            res.update({'errcode': 0, 'data': msg})
        else:
            res.update({'errmsg': u"找不到该商品"})
    # return succ, msg
    return res