Skip to content

Commit 35619a8

Browse files
committed
complite catalog
1 parent c96cdb8 commit 35619a8

3 files changed

Lines changed: 62 additions & 5 deletions

File tree

main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from pyaterochka_api import PyaterochkaAPI
2+
from pprint import pprint
23

34
async def main():
45
# RUS: Использование проксирования опционально. Вы можете создать несколько агентов с разными прокси для ускорения парса.
56
# ENG: Proxy usage is optional. You can create multiple agents with different proxies for faster parsing.
67
async with PyaterochkaAPI(headless=False) as API:
78
# RUS: Выводит активные предложения магазина
89
# ENG: Outputs active offers of the store
9-
print(f"Active offers output: {(await API.Catalog.tree(sap_code_store_id="35XY")).json()!s:.100s}...\n")
10+
pprint((await API.Catalog.tree(sap_code_store_id="35XY", subcategories=False)).json())
1011

1112
import asyncio
1213
asyncio.run(main())

pyaterochka_api/endpoints/catalog.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""Работа с каталогом"""
22

3-
from typing import TYPE_CHECKING
4-
3+
from typing import TYPE_CHECKING, Optional
4+
import urllib.parse
55
from human_requests.abstraction import FetchResponse, HttpMethod
66

7-
from ..enums import PurchaseMode
7+
from ..enums import PurchaseMode, Sorting
88
if TYPE_CHECKING:
99
from ..manager import PyaterochkaAPI
1010

@@ -47,10 +47,34 @@ async def tree(self,
4747
request_url = f"{self._parent.CATALOG_URL}/catalog/v2/stores/{sap_code_store_id}/categories?mode={mode.value}&include_restrict={include_restrict}&include_subcategories={1 if subcategories else 0}"
4848
return await self._parent._request(method=HttpMethod.GET, url=request_url)
4949

50+
async def tree_extended(self,
51+
sap_code_store_id: str,
52+
category_id: str,
53+
include_restrict: bool = True,
54+
mode: PurchaseMode = PurchaseMode.STORE) -> FetchResponse:
55+
"""Расширенное представление категории и её подкатегорий."""
56+
request_url = f"{self._parent.CATALOG_URL}/api/catalog/v2/stores/{sap_code_store_id}/categories/{category_id}/extended?mode={mode.value}&include_restrict={str(include_restrict).lower()}"
57+
return await self._parent._request(method=HttpMethod.GET, url=request_url)
58+
59+
async def search(self,
60+
sap_code_store_id: str,
61+
query: str,
62+
include_restrict: bool = True,
63+
mode: PurchaseMode = PurchaseMode.STORE,
64+
limit: int = 12) -> FetchResponse:
65+
"""Поиск по товарам И категориям."""
66+
q = urllib.parse.quote(query)
67+
request_url = f"{self._parent.CATALOG_URL}/api/catalog/v3/stores/{sap_code_store_id}/search?mode={mode.value}&include_restrict={str(include_restrict).lower()}&q={q}&limit={limit}"
68+
return await self._parent._request(method=HttpMethod.GET, url=request_url)
69+
5070
async def products_list(
5171
self,
5272
category_id: str,
5373
sap_code_store_id: str,
74+
price_min: Optional[int] = None,
75+
price_max: Optional[int] = None,
76+
brands: list[str] = [],
77+
include_restrict: bool = True,
5478
mode: PurchaseMode = PurchaseMode.STORE,
5579
limit: int = 30
5680
) -> FetchResponse:
@@ -74,7 +98,27 @@ async def products_list(
7498
if limit < 1 or limit >= 500:
7599
raise ValueError("Limit must be between 1 and 499")
76100

77-
request_url = f"{self._parent.CATALOG_URL}/catalog/v2/stores/{sap_code_store_id}/categories/{category_id}/products?mode={mode.value}&limit={limit}"
101+
request_url = f"{self._parent.CATALOG_URL}/catalog/v2/stores/{sap_code_store_id}/categories/{category_id}/products?mode={mode.value}&limit={limit}&include_restrict={str(include_restrict).lower()}"
102+
if price_min:
103+
request_url += "&price_min="+str(price_min)
104+
if price_max:
105+
request_url += "&price_max="+str(price_max)
106+
if len(brands) > 0:
107+
encoded_brands = [f'brands={urllib.parse.quote(brand)}' for brand in brands]
108+
request_url += "&" + '&&'.join(encoded_brands)
109+
110+
return await self._parent._request(method=HttpMethod.GET, url=request_url)
111+
112+
async def products_line(
113+
self,
114+
category_id: str,
115+
sap_code_store_id: str,
116+
include_restrict: bool = True,
117+
mode: PurchaseMode = PurchaseMode.STORE,
118+
order_by: Sorting = Sorting.POPULARITY
119+
) -> FetchResponse:
120+
"""Рекомендованные товары \"что интересного?\"."""
121+
request_url = f"https://5d.5ka.ru/api/catalog/v1/stores/{sap_code_store_id}/categories/{category_id}/products_line?mode={mode.value}&include_restrict={str(include_restrict).lower()}&order_by={order_by.value}"
78122
return await self._parent._request(method=HttpMethod.GET, url=request_url)
79123

80124

pyaterochka_api/enums.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,15 @@
44
class PurchaseMode(Enum):
55
STORE = "store"
66
DELIVERY = "delivery"
7+
8+
class Sorting(Enum):
9+
PRICE_DESC = "price_desc"
10+
"""Сначала дороже"""
11+
PRICE_ASC = "price_asc"
12+
"""Сначала дешевле"""
13+
DISCOUNT_DESC = "discount_desc"
14+
"""По размеру скидки"""
15+
RATING_DESC = "rating_desc"
16+
"""Сначала с высоким рейтингом"""
17+
POPULARITY = "popularity"
18+
"""Сначала популярные"""

0 commit comments

Comments
 (0)