python-shopee icon indicating copy to clipboard operation
python-shopee copied to clipboard

Information that can be edited in individual items

Open himacri opened this issue 4 years ago • 14 comments

Please let me know if I'm super new to this. I am going to use pyshopee to modify my item information. Can I update the following information?

Condition of the item: new or used Brand name: ex)No Brand Remove from the list when stock is gone.

I'm new to python itself and I don't know how to write code, so it would be great if you could post some examples. Thanks.

himacri avatar Sep 12 '20 02:09 himacri

import pyshopee

'''For updating an old product 
'''

client = pyshopee.Client( shopid=your_shopid, partnerid=your_partnerid, API_key=your_key )

# Use this call to get detail of item
item_detail = client.item.get_item_detail(item_id=your_item_id)

# Use this call to update a product item. 
# update_data would be the attribute you want to modify like category_id, name, description ... 
# you can find it in https://open.shopee.com/documents?module=2&type=1&id=376
update_data = {
        "item_id": item_id,
        "category_id": category_id,
        "name": item_name,
        "description": description,
        "price": item_price,
        "stock": 1, ... 
    }
response = client.item.update_item(update_data=update_data)
print(response)
import pyshopee

'''For creating a new product 
'''

client = pyshopee.Client( shopid=your_shopid, partnerid=your_partnerid, API_key=your_key )

# Use this call to create a new product 
# The detail of product_data could be found in https://open.shopee.com/documents?module=2&type=1&id=365
product_data = {
        "item_id": item_id,
        "category_id": category_id,
        "name": item_name,
        "description": description,
        "price": item_price,
        "stock": 1, ... 
    }
response = client.item.add(product_data=product_data)
print(response)

JimCurryWang avatar Sep 14 '20 12:09 JimCurryWang

@himacri I hope this would be helpful for you.

JimCurryWang avatar Sep 14 '20 12:09 JimCurryWang

Thank you for the kind explanation. The examples are very clear.

himacri avatar Sep 14 '20 14:09 himacri

Hi. I tried to update the information using the examples you gave me, but it doesn't work. Please tell me what's wrong with this code.

import pyshopee item_detail = client.item.get_item_detail(item_id=5151063853) print(item_detail) response = client.item.update_item(item_id=5151063853,name='tanuki', description="test_play", condition='used') print(response)

Thanks.

himacri avatar Sep 15 '20 14:09 himacri

@himacri you must create client before you used it please take a look this

client = pyshopee.Client( shopid=your_shopid, partnerid=your_partnerid, API_key=your_key )

and before you can used get item detail. you must registered your shop into the shopee API first.

tjengbudi avatar Sep 15 '20 14:09 tjengbudi

client is done before this code and before you can use item detail.

himacri avatar Sep 15 '20 14:09 himacri

@himacri read the shopee open document first before you try to use this api. it will make easier for you to understand.

authorize_url = client.shop.authorize(redirect_url="https://shopee.tw") print(authorize_url)

run this code to register your shop to your shopee api account. after that you will can used

client = pyshopee.Client( shopid=your_shopid, partnerid=your_partnerid, API_key=your_key )

tjengbudi avatar Sep 15 '20 14:09 tjengbudi

Authentication is already successful, for example, deleting an item is successful. paste below the error that appears after I run it.

{'item': {'logistics': [{'logistic_name': 'Japan Post', 'enabled': True, 'logistic_id': 18041, 'is_free': False}], 'original_price': 62.93, 'package_width': 0.0, 'cmt_count': 0, 'weight': 1.5, 'shopid': 303776630, 'currency': 'SGD', 'create_time': 1599292355, 'likes': 0, 'images': ['https://cf.shopee.sg/file/be3a873008d636371e1ccde64946914d', 'https://cf.shopee.sg/file/be3a873008d636371e1ccde64946914d', 'https://cf.shopee.sg/file/b0e2fe81c1b3c0720fa29115e0bedf8f', 'https://cf.shopee.sg/file/c9ce7ac33bde87fa2f4ba8a9ec89b528', 'https://cf.shopee.sg/file/f6874c6747be75cc074bd88226f8a510'], 'tenures': [], 'is_2tier_item': False, 'days_to_ship': 2, 'package_length': 0.0, 'reserved_stock': 0, 'stock': 1, 'status': 'NORMAL', 'update_time': 1599840394, 'description': 'Although it is not taken out from the box, it is dirty and damaged.\nThank you for your understanding.\n\nIt will be shipped in recycled cardboard.\n\nNo roses. Available for immediate purchase.\n\n#Rei Ayanami\n#Makinami Mari Illustrious\n#Evangelion\n#EVANGELION\n#1 lottery', 'views': 4, 'price': 62.93, 'sales': 0, 'discount_id': 0, 'item_id': 5151063853, 'wholesales': [], 'condition': 'NEW', 'package_height': 0.0, 'name': 'Eva figure Mari Ray Evangelion', 'rating_star': 0.0, 'item_sku': 'm41832613578', 'variations': [], 'size_chart': '', 'is_pre_order': False, 'has_variation': False, 'attributes': [{'attribute_name': 'Brand', 'is_mandatory': True, 'attribute_id': 21748, 'attribute_value': 'No Brand', 'attribute_type': 'STRING_TYPE'}], 'category_id': 20452}, 'warning': '', 'request_id': '32b39eaa40c650df985708c911ced9ea'}

TypeError Traceback (most recent call last) in () 4 print(item_detail) 5 ----> 6 response = client.item.update_item(item_id=5151063853,name='tanuki', description="test_play", condition='used') 7 print(response)

TypeError: update_item() got an unexpected keyword argument 'item_id'

himacri avatar Sep 15 '20 15:09 himacri

@himacri I think you should put all of the params you got from function "get_item_detail" into your function "update_item"

JimCurryWang avatar Sep 15 '20 20:09 JimCurryWang

For pyshopee, we need to build a param named "update_data"

def update_item(self, update_data):
        """
        Use this call to update a product item. 
        Should get dependency by calling below API first
        
        shopee.item.GetItemDetail
        :param update_data:
        :return:
        """
        return self.client.execute("item/update", "POST", update_data)

Hence, we need to build the "update_data" first.


# Example 

shopid = 1234567
partner_id = 9876543221
secret_key = "12dqwert346788234ee567300456dfgh34sdfgb8e523123052a123fb6176c567"

shopee = pyshopee.Client(shop_id=shopid, partner_id=partner_id, secret_key=secret_key) 


# some functions to build the body
def _cleaning_hashtag(description, **params):
    '''
    '''
    hashtag_pattern = re.compile(r"#(.*)[\s]{0,1}", flags=re.UNICODE)
    cleaned_description = hashtag_pattern.sub(r' ', description)

    return cleaned_description


def _builder_weight(item_weight, default_weight=0.1, **params):
    ''' the net weight of this item, the unit is KG.
        : type: float
        : require: yes
    '''
    weight = item_weight/100000  if item_weight else default_weight
    
    return float(weight)


def _builder_logistics(**params):
    '''
    '''

    logistics = list()

    resp = shopee.logistic.get_logistics()

    logistics_resp = resp.get("logistics")
    for logis in logistics_resp:
        if logis.get('enabled'):
            if logis.get('fee_type') == 'SIZE_SELECTION':
                logis['sizes'] = logis['sizes'][0]['size_id']
            else:
                logistics.append(logis)

    return logistics



def main():
    update_data = {
        "category_id": category_id,
        "name": item_name.strip(),
        "description": description,
        "price": item_price,
        "stock": 1,
        "weight": _builder_weight(item_weight=item_weight, default_weight=0.1),
        # "variations": variations,
        "images": [
            {
                "url": "https://cfshopeetw-a.akamaihd.net/file/b77c9b16ec1dd734c0c663fd1fcb8ac0"
            },
            {
                "url": 'https://cfshopeetw-a.akamaihd.net/file/b77c9b16ec1dd734c0c663fd1fcb8ac0'
            },
            {
                "url": 'https://cfshopeetw-a.akamaihd.net/file/b77c9b16ec1dd734c0c663fd1fcb8ac0'
            },
            {
                "url": 'https://cfshopeetw-a.akamaihd.net/file/b77c9b16ec1dd734c0c663fd1fcb8ac0'
            }
        ],
        "attributes": [{'attributes_id': 1365, 'value': 'Napla'}]
        "logistics": _builder_logistics(),
        # "package_length": 200,
        # "package_width": 200,
        # "package_height": 200,
        # "days_to_ship": 10,
        # "wholesales": wholesales
    }

    response = shopee.item.add(update_data = update_data)
    pprint(response)

JimCurryWang avatar Sep 15 '20 20:09 JimCurryWang

import pyshopee

'''For updating an old product 
'''

client = pyshopee.Client( shopid=your_shopid, partnerid=your_partnerid, API_key=your_key )

# Use this call to get detail of item
item_detail = client.item.get_item_detail(item_id=your_item_id)

# Use this call to update a product item. 
# update_data would be the attribute you want to modify like category_id, name, description ... 
# you can find it in https://open.shopee.com/documents?module=2&type=1&id=376
update_data = {
        "item_id": item_id,
        "category_id": category_id,
        "name": item_name,
        "description": description,
        "price": item_price,
        "stock": 1, ... 
    }
response = client.item.update_item(update_data=update_data)
print(response)
import pyshopee

'''For creating a new product 
'''

client = pyshopee.Client( shopid=your_shopid, partnerid=your_partnerid, API_key=your_key )

# Use this call to create a new product 
# The detail of product_data could be found in https://open.shopee.com/documents?module=2&type=1&id=365
product_data = {
        "item_id": item_id,
        "category_id": category_id,
        "name": item_name,
        "description": description,
        "price": item_price,
        "stock": 1, ... 
    }
response = client.item.add(product_data=product_data)
print(response)

@tjengbudi @himacri The origin examples have some error in it accidentally, however, I have corrected that. Please have a look.

JimCurryWang avatar Sep 15 '20 21:09 JimCurryWang

Thank you for your reply. I will try it.

himacri avatar Sep 15 '20 22:09 himacri

Hi. I used the code you gave me to update the product information. Thank you so much.

himacri avatar Sep 16 '20 14:09 himacri

Hi. I used the code you gave me to creating the new product information. Thank you so much.

himacri avatar Sep 20 '20 08:09 himacri