Search code examples
pythondjangopython-3.5ebay-api

Python ebay sdk - how to add item specifics dynamically?


I am using ebay-python-sdk I created the myitem object, so I can api.execute('VerifyAddItem', myitem) it. But I need to add ItemSpecifics, and here I encounter problems. I have an array of item specifics made of tuples:

a = [('Occasion', 'Casual'), ('Brand', 'Ralph Lauren'), ('Style', 'Polo Shirt')]

and i want to add it to 'myitem'. so i use:

for p in a:
    ar.append([{"Name":p[0]}, {"Value":p[1]}])

myitem["Item"]["ItemSpecifics"]["NameValueList"] = ar

but i get error 'KeyError: 'ItemSpecifics'' What do I need to change in my code to add it correctly?

If I write this code to myitem object, it is ok. I just don't know how to add it dynamically.

"ItemSpecifics": {
                "NameValueList": [
                    {"Name": "Occasion", "Value": "Casual"},
                    {"Name": "Brand", "Value": "Ralph Lauren"},
                    {"Name": "Style", "Value": "Polo Shirt"},
                    {"Name": "Sleeve Style", "Value": "Short Sleeve"}
                ]
            },

myitem object:

myitem = {
        "Item": {
            "Title": name,
            "Description": "<![CDATA[{}]]>".format(desc),
            "PrimaryCategory": {"CategoryID": "176984"},
            "StartPrice": str(round(Decimal(start_price), 2)),
            "CategoryMappingAllowed": "true",
            "Country": "GB",
            "ConditionID": "1000",
            "Currency": "GBP",
            "DispatchTimeMax": "3",
            "ListingDuration": "{}".format(str(auction_len)),
            "ListingType": "FixedPriceItem",
            "PaymentMethods": "PayPal",
            "PayPalEmailAddress": "test@ebay.com",
            "PictureDetails": {"PictureURL": pict_list},
            "PostalCode": "bh102as",
            "ProductListingDetails":{
               "EAN": "8054241786423",
            },
            "Quantity": str(round(Decimal(qty), 0)),
            "ReturnPolicy": {
                "ReturnsAcceptedOption": "ReturnsAccepted",
                "RefundOption": "MoneyBack",
                "ReturnsWithinOption": "Days_30",
                "Description": "If you are not satisfied, return the book for refund.",
                "ShippingCostPaidByOption": "Buyer"
            },
            "ShippingDetails": [{
                "ShippingType": "Free",
                "ShippingServiceOptions": {
                    "FreeShipping": "true",
                    "ShippingServicePriority": "1",
                    "ShippingService": "ShippingMethodStandard",
                    "ShippingServiceCost": "0"
                }
            },
            {
                "ShippingType": "Flat",
                "ShippingServiceOptions": {
                    "FreeShipping": "false",
                    "ShippingServicePriority": "2",
                    "ShippingService": "UK_RoyalMailSecondClassStandard",
                    "ShippingServiceCost": "0.50"
                }
            }],
            "Site": "UK"
        }
    }

Can you point me to the right direction?

Thanks in advance.


Solution

  • You're getting an error because using the x[key] = val syntax only works one level deep. Trying to assign x[y][key] = val results in an error because y doesn't exist within the dictionary. If you execute myitem['Item']['ItemSpecifics']={}, before trying to create a dictionary within it will work. Alternatively, you could specify it right away when you create you dict:

    myitem = {
        "Item": {
            ...
            "ItemSpecifics": {}
        }
    }