Skip to content

Commit a276b4f

Browse files
committed
Merge branch 'developer' into main
2 parents 6c6f78c + c65c5ab commit a276b4f

9 files changed

Lines changed: 112 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Custom folders and files to ignore
22
.vscode/
33
.DS_Store
4+
test.py
45

56

67
# Byte-compiled / optimized / DLL files

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ hotproducts = aliexpress.get_hotproducts(keywords='bluetooth earphones', max_sal
5050
print(hotproducts.products[0].product_main_image_url)
5151
```
5252

53+
**Get categories:**
54+
55+
```python
56+
parent_categories = aliexpress.get_parent_categories()
57+
child_categories = aliexpress.get_child_categories(parent_categories[0].category_id)
58+
```
59+
5360
## License
5461

5562
Copyright © 2020 Sergio Abad. See [license](https://github.com/sergioteula/python-aliexpress-api/blob/master/LICENSE) for details.

aliexpress_api/api.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
API in an easier way.
66
"""
77

8+
from aliexpress_api.errors.exceptions import CategoriesNotFoudException
9+
from aliexpress_api.helpers.categories import filter_child_categories, filter_parent_categories
10+
from aliexpress_api.models.category import ChildCategory
811
from .skd import setDefaultAppInfo
912
from .skd import api as aliapi
1013
from .errors import ProductsNotFoudException, InvalidTrackingIdException
@@ -39,6 +42,7 @@ def __init__(self,
3942
self._language = language
4043
self._currency = currency
4144
self._app_signature = app_signature
45+
self.categories = None
4246
setDefaultAppInfo(self._key, self._secret)
4347

4448

@@ -56,7 +60,7 @@ def get_products_details(self,
5660
according to the country's tax rate policy.
5761
5862
Returns:
59-
``List[models.Product]``: A list of products.
63+
``list[models.Product]``: A list of products.
6064
6165
Raises:
6266
``ProductsNotFoudException``
@@ -188,3 +192,65 @@ def get_hotproducts(self,
188192
return response
189193
else:
190194
raise ProductsNotFoudException('No products found with current parameters')
195+
196+
197+
def get_categories(self, **kwargs) -> List[Union[models.Category, ChildCategory]]:
198+
"""Get all available categories, both parent and child.
199+
200+
Returns:
201+
``list[models.Category | models.ChildCategory]``: A list of categories.
202+
203+
Raises:
204+
``CategoriesNotFoudException``
205+
``ApiRequestException``
206+
``ApiRequestResponseException``
207+
"""
208+
request = aliapi.rest.AliexpressAffiliateCategoryGetRequest()
209+
request.app_signature = self._app_signature
210+
211+
response = api_request(request, 'aliexpress_affiliate_category_get_response')
212+
213+
if response.total_result_count > 0:
214+
self.categories = response.categories.category
215+
return self.categories
216+
else:
217+
raise CategoriesNotFoudException('No categories found')
218+
219+
220+
def get_parent_categories(self, use_cache=True, **kwargs) -> List[models.Category]:
221+
"""Get all available parent categories.
222+
223+
Args:
224+
use_cache (``bool``): Uses cached categories to reduce API requests.
225+
226+
Returns:
227+
``list[models.Category]``: A list of parent categories.
228+
229+
Raises:
230+
``CategoriesNotFoudException``
231+
``ApiRequestException``
232+
``ApiRequestResponseException``
233+
"""
234+
if not use_cache or not self.categories:
235+
self.get_categories()
236+
return filter_parent_categories(self.categories)
237+
238+
239+
def get_child_categories(self, parent_category_id: int, use_cache=True, **kwargs) -> List[models.ChildCategory]:
240+
"""Get all available child categories for a specific parent category.
241+
242+
Args:
243+
parent_category_id (``int``): The parent category id.
244+
use_cache (``bool``): Uses cached categories to reduce API requests.
245+
246+
Returns:
247+
``list[models.ChildCategory]``: A list of child categories.
248+
249+
Raises:
250+
``CategoriesNotFoudException``
251+
``ApiRequestException``
252+
``ApiRequestResponseException``
253+
"""
254+
if not use_cache or not self.categories:
255+
self.get_categories()
256+
return filter_child_categories(self.categories, parent_category_id)

aliexpress_api/errors/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class ProductsNotFoudException(AliexpressException):
3232
"""Raised if no products are found"""
3333
pass
3434

35+
class CategoriesNotFoudException(AliexpressException):
36+
"""Raised if no categories are found"""
37+
pass
38+
3539
class InvalidTrackingIdException(AliexpressException):
3640
"""Raised if the tracking ID is not present or invalid"""
3741
pass

aliexpress_api/helpers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .requests import api_request
22
from .arguments import get_list_as_string, get_product_ids
33
from .products import parse_products
4+
from .categories import filter_parent_categories, filter_child_categories
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List, Union
2+
from .. import models
3+
4+
5+
def filter_parent_categories(categories: List[Union[models.Category, models.ChildCategory]]) -> List[models.Category]:
6+
filtered_categories = []
7+
8+
for category in categories:
9+
if not hasattr(category, 'parent_category_id'):
10+
filtered_categories.append(category)
11+
12+
return filtered_categories
13+
14+
15+
def filter_child_categories(categories: List[Union[models.Category, models.ChildCategory]],
16+
parent_category_id: int) -> List[models.ChildCategory]:
17+
filtered_categories = []
18+
19+
for category in categories:
20+
if hasattr(category, 'parent_category_id') and category.parent_category_id == parent_category_id:
21+
filtered_categories.append(category)
22+
23+
return filtered_categories

aliexpress_api/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
from .affiliate_link import AffiliateLink
55
from .hotproducts import HotProductsResponse
66
from .product import Product
7+
from .category import Category, ChildCategory

aliexpress_api/models/category.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Category:
2+
category_id: int
3+
category_name: str
4+
5+
6+
class ChildCategory(Category):
7+
parent_category_id: int

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='python-aliexpress-api',
8-
version='2.0.2',
8+
version='2.1.0',
99
author='Sergio Abad',
1010
author_email='sergio.abad@bytelix.com',
1111
description='AliExpress API wrapper for Python',

0 commit comments

Comments
 (0)