Esempio n. 1
0
 def get_active_upc(self):
     '''获取没有被占用的upc'''
     seller = self.instance_id.seller_id
     marketplaceid = self.instance_id.market_place_id
     mws_obj = Products(access_key=str(seller.access_key),
                        secret_key=str(seller.secret_key),
                        account_id=str(seller.merchant_id),
                        region=seller.country_id.amazon_marketplace_code
                        or seller.country_id.code,
                        proxies={})
     data = xlrd.open_workbook('/Users/king/Desktop/upc.xls')
     table = data.sheets()[0]
     workbook = xlwt.Workbook(encoding='utf8')
     valid_sheet = workbook.add_sheet('valid upc')
     invalid_sheet = workbook.add_sheet('invalid upc')
     max_time = 20
     wait_time = 1
     i = 0
     j = 0
     upcs = set()
     for row in range(table.nrows):
         upc = table.cell(row, 0).value
         if upc not in upcs:
             upcs.add(upc)
         else:
             continue
         if type(upc) is not unicode:
             raise UserError(u'type(upc) is not unicode!')
         upc = upc.replace(' ', '')
         while True:
             try:
                 result = mws_obj.list_matching_products(
                     marketplaceid=marketplaceid, query=upc, contextid=None)
                 break
             except Exception, e:
                 if wait_time > max_time:
                     result = False
                     break
                 time.sleep(wait_time)
                 wait_time = wait_time * 2
         if result:
             data = result.parsed
             if data.get('Products') == {}:
                 print upc, ' valid ', row
                 valid_sheet.write(i, 0, upc)
                 i += 1
             else:
                 print upc, ' invalid ', row
                 invalid_sheet.write(j, 0, upc)
                 j += 1
Esempio n. 2
0
 def print_info_by_upc(self):
     '''根据upc获取产品信息'''
     seller = self.instance_id.seller_id
     marketplaceid = self.instance_id.market_place_id
     mws_obj = Products(access_key=str(seller.access_key),
                        secret_key=str(seller.secret_key),
                        account_id=str(seller.merchant_id),
                        region=seller.country_id.amazon_marketplace_code
                        or seller.country_id.code,
                        proxies={})
     for upc in upc_list:
         result = mws_obj.list_matching_products(
             marketplaceid=marketplaceid, query=upc, contextid=None)
         data = result.parsed
         print data
Esempio n. 3
0
 def get_asin_info_from_amazon(self, all_asin, log):
     seller = self.instance_id.seller_id
     proxy_data = seller.get_proxy_server()
     marketplaceid = self.instance_id.market_place_id
     mws_obj = Products(access_key=str(seller.access_key),
                        secret_key=str(seller.secret_key),
                        account_id=str(seller.merchant_id),
                        region=seller.country_id.amazon_marketplace_code
                        or seller.country_id.code,
                        proxies=proxy_data)
     asin_info = {}
     asins = []
     asin_count = len(all_asin)
     i = 1
     for asin in all_asin:
         asins.append(asin)
         if len(asins) == 5:
             self.get_data_from_amazon(mws_obj, marketplaceid, asins,
                                       asin_info, log)  # 获取亚马逊数据
             asins = []
             if i % 10 == 0:
                 log.message = u'正在获取店铺产品信息... 一共%d个产品 已获取%d个产品' % (
                     asin_count, i)
                 self._cr.commit()
         if i == asin_count:
             if asins:
                 self.get_data_from_amazon(mws_obj, marketplaceid, asins,
                                           asin_info, log)  # 获取亚马逊数据
             log.message = u'获取店铺产品信息完成!一共%d个产品,用时%s' % (
                 asin_count, self.get_del_time(log.create_date))
             self._cr.commit()
         i += 1
     return asin_info
Esempio n. 4
0
 def print_product_info_by_asin(self):
     '''根据asin获取产品信息'''
     seller = self.instance_id.seller_id
     marketplaceid = self.instance_id.market_place_id
     mws_obj = Products(access_key=str(seller.access_key),
                        secret_key=str(seller.secret_key),
                        account_id=str(seller.merchant_id),
                        region=seller.country_id.amazon_marketplace_code
                        or seller.country_id.code,
                        proxies={})
     result = mws_obj.get_matching_product_for_id(
         marketplaceid=marketplaceid, type='ASIN', ids=asins)
     data = result.parsed
     if type(data) is not list:
         data = [data]
     for item in data:
         print item
Esempio n. 5
0
 def query_code(self):
     '''查询产品信息'''
     seller = self.instance_id.seller_id
     marketplaceid = self.instance_id.market_place_id
     print seller.access_key, seller.secret_key, seller.merchant_id, seller.country_id.amazon_marketplace_code
     mws_obj = Products(access_key=str(seller.access_key),
                        secret_key=str(seller.secret_key),
                        account_id=str(seller.merchant_id),
                        region=seller.country_id.amazon_marketplace_code
                        or seller.country_id.code,
                        proxies={})
     if self.type in ('ASIN', 'SellerSKU'):
         result = mws_obj.get_matching_product_for_id(
             marketplaceid=marketplaceid, type=self.type, ids=[self.code])
         self.result = result.parsed
     elif self.type == 'UPC':
         result = mws_obj.list_matching_products(
             marketplaceid=marketplaceid, query=self.code, contextid=None)
         self.result = result.parsed
Esempio n. 6
0
 def print_product_info_by_sku(self):
     '''根据sku获取产品信息'''
     seller = self.instance_id.seller_id
     marketplaceid = self.instance_id.market_place_id
     print seller.access_key, seller.secret_key, seller.merchant_id, seller.country_id.amazon_marketplace_code
     mws_obj = Products(access_key=str(seller.access_key),
                        secret_key=str(seller.secret_key),
                        account_id=str(seller.merchant_id),
                        region=seller.country_id.amazon_marketplace_code
                        or seller.country_id.code,
                        proxies={})
     # print seller.country_id.amazon_marketplace_code or seller.country_id.code,marketplaceid
     result = mws_obj.get_matching_product_for_id(
         marketplaceid=marketplaceid, type='SellerSKU', ids=skus)
     data = result.parsed
     if type(data) is not list:
         data = [data]
     for item in data:
         sku = item.get('Id', {}).get('value', '')
         asin = item.get('Products', {}).get('Product', {}).get(
             'Identifiers', {}).get('MarketplaceASIN',
                                    {}).get('ASIN', {}).get('value', '')
         print sku, asin
    def create_product(self, product_tmpls, all_product_info):
        '''创建产品'''
        attr_obj = self.env['product.attribute']
        attr_val_obj = self.env['product.attribute.value']
        # amazon_att_obj = self.env['amazon.attribute.ept']
        for template in product_tmpls:
            Products = template.get('Products', {})
            Product = Products.get('Product', {})
            Identifiers = Product.get('Identifiers', {})
            SalesRankings = Product.get('SalesRankings', {})
            AttributeSets = Product.get('AttributeSets', {})
            ItemAttributes = AttributeSets.get('ItemAttributes', {})
            Title = unicode(ItemAttributes.get('Title', {}).get('value', ''))
            Brand = ItemAttributes.get('Brand', {}).get('value', '')
            Manufacturer = ItemAttributes.get('Manufacturer', {}).get('value', '')
            SmallImage = ItemAttributes.get('URL', {}).get('value', '')
            Relationships = Product.get('Relationships', {})
            VariationChild = Relationships.get('VariationChild', [])
            product_variant_ids = []
            for child in VariationChild:
                child.pop('Identifiers')
                child.pop('MarketplaceId')
                for (attr_name, attr_val) in child.items():
                    attr_record = attr_obj.search([('name', '=', attr_name)], limit=1)
                    if not attr_record:
                        attr_record = attr_obj.create({'name': attr_name})
                    attr_val_record = attr_val_obj.search([('name', '=', attr_name)], limit=1)
                    if not attr_val_record:
                        attr_val_record = attr_val_obj.create({
                            'attribute_id': attr_val_record.id,
                            'name': attr_val.get('value', ''),
                        })

            val = {
                'name': Title,
                'type': 'product',
                'product_variant_ids': product_variant_ids,
            }
 def get_asin_info_from_amazon(self, all_asin):
     asin_count = len(all_asin)
     self.progress_message = u'正在获取产品数据... 共%d个产品,已获取%d个产品' % (asin_count,
                                                               0)
     self._cr.commit()
     instance = self.instance_id
     seller = instance.seller_id
     proxy_data = seller.get_proxy_server()
     mws_obj = Products(access_key=str(seller.access_key),
                        secret_key=str(seller.secret_key),
                        account_id=str(seller.merchant_id),
                        region=seller.country_id.amazon_marketplace_code
                        or seller.country_id.code,
                        proxies=proxy_data)
     marketplace_id = instance.market_place_id
     asin_info = {}
     asins = []
     i = 1
     for asin in all_asin:
         print i
         asins.append(asin)
         if len(asins) == 5:
             self.get_data_from_amazon(mws_obj, marketplace_id, asins,
                                       asin_info)  # 获取亚马逊数据
             asins = []
             if i % 10 == 0:
                 self.progress_message = u'正在获取产品数据... 共%d个产品,已获取%d个产品' % (
                     asin_count, i)
                 self._cr.commit()
         if i == asin_count:
             if asins:
                 self.get_data_from_amazon(mws_obj, marketplace_id, asins,
                                           asin_info)  # 获取亚马逊数据
             break
         i += 1
     self.product_data = base64.b64encode(str(asin_info))
     return True
    def download_sale_order(self):
        '''下载订单 test'''
        if self.start_date:
            db_import_time = time.strptime(self.start_date, "%Y-%m-%d %H:%M:%S")
            db_import_time = time.strftime("%Y-%m-%dT%H:%M:%S", db_import_time)
            start_date = time.strftime("%Y-%m-%dT%H:%M:%S",
                                       time.gmtime(time.mktime(time.strptime(db_import_time, "%Y-%m-%dT%H:%M:%S"))))
            start_date = str(start_date) + 'Z'
        else:
            start_date = False
        if self.end_date:
            db_import_time = datetime.strptime(self.end_date, "%Y-%m-%d %H:%M:%S") - timedelta(hours=5)
            db_import_time = db_import_time.strftime("%Y-%m-%dT%H:%M:%S")
            end_date = time.strftime("%Y-%m-%dT%H:%M:%S",
                                     time.gmtime(time.mktime(time.strptime(db_import_time, "%Y-%m-%dT%H:%M:%S"))))
            end_date = str(end_date) + 'Z'
        else:
            end_date = False

        vals = {}
        for instance in self.instance_ids:
            seller = instance.seller_id
            if vals.has_key(seller):
                vals[seller].append(instance.market_place_id)
            else:
                vals[seller] = [instance.market_place_id]
        for seller, marketplaces in vals.items():
            # print seller,marketplaces
            # data = self.get_sale_order_from_amazon(seller, marketplaces, end_date, start_date)
            with open('/Users/king/Desktop/sale_order.txt', 'r') as f:
                content = f.read()
                data = eval(content)
            #找出odoo中不存在的asin
            sku_info = {}
            for val in data:
                order = val['order']
                MarketplaceId = order.get('MarketplaceId', {}).get('value', '')
                if not sku_info.has_key(MarketplaceId):
                    sku_info[MarketplaceId] = []
                for line in val['order_line']:
                    OrderItem = line.get('OrderItems', {}).get('OrderItem', [])
                    if type(OrderItem) is dict:
                        OrderItem = [OrderItem]
                    for order_item in OrderItem:
                        SellerSKU = order_item.get('SellerSKU', {}).get('value', '')
                        asin = order_item.get('ASIN', {}).get('value', '')
                        product = self.env['amazon.product.ept'].search([
                            '|', ('product_asin', '=', asin),
                            ('default_code', '=', SellerSKU)
                        ])
                        if not product:
                            sku_info[MarketplaceId].append(SellerSKU)
            # print 'odoo中不存在的产品sku:',sku_info
            #根据sku获取产品信息
            proxy_data = seller.get_proxy_server()
            mws_obj = Products(access_key=str(seller.access_key), secret_key=str(seller.secret_key),
                               account_id=str(seller.merchant_id),
                               region=seller.country_id.amazon_marketplace_code or seller.country_id.code,
                               proxies=proxy_data)
            for (marketplaceid, skus) in sku_info.items():
                all_product_info = {}
                product_info = []
                sku_list = []
                for i in range(len(skus)):
                    print i
                    sku_list.append(skus[i])
                    if len(sku_list) == 5:
                        data = self.get_product_data_by_sku(mws_obj, marketplaceid, 'SellerSKU', sku_list)
                        product_info.extend(data)
                        sku_list = []
                    if i + 1 == len(skus) and sku_list:
                        data = self.get_product_data_by_sku(mws_obj, marketplaceid, 'SellerSKU', sku_list)
                        product_info.extend(data)
                # print 'product_info:',product_info
                # 找出母体
                parent_asins = []
                product_tmpls = []
                for pro_info in product_info:
                    Product = pro_info.get('Products', {}).get('Product', {})
                    Identifiers = Product.get('Identifiers', {})
                    asin = Identifiers.get('MarketplaceASIN', {}).get('ASIN', {}).get('value', '')
                    all_product_info.update({asin: pro_info})
                    Relationships = Product.get('Relationships', {})
                    if Relationships == {}:
                        product_tmpls.append(pro_info)
                    elif Relationships.has_key('VariationChild'):
                        product_tmpls.append(pro_info)
                    elif Relationships.has_key('VariationParent'):
                        parent_asin = Relationships.get('VariationParent', {}).get('Identifiers', {})\
                            .get('MarketplaceASIN', {}).get('ASIN', {}).get('value', '')
                        if parent_asin not in parent_asins:
                            parent_asins.append(parent_asin)
                # print 'product_tmpls,parent_asins:',product_tmpls,parent_asins
                #获取母体信息
                asin_list = []
                for i in range(len(parent_asins)):
                    asin = parent_asins[i]
                    asin_list.append(asin)
                    if len(asin_list) == 5:
                        data = self.get_product_data_by_sku(mws_obj, marketplaceid, 'ASIN', asin_list)
                        product_tmpls.extend(data)
                        asin_list = []
                    if i + 1 == len(parent_asins) and asin_list:
                        data = self.get_product_data_by_sku(mws_obj, marketplaceid, 'ASIN', asin_list)
                        product_tmpls.extend(data)
                # print 'product_tmpls:', product_tmpls
                # #获取子产品asin list
                # child_asin_list = []
                # for product_tmpl in product_tmpls:
                #     VariationChild = product_tmpl.get('Products', {}).get('Product', {}).get('Relationships', {})\
                #         .get('VariationChild', [])
                #     for child in VariationChild:
                #         child_asin = child.get('Identifiers', {}).get('MarketplaceASIN', {}).get('ASIN', {})\
                #             .get('value', '')
                #         if child_asin not in child_asin_list and not all_product_info.has_key(child_asin):
                #             child_asin_list.append(child_asin)
                # # print 'child_asin_list:',child_asin_list
                # #获取子产品信息
                # asin_list = []
                # for i in range(len(child_asin_list)):
                #     data = []
                #     asin = child_asin_list[i]
                #     asin_list.append(asin)
                #     if len(asin_list) == 5:
                #         data = self.get_product_data_by_sku(mws_obj, marketplaceid, 'ASIN', asin_list)
                #         asin_list = []
                #     if i + 1 == len(child_asin_list) and asin_list:
                #         data = self.get_product_data_by_sku(mws_obj, marketplaceid, 'ASIN', asin_list)
                #     if data:
                #         for child_pro_info in data:
                #             asin = child_pro_info.get('Products', {}).get('Product', {}).get('Identifiers', {})\
                #                 .get('MarketplaceASIN', {}).get('ASIN', {}).get('value', '')
                #             all_product_info.update({asin: child_pro_info})
                # print 'all_product_info:',all_product_info
                #创建产品
                self.create_product(product_tmpls, all_product_info)
        return