def handle(self, brand_name, *args, **options): competitors = Competitor.objects.filter(brand__name=brand_name) brand = Brand.objects.get(name=brand_name) all_products = Product.objects.filter(brand=brand).select_related() for competitor in competitors: print 'now scraping ' + str(competitor.name.capitalize()) scraper = eval(str(competitor.name.capitalize()) + 'Scraper()') for product in all_products: try: existing_result = Result.objects.get(product=product, competitor=competitor) print 'already zere' continue except: sleep(1) price = scraper.get_price(product.part_number) self.stdout.write('%s with price %s' % (product.part_number, price)) if price != 0: result = Result() result.product = product result.competitor = competitor result.price = price if product.mro_price < price: result.is_cheaper = False else: result.is_cheaper = True product.is_cheaper = True result.save() print 'inserted new product' self.stdout.write('Successfully finished')
def handle(self, brand_name, *args, **options): competitors = Competitor.objects.filter(brand__name=brand_name) brand = Brand.objects.get(name=brand_name) all_products = Product.objects.filter(brand=brand).select_related() for competitor in competitors: print "now scraping " + str(competitor.name.capitalize()) scraper = eval(str(competitor.name.capitalize()) + "Scraper()") for product in all_products: sleep(1) price = scraper.get_price(product.part_number) self.stdout.write("%s with price %s" % (product.part_number, price)) try: existing_result = Result.objects.get(product=product, competitor=competitor) archive = Archive( product=product, competitor=competitor, price=existing_result.price, scraped=existing_result.scraped, ) if price == existing_result.price: exiting_result.changed = False existing_result.save() print "price not changed" continue existing_result.price = price existing_result.changed = True if product.mro_price < price: existing_result.is_cheaper = False else: existing_result.is_cheaper = True product.is_cheaper = True existing_result.save() print "succesfully updated product and archive" except: if price != 0: result = Result() result.product = product result.competitor = competitor result.price = price if product.mro_price < price: result.is_cheaper = False else: result.is_cheaper = True product.is_cheaper = True result.save() print "inserted new product" self.stdout.write("Successfully finished")