Ejemplo n.º 1
0
 def add_new_product_to_offers(self, new_product):
     new_offer = Offer.from_product(new_product)
     new_offer.price += settings['maxPriceMargin']
     new_offer.shipping_time = {
         'standard': settings['shipping'],
         'prime': settings['primeShipping']
     }
     new_offer.prime = True
     # self.products[new_product.uid] = new_product
     new_offer.offer_id = self.marketplace_api.add_offer(new_offer).offer_id
     self.offers[new_product.uid] = new_offer
Ejemplo n.º 2
0
 def create_new_offer(self, offers: List[Offer], product: Product,
                      product_prices_by_uid: dict):
     offer = Offer.from_product(product)
     offer.prime = True
     offer.shipping_time['standard'] = self.settings["shipping"]
     offer.shipping_time['prime'] = self.settings["primeShipping"]
     offer.merchant_id = self.merchant_id
     offer.price = self.calculate_optimal_price(product_prices_by_uid,
                                                offer,
                                                product.uid,
                                                current_offers=offers +
                                                [offer])
     self.api.add_offer(offer)
Ejemplo n.º 3
0
 def add_new_product_to_offers(self, new_product):
     new_offer = Offer.from_product(new_product)
     new_offer.price += settings['maxPriceMargin']
     new_offer.shipping_time = {
         'standard': settings['shipping'],
         'prime': settings['primeShipping']
     }
     new_offer.prime = True
     self.products[new_product.uid] = new_product
     try:
         new_offer.offer_id = self.marketplace_api.add_offer(
             new_offer).offer_id
         self.offers[new_product.uid] = new_offer
     except Exception as e:
         print('error on adding a new offer:', e)
Ejemplo n.º 4
0
    def append_marketplace_situations(self, line, csv_merchant_id):
        self.prepare_joined_data(line['product_id'], line['timestamp'],
                                 line['merchant_id'])
        self.product_prices[line['product_id']].append(float(line['price']))

        merchant = self.joined_data[line['product_id']][
            line['timestamp']].merchants[line['merchant_id']]
        if line['offer_id'] not in merchant:
            merchant[line['offer_id']] = Offer(
                line['amount'], line['merchant_id'], line['offer_id'],
                line['price'], line['prime'], line['product_id'],
                line['quality'], {
                    'standard': line['shipping_time_standard'],
                    'prime': line['shipping_time_prime']
                }, '', line['uid'])
 def post_offer(self, product, price, existing_offer):
     new_offer = Offer.from_product(product)
     new_offer.price = price
     new_offer.shipping_time = {
         'standard': settings['shipping'],
         'prime': settings['primeShipping']
     }
     new_offer.prime = True
     try:
         if existing_offer is None:
             return self.marketplace_api.add_offer(new_offer)
         else:
             self.marketplace_api.restock(existing_offer.offer_id,
                                          product.amount, product.signature)
             return None
     except Exception as e:
         print('error on posting an offer:', e)
Ejemplo n.º 6
0
 def add_new_product_to_offers(self, new_product, marketplace_offers):
     new_offer = Offer.from_product(new_product)
     new_offer.price = self.calculate_prices(marketplace_offers,
                                             new_product.uid,
                                             new_product.price,
                                             new_product.product_id)
     new_offer.shipping_time = {
         'standard': settings['shipping'],
         'prime': settings['primeShipping']
     }
     new_offer.prime = True
     try:
         new_offer.offer_id = self.marketplace_api.add_offer(
             new_offer).offer_id
         self.products[new_product.uid] = new_product
         self.offers[new_product.uid] = new_offer
     except Exception as e:
         print('error on adding a new offer:', e)
Ejemplo n.º 7
0
    def execute_logic(self):
        try:
            offers = self.marketplace_api.get_offers(include_empty_offers=True)
        except Exception as e:
            print('error on getting offers from the marketplace:', e)
            return 1.0 / settings['max_req_per_sec']
        own_offers = [
            offer for offer in offers if offer.merchant_id == self.merchant_id
        ]
        own_offers_by_uid = {offer.uid: offer for offer in own_offers}
        missing_offers = settings['max_amount_of_offers'] - sum(
            offer.amount for offer in own_offers)

        new_products = []
        for _ in range(missing_offers):
            try:
                prod = self.producer_api.buy_product()
                new_products.append(prod)
            except:
                pass

        for product in new_products:
            try:
                if product.uid in own_offers_by_uid:
                    offer = own_offers_by_uid[product.uid]
                    offer.amount += product.amount
                    offer.signature = product.signature
                    self.marketplace_api.restock(offer.offer_id,
                                                 amount=product.amount,
                                                 signature=product.signature)
                    offer.price = self.price_product(product)
                    self.marketplace_api.update_offer(offer)
                else:
                    offer = Offer.from_product(product)
                    offer.price = self.price_product(product)
                    offer.prime = True
                    offer.shipping_time['standard'] = self.settings['shipping']
                    offer.shipping_time['prime'] = self.settings[
                        'primeShipping']
                    self.marketplace_api.add_offer(offer)
            except Exception as e:
                print('could not handle product:', product, e)

        return 1.0 / settings['max_req_per_sec']
Ejemplo n.º 8
0
    def append_marketplace_situations(self, line, csv_merchant_id=None):
        merchant_id = line['merchant_id']
        if csv_merchant_id == merchant_id:
            merchant_id = self.merchant_id

        if len(self.timestamps
               ) > 0 and line['timestamp'] <= self.timestamps[-1]:
            return
        self.prepare_joined_data(line['product_id'], line['timestamp'],
                                 merchant_id)
        merchant = self.joined_data[line['product_id']][
            line['timestamp']].merchants[merchant_id]
        if line['offer_id'] not in merchant:
            merchant[line['offer_id']] = Offer(
                line['amount'], line['merchant_id'], line['offer_id'],
                line['price'], line['prime'], line['product_id'],
                line['quality'], {
                    'standard': line['shipping_time_standard'],
                    'prime': line['shipping_time_prime']
                }, '', line['uid'])
Ejemplo n.º 9
0
    def execute_logic(self):
        next_training_session = self.last_learning \
                                + datetime.timedelta(minutes=self.settings['minutes_between_learnings'])
        if next_training_session <= datetime.datetime.now():
            self.last_learning = datetime.datetime.now()
            trigger_learning(self.merchant_token, self.merchant_id,
                             settings['kafka_reverse_proxy_url'])

        request_count = 0

        self.models_per_product = self.load_models_from_filesystem()

        try:
            offers = self.marketplace_api.get_offers(include_empty_offers=True)
        except Exception as e:
            print('error on getting offers:', e)
            return max(1.0, request_count) / settings['max_req_per_sec']
        own_offers = [
            offer for offer in offers if offer.merchant_id == self.merchant_id
        ]
        own_offers_by_uid = {offer.uid: offer for offer in own_offers}
        missing_offers = settings['max_amount_of_offers'] - sum(
            offer.amount for offer in own_offers)

        new_products = []
        for _ in range(missing_offers):
            try:
                prod = self.producer_api.buy_product()
                new_products.append(prod)
            except:
                pass

        products = self.producer_api.get_products()
        product_prices_by_uid = {
            product.uid: product.price
            for product in products
        }

        for own_offer in own_offers:
            if own_offer.amount > 0:
                own_offer.price = self.price_product(own_offer,
                                                     product_prices_by_uid,
                                                     current_offers=offers)
                try:
                    self.marketplace_api.update_offer(own_offer)
                    request_count += 1
                except Exception as e:
                    print('error on updating offer:', e)

        for product in new_products:
            try:
                if product.uid in own_offers_by_uid:
                    offer = own_offers_by_uid[product.uid]
                    offer.amount += product.amount
                    offer.signature = product.signature
                    try:
                        self.marketplace_api.restock(
                            offer.offer_id,
                            amount=product.amount,
                            signature=product.signature)
                    except Exception as e:
                        print('error on restocking an offer:', e)
                    offer.price = self.price_product(product,
                                                     product_prices_by_uid,
                                                     current_offers=offers)
                    try:
                        self.marketplace_api.update_offer(offer)
                        request_count += 1
                    except Exception as e:
                        print('error on updating an offer:', e)
                else:
                    offer = Offer.from_product(product)
                    offer.prime = True
                    offer.shipping_time['standard'] = self.settings['shipping']
                    offer.shipping_time['prime'] = self.settings[
                        'primeShipping']
                    offer.merchant_id = self.merchant_id
                    offer.price = self.price_product(product,
                                                     product_prices_by_uid,
                                                     current_offers=offers +
                                                     [offer])
                    try:
                        self.marketplace_api.add_offer(offer)
                    except Exception as e:
                        print('error on adding an offer to the marketplace:',
                              e)
            except Exception as e:
                print('could not handle product:', product, e)

        return max(1.0, request_count) / settings['max_req_per_sec']
Ejemplo n.º 10
0
 def create_own_offer(self) -> Offer:
     return Offer(product_id='1', price=30.0)
Ejemplo n.º 11
0
 def create_current_offers(self) -> List[Offer]:
     offer_list = list()
     offer_list.append(Offer(price=20.0))
     offer_list.append(Offer(price=40.0))
     return offer_list
 def generate_offer_list(self):
     offer_list = list()
     offer_list.append(Offer(offer_id='1'))
     return offer_list