def sync_yhd(db, step): goods = db.query( 'select gds.id,gds.status,gds.goods_link_id from goods_distributor_shop gds, distributor_shop ds ' 'where gds.distributor_shop_id=ds.id and gds.status <> "PREPARE" and gds.deleted=0 ' 'and gds.distributor_goods_id <> "" and ds.distributor_id=%s', options.distributor_id_yihaodian) goods_dict = dict([(g.goods_link_id, g) for g in goods]) goods_slice = [goods[i:i + step] for i in range(0, len(goods), step)] for slc in goods_slice: yhd = Yihaodian('yhd.general.products.search') response = yhd.sync_fetch( outerIdList=','.join(map(str, [s.goods_link_id for s in slc]))) yhd.parse_response(response) if not yhd.is_ok(): logging.error('sync goods status. yihaodian request error %s', response) continue for product in yhd.message.findall('productList/product'): g = goods_dict[int(product.findtext('outerId'))] can_sale = product.findtext('canSale') if can_sale == '0' and g.status != 'OFF_SALE': db.execute( 'update goods_distributor_shop set status="OFF_SALE", offsale_at=NOW(), onsale_at=NULL ' 'where distributor_shop_id=%s and goods_link_id=%s', options.shop_id_yihaodian, g.goods_link_id) logging.info('sync goods status. offsale yihaodian item %s', g.goods_link_id) elif can_sale == '1' and g.status != 'ON_SALE': db.execute( 'update goods_distributor_shop set status="ON_SALE", onsale_at=NOW(), offsale_at=NULL ' 'where distributor_shop_id=%s and goods_link_id=%s', options.shop_id_yihaodian, g.goods_link_id) logging.info('sync goods status. onsale yihaodian item %s', g.goods_link_id)
def fetch(db, step): link_id_list = db.query( 'select goods_link_id from goods_distributor_shop where distributor_shop_id=%s ' 'and distributor_goods_id="" and status <> "PREPARE" and deleted=0', options.shop_id_yihaodian) link_id_slice = [[link.goods_link_id for link in link_id_list[i:i + step]] for i in range(0, len(link_id_list), step)] for links in link_id_slice: yhd = Yihaodian('yhd.general.products.search') response = yhd.sync_fetch(outerIdList=','.join(map(str, links))) yhd.parse_response(response) if yhd.is_ok(): yhd_products = yhd.message.findall('productList/product') for yhd_product in yhd_products: outer_id = yhd_product.findtext('outerId') url = yhd_product.findtext('prodDetailUrl') goods_code = yhd_product.findtext('productCode') db.execute( 'update goods_distributor_shop set distributor_goods_id=%s, extra=%s ' 'where distributor_shop_id=%s and goods_link_id=%s', goods_code, url, options.shop_id_yihaodian, outer_id) else: logging.error('fetch yihaodian goods_id failed. %s %s', yhd.get_field('outerIdList'), response)