import keepa
key = 'engpg35aootqo11korrc9p4otp0p1il5a5ssnjvdq5ehfnh6us8hb1klobq61c9t'
api = keepa.Keepa(key)
# print(dir(api))
for d in dir(api):
    print(d)
# categories = api.search_for_categories("movies")
# print(categories)
# categories = api.search_for_categories('Tabletop & Serveware')
categories = api.search_for_categories('spoons')
print(len(categories.keys()), list(categories.keys()))
for cat_id in categories:
    print(cat_id, categories[cat_id]['name'])


# quit()
product_parms = {
    # 'author': 'jim butcher',
    'sort': ["current_SALES", "asc"],
    'current_SALES_gte': 500,
    'current_SALES_lte': 510,
    'categories_include': [498924]
}

product_parms = {
    "current_SALES_gte": 500,
    "current_SALES_lte": 50000,
    "rootCategory": 1055398,  # 一级分类id
    'categories_include': list(categories.keys()),
    "current_AMAZON_gte": 1998,  # 亚马逊当前价格 -- 1998>=19.98
    "current_AMAZON_lte": 1998,  # 亚马逊当前价格 -- 1998<=19.98
    "sort": [
        [
            "current_SALES",
            "asc"
        ]
    ],
    "productType": [
        0,
        1
    ],
    "perPage": 50,
    "page": 0,
    # 'categories_include': [3206325011]
}

product_parms = {
    # "title": "spoons",
    "current_AMAZON_gte": 1,
    "current_AMAZON_lte": 3000,
    # "current_SALES_gte": 100,
    # "current_SALES_lte": 1000,
    'delta30_SALES_gte': 100,
    'delta30_SALES_lte': 500,
    # "categories_include": [
    #     "289754"
    # ],
    "sort": [
        [
            "current_SALES",
            "asc"
        ]
    ],
    "productType": [
        0,
        1
    ],
    "perPage": 500,
    "page": 0
}

asins = api.product_finder(product_parms=product_parms)
print(len(asins), asins)

# quit()


class KeepApi(object):

    def __init__(self):
        self.key = 'engpg35aootqo11korrc9p4otp0p1il5a5ssnjvdq5ehfnh6us8hb1klobq61c9t'
        self.api = keepa.Keepa(self.key)

    def product_finder(self):
        pass

    def search_for_categories(self, searchterm=''):
        # 关键词查找分类
        return self.api.search_for_categories(
            searchterm=searchterm,
            domain='US',
            wait=True
        )

    def category_lookup(self):
        pass

    def best_sellers_query(self):
        # 返回asin_list
        pass


if __name__ == '__main__':
    # categories = api.search_for_categories("Home & Kitchen")
    # categories = api.search_for_categories("coasters")
    # print(type(categories), categories)
    # print(list(categories.items())[0], categories)
    # print(list(categories.items())[1], categories)
    # category = list(categories.items())[0][0]
    # asins = api.best_sellers_query(category)
    # print(len(asins), asins)
    # rank_avg_range参数无用,报错
    # asins = api.best_sellers_query(category, rank_avg_range=100)
    # print(len(asins), asins)

    # api_obj = KeepApi()
    # categories = api_obj.search_for_categories('science')
    # print(type(categories), categories)
    # for cat_id in categories:
    #     print(cat_id, categories[cat_id]['name'])
    #
    # for k, v in categories.items():
    #     print(k, v)
    pass