def process_item(self, item, spider): session = self.Session() exist_product = session.query(Product).filter( literal(item["product_code"]).like('%' + Product.code + '%')).first() if exist_product is not None: exist_product.price_tiki = item["price"] session.commit() raise DropItem("Update item: %s" % item["product_code"]) session.close() else: product = Product() product.name = item["product_name"] product.code = item["product_code"] # product.price_nkim = item["price"] product.price_tiki = item["price"] try: session.add(product) session.commit() except: session.rollback() raise finally: session.close()
def addProduct(): form = AddProductForm() makers = Maker.query.all() if makers: form.maker.choices = [(a.id, a.name) for a in makers] if form.validate_on_submit(): product = Product() product.code = form.code.data product.maker_id = form.maker.data product.maker_code = form.maker_code.data product.desc_CS = form.desc_CS.data product.desc_JP = form.desc_JP.data product.long_desc = form.long_desc.data product.detailed_desc = form.detailed_desc.data product.subcategory_desc = form.subcategory_desc.data product.keywords = form.keywords.data product.price_unit = form.price_unit.data product.price_retail = form.price_retail.data product.qty_stock = form.qty_stock.data product.limited_flg = form.limited_flg.data product.axm_node = form.axm_node.data if form.package_size.data == '': product.package_size = None else: product.package_size = form.package_size.data category_id = Maker.query.filter_by(id=product.maker_id).first().category_id if category_id: product.category_id = category_id db.session.add(product) db.session.commit() term_ids_str = request.form.getlist('term') for id in term_ids_str: t = CatalogedProducts() t.product_id = product.id t.catalog_id = int(id) db.session.add(t) db.session.commit() flash(gettext("New product successfully added.")) return redirect(url_for("editProduct", id=product.id)) catalog = prepare_catalog() return render_template("product/addProduct.html", title=gettext('Add new product'), catalog=catalog, form=form)
def process_item(self, item, spider): session = self.Session() product = Product() product.name = item["product_name"] product.code = item["product_code"] product.price_nkim = item["price"] #product.price_tiki = "0" try: session.add(product) session.commit() except: session.rollback() raise finally: session.close() return item
def check_product(self, product): logger.debug("product['code'] : %s", product["code"]) url = f"{AMAZON_BASE_PRODUCT_URL}{product['code']}" logger.debug("url : %s", url) page = BeautifulSoup(requests.get(url, headers=headers).content, "html.parser") tracked_product = Product() tracked_product.code = product["code"] tracked_product.url = url product_title_tag = page.find(id="productTitle") if product_title_tag is None: logger.debug("spam detected") time.sleep(15) # spam detected return tracked_product.title = product_title_tag.text.strip() if "selector" in product: count = ( product["selector"]["count"] if "count" in product["selector"] else 0 ) price_tag = page.select(product["selector"]["value"])[count] else: price_tag = page.find(id="priceblock_ourprice") if price_tag is not None: price = price_tag.text.strip() tracked_product.price = float( price[0 : price.rfind(" ") - 1].replace(",", ".") ) logger.debug("product.title : %s", tracked_product.title) logger.debug("product.price : %f", tracked_product.price) if "price" in product: logger.debug("checked price : %f", product["price"]) if tracked_product.price <= product["price"]: logger.debug( "price lower (%s) : %f -> %f", product["code"], product["price"], tracked_product.price, ) if self.enable_email: subject = format_string( self.email["subject"], tracked_product.title, str(tracked_product.price), url, ) body = format_string( self.email["body"], tracked_product.title, str(tracked_product.price), url, ) self.send_email(subject=subject, body=body) if self.enable_notification: self.send_notification_topic( "amazon_tracker", tracked_product.title, str(tracked_product.price), url, ) if "registration_token" in self.email: for token in email["registration_token"]: self.send_notification_device( token, tracked_product.title, str(tracked_product.price), url, ) # self.checked_products.append(product["code"]) elif "reduction" in product: if ( page.find("span", {"class": "priceBlockStrikePriceString"}) is not None ): print("price reductionin page") else: logger.debug("produce %s available", product["co"]) if self.enable_email: self.send_email( product["code"], title=tracked_product.title, url=url ) if self.enable_notification: self.send_notification_topic( "amazon_tracker", tracked_product.title, "Is available", url )