timotheus / ebaysdk-python

eBay API SDK for Python
https://developer.ebay.com/tools/sdks
Other
796 stars 321 forks source link

Finding API usage: AspectHistogram #266

Open mrchuk opened 5 years ago

mrchuk commented 5 years ago

This is effectively a duplicate of #142, which was not answered. I'm trying to get all details including the aspects from findItemsAdvanced and it is unclear from the docs how to go about this. I am able to get all pages of searches but with only the basic searchresult fields, not the aspects. I have verified my code is working because if I change AspectHistogram to SellerInfo, I get that as well.

`api_request` = {
            'keywords': kwds,
            'categoryId' : ['180250'],
            'paginationInput': {
                'entriesPerPage': '100',
                'pageNumber': page
            },
            'itemFilter': [
                {'name': 'LocatedIn',
                 'value': 'WorldWide',
                                },
            ],
            'outputSelector': [ 'AspectHistogram' , ]
        }

I thought by setting a categoryId this should work, but apparently not. The docs in this area are a bit of a mess and I have not been able to work out what's needed here. Therre appears to be some sort of trick to it but I don't get it. Does it require one API call per item to get the aspect detail, using the categoryId of the item? Surely not!

Can we please get a usage example for this feature?

Update: had a closer look at the dict being returned, and it DOES include the aspect container. However this is the per-category aspects, not the per-item ones. So the revised question is, can the API be used to return the aspects defined for a specific item, and if so, how? I am hoping iit doesn't require making an additional call for each item using the catagoryId returned for it, but it is increasingly looking like that.

fsmosca commented 4 years ago

@mrchuk I tried your category id and I get the aspect histogram.

Sample code

import json

from ebaysdk.exception import ConnectionError
from ebaysdk.finding import Connection as Finding

def finding_find_items_advanced_with_aspect_histogram_demo():
    """        
    API:
        eBay API name: Finding,
        Call name: findItemsAdvanced,
        Reference: https://developer.ebay.com/Devzone/finding/CallRef/findItemsAdvanced.html

    SDK: ebaysdk-2.1.5

    Enviroment:
        Production: true, Sandbox: false

    System:
        Windows 10
        Python 3.7.4
    """
    out_fn = 'fia_aspect.json'

    cat_id = '180250'  # Level: 2, name: Model Railroads & Trains

    request = {
            'categoryId': cat_id,
            'itemFilter': [
                {'name': 'HideDuplicateItems', 'value': True},
                {'name': 'MinPrice', 'value': 50}
            ],
            'paginationInput': {'entriesPerPage': 10, 'pageNumber': 1},
            'outputSelector': ['AspectHistogram']
    }

    try:
        api = Finding(config_file='my_ebay.yaml', https=True, debug=False)        

        response = api.execute('findItemsAdvanced', request)
        r = response.dict()

        print(f'Extaction date time w.r.t UTC 0: {r["timestamp"]}')
        print()

        item_list = r['searchResult']['item']

        for n, item in enumerate(item_list):
            item_id = item['itemId']
            title = item['title']
            url = item['viewItemURL']
            price = item['sellingStatus']['currentPrice']['value']
            unit = item['sellingStatus']['currentPrice']['_currencyId']

            print(f'num      : {n+1}')
            print(f'item id  : {item_id}')
            print(f'url      : {url}')
            print(f'title    : {title}')
            print(f'price    : {price} {unit}')
            print()

            if n >= 4:
                break

        with open(out_fn, 'w', encoding='utf-8') as f:
            f.write(json.dumps(r, indent=4))

    except ConnectionError as e:
        print(e)
        print(e.response.dict())

    except Exception:
        print('Unexpected exception:')
        raise

if __name__ == '__main__':
    finding_find_items_advanced_with_aspect_histogram_demo()

Ouput from fia_aspect.json file

...

    "aspectHistogramContainer": {
        "domainDisplayName": "Model Railroads & Trains",
        "aspect": [
            {
                "valueHistogram": [
                    {
                        "count": "26",
                        "_valueName": "3rd Rail"
                    },
                    {
                        "count": "1",
                        "_valueName": "A.C. Gilbert"
                    },
                    {
                        "count": "1",
                        "_valueName": "Academy"
                    },
                    {
                        "count": "626",
                        "_valueName": "Accucraft"
                    },

...

You may close this issue if you no longer have problem with it.