Advertisement
Guest User

Untitled

a guest
May 4th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1.     def parse(self, response):
  2.         parser = SamsParser(content=response, logger=self.log)
  3.         try:
  4.             parser.in_stock_online
  5.         except (ParsingException, SearchException):
  6.             item = self.item
  7.             item['url'] = response.url
  8.             yield item
  9.         except SearchPageError:
  10.             item = self.item
  11.             item['url'] = response.url
  12.             item['status'] = 'check'
  13.             yield item
  14.         else:
  15.             parent = {}
  16.             defaults = {
  17.                 'item_name': '',
  18.                 'item_number': '',
  19.                 'product_id': 'xxxxxx',
  20.                 'sku': '',
  21.                 'upc': '',
  22.                 'in_stock_online': False,
  23.                 'in_stock_instore': False,
  24.                 'price': 0.0,
  25.                 'shipping': 0.0,
  26.                 'surcharge': 0.0,
  27.                 'discount': 0.0,
  28.                 'sale_limit': 0,
  29.                 'sale_end_date': datetime(2001, 01, 01),
  30.                 'original_package': None,
  31.                 'variant_name': '0'
  32.             }
  33.             parent.update(defaults)
  34.             parent['item_number'] = self.item_number
  35.             parse_keys = [
  36.                 'product_id',
  37.                 'item_number',
  38.                 'sku',
  39.                 'in_stock_online',
  40.                 'price',
  41.                 'discount'
  42.             ]
  43.             for key in parse_keys:
  44.                 try:
  45.                     if hasattr(parser, key):
  46.                         parent[key] = getattr(parser, key)
  47.                     if not parent[key]:
  48.                         parent[key] = defaults[key]
  49.                 except (ParsingException, SearchException, SearchPageError):
  50.                     parent[key] = defaults[key]
  51.             for child in parser.variants(parent):
  52.                 item = self.item
  53.                 item['url'] = response.url
  54.                 item['sku'] = child['sku']
  55.                 item['product_id'] = child['product_id']
  56.                 item['price'] = child['price'] if not child['discount'] else child['price'] - child['discount']
  57.                 item['shipping_surcharge'] = child['surcharge']
  58.                 item['status'] = 'In Stock' if child['in_stock_online'] else 'Out Of Stock'
  59.                 item['option'] = BaseParser.format_variants(child['variant_name'], 15)
  60.                 item['original_packaging'] = None
  61.                 url = 'http://www.samsclub.com/sams/shop/product/moneybox/shippingDeliveryInfo.jsp?'
  62.                 params = 'zipCode=10023&productId=%s&skuId=%s&status=inStock&isSelectedZip=false&isLoggedIn=false'
  63.                 url += params % (item['product_id'], item['sku'])
  64.                 if item['status'] == 'In Stock':
  65.                     meta = {'item': item, 'cookiejar': response.meta['cookiejar'] + item['option']}
  66.                     yield Request(url, callback=self.parse_shipping, meta=meta)
  67.                 else:
  68.                     item['shipping'] = 0.0
  69.                     yield item
  70.  
  71.     def parse_shipping(self, response):
  72.         parser = SamsParser(content=response, logger=self.log)
  73.         item = response.meta['item']
  74.         item['shipping'] = parser.shipping
  75.         if item['shipping'] == 'check':
  76.             item['shipping'] = 0.0
  77.             item['status'] = 'check'
  78.         return [item]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement