def handle(self, *args, **options):
        brand = Brand.objects.get(name='Dodge')
        competitor = Competitor.objects.get(name='kscdirect')
        # scraper = KscdirectScraper()
        # scraper.scrap()
        with open('/home5/shopmroc/utilities/prices/management/commands/kscdirect_results.csv', 'rb') as f:
            reader = csv.reader(f, dialect='excel')
            for row in reader:
                print row
                price = row[2]
                if price == 0:
                    continue
                try:
                    product = Product.objects.get(part_number=row[1])
                except:
                    print 'no product'
                    continue

                try:
                    existing_result = Result.objects.get(product=product, competitor=competitor)
                    archive = Archive(product=existing_result.product, competitor=competitor, price=existing_result.price, scraped=existing_result.scraped)
                    archive.save()
                    print price, existing_result.price
                    existing_result.scraped = date.today()
                    if float(price) == float(existing_result.price):
                        existing_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
                        existing_result.product.is_cheaper = True
                    existing_result.save()
                    print 'succesfully updated product and archive'
                except:
                    result = Result(product=product, competitor=competitor)
                    result.price = price
                    result.changed = False
                    if product.mro_price < price:
                        result.is_cheaper = False
                    else:
                        result.is_cheaper = True
                        result.product.is_cheaper = True
                    result.save()
                    print 'succesfully inserted new result'
Esempio n. 2
0
def scrap_competitor(*args, **kwargs):
    try:
        competitor_name = args[0]
        brand_name = args[1]
    except:
        pass
    brand = Brand.objects.get(name=brand_name)
    competitor = Competitor.objects.get(name=competitor_name)
    if competitor_name.capitalize() == 'Plccenter':
        return
    if competitor_name.capitalize() == 'Kscdirect':
        return
    else:
        #scraper = eval(str(competitor.name.capitalize()) + 'Scraper()')
        scraper = scrapers[competitor.name.capitalize()]
    
    
    previous_results = Result.objects.filter(product__brand=brand, competitor=competitor).exclude(scraped=today).prefetch_related('product')
    for existing_result in previous_results:
        print existing_result.product
        price = scraper.get_price(existing_result.product.part_number)
        if price == 0:
            continue
        print '%s with price %s' % (existing_result.product.part_number, price)

        archive = Archive(product=existing_result.product, competitor=competitor, price=existing_result.price, scraped=existing_result.scraped)
        archive.save()
        print price, existing_result.price
        existing_result.scraped = datetime.date.today()
        if float(price) == float(existing_result.price):
            existing_result.changed = False
            existing_result.save()
            print 'price not changed'
            continue

        existing_result.price = price
        existing_result.changed = True
        if float(existing_result.product.mro_price) < float(price):
            existing_result.is_cheaper = False
        else:
            existing_result.is_cheaper = True
            existing_result.product.is_cheaper = True
        existing_result.save()
        print 'succesfully updated product and archive'
    def handle(self, competitor_name, brand_name, *args, **options):
        brand = Brand.objects.get(name=brand_name)
        competitor = Competitor.objects.get(name=competitor_name)
        
        if competitor_name.capitalize() == 'Kscdirect':
            return
        if competitor_name.capitalize() == 'Plccenter':
            scraper = PlccenterScraper(brand_name.capitalize())
        else:
            scraper = eval(str(competitor.name.capitalize()) + 'Scraper()')

        previous_results = Result.objects.filter(product__brand=brand, competitor=competitor).exclude(scraped=yesterday).exclude(scraped=today).prefetch_related('product')
        for existing_result in previous_results:
            print existing_result.product
            price = scraper.get_price(existing_result.product.part_number)
            if price == 0:
                continue
            self.stdout.write('%s with price %s' % (existing_result.product.part_number, price))

            archive = Archive(product=existing_result.product, competitor=competitor, price=existing_result.price, scraped=existing_result.scraped)
            archive.save()
            print price, existing_result.price
            existing_result.scraped = date.today()
            if float(price) == float(existing_result.price):
                existing_result.changed = False
                existing_result.save()
                print 'price not changed'
                continue

            existing_result.price = price
            existing_result.changed = True
            if float(existing_result.product.mro_price) < float(price):
                existing_result.is_cheaper = False
            else:
                existing_result.is_cheaper = True
                existing_result.product.is_cheaper = True
            existing_result.save()
            print 'succesfully updated product and archive'