11"""Работа с каталогом"""
22
3- from typing import TYPE_CHECKING , Optional
3+ from typing import TYPE_CHECKING
44
55from human_requests .abstraction import FetchResponse , HttpMethod
66
7+ from ..enums import PurchaseMode
78if TYPE_CHECKING :
8- from ..old import ChizhikAPI
9+ from ..manager import PyaterochkaAPI
910
1011
1112class ClassCatalog :
@@ -15,19 +16,18 @@ class ClassCatalog:
1516 работу с фидами товаров и отзывами.
1617 """
1718
18- def __init__ (self , parent : "ChizhikAPI" , CATALOG_URL : str ):
19- self ._parent : "ChizhikAPI" = parent
20- self .CATALOG_URL : str = CATALOG_URL
19+ def __init__ (self , parent : "PyaterochkaAPI" ):
20+ self ._parent : "PyaterochkaAPI" = parent
2121 self .Product : ProductService = ProductService (
22- parent = self ._parent , CATALOG_URL = CATALOG_URL
22+ parent = self ._parent
2323 )
2424 """Сервис для работы с товарами в каталоге."""
2525
2626 async def tree (self ,
27+ sap_code_store_id : str ,
2728 subcategories : bool = False ,
2829 include_restrict : bool = True ,
29- mode : PurchaseMode = PurchaseMode .STORE ,
30- sap_code_store_id : str = DEFAULT_STORE_ID ) -> FetchResponse :
30+ mode : PurchaseMode = PurchaseMode .STORE ) -> FetchResponse :
3131 """
3232 Asynchronously retrieves a list of categories from the Pyaterochka API.
3333
@@ -44,16 +44,14 @@ async def tree(self,
4444 Exception: If the response status is not 200 (OK) or 403 (Forbidden / Anti-bot).
4545 """
4646
47- request_url = f"{ self .API_URL } /catalog/v2/stores/{ sap_code_store_id } /categories?mode={ mode .value } &include_restrict={ include_restrict } &include_subcategories={ 1 if subcategories else 0 } "
48- _is_success , response , _response_type = await self .api .fetch (url = request_url )
49- return response
47+ 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 } "
48+ return await self ._parent ._request (method = HttpMethod .GET , url = request_url )
5049
5150 async def products_list (
52-
5351 self ,
5452 category_id : str ,
53+ sap_code_store_id : str ,
5554 mode : PurchaseMode = PurchaseMode .STORE ,
56- sap_code_store_id : str = DEFAULT_STORE_ID ,
5755 limit : int = 30
5856 ) -> FetchResponse :
5957 f"""
@@ -76,20 +74,22 @@ async def products_list(
7674 if limit < 1 or limit >= 500 :
7775 raise ValueError ("Limit must be between 1 and 499" )
7876
79- request_url = f"{ self .API_URL } /catalog/v2/stores/{ sap_code_store_id } /categories/{ category_id } /products?mode={ mode .value } &limit={ limit } "
80- _is_success , response , _response_type = await self .api .fetch (url = request_url )
81- return response
77+ request_url = f"{ self ._parent .CATALOG_URL } /catalog/v2/stores/{ sap_code_store_id } /categories/{ category_id } /products?mode={ mode .value } &limit={ limit } "
78+ return await self ._parent ._request (method = HttpMethod .GET , url = request_url )
8279
8380
8481class ProductService :
8582 """Сервис для работы с товарами в каталоге."""
8683
87- def __init__ (self , parent : "ChizhikAPI" , CATALOG_URL : str ):
88- self ._parent : "ChizhikAPI" = parent
89- self .CATALOG_URL : str = CATALOG_URL
84+ def __init__ (self , parent : "PyaterochkaAPI" ):
85+ self ._parent : "PyaterochkaAPI" = parent
9086
9187 async def info (
92- self , plu_id : int
88+ self ,
89+ sap_code_store_id : str ,
90+ plu_id : int ,
91+ mode : PurchaseMode = PurchaseMode .STORE ,
92+ include_restrict : bool = True
9393 ) -> FetchResponse :
9494 """
9595 Asynchronously retrieves product information from the Pyaterochka API for a given PLU ID. Average time processing 2 seconds (first start 6 seconds).
@@ -101,21 +101,6 @@ async def info(
101101 Raises:
102102 ValueError: If the response does not contain the expected JSON data.
103103 """
104-
105- url = f"{ self .BASE_URL } /product/{ plu_id } /"
106- response = await self .api .browser_fetch (url = url , selector = 'script#__NEXT_DATA__[type="application/json"]' )
107-
108- match = re .search (
109- r'<script\s+id="__NEXT_DATA__"\s+type="application/json">(.+?)</script>' ,
110- response ,
111- flags = re .DOTALL
112- )
113- if not match :
114- raise ValueError ("product_info: Failed to find JSON data in the response" )
115- json_text = match .group (1 )
116- data = json .loads (json_text )
117- data ["props" ]["pageProps" ]["props" ]["productStore" ] = json .loads (data ["props" ]["pageProps" ]["props" ]["productStore" ])
118- data ["props" ]["pageProps" ]["props" ]["catalogStore" ] = json .loads (data ["props" ]["pageProps" ]["props" ]["catalogStore" ])
119- data ["props" ]["pageProps" ]["props" ]["filtersPageStore" ] = json .loads (data ["props" ]["pageProps" ]["props" ]["filtersPageStore" ])
120-
121- return data
104+
105+ request_url = f"{ self ._parent .CATALOG_URL } /catalog/v2/stores/{ sap_code_store_id } /products/{ plu_id } ?mode={ mode .value } &include_restrict={ str (include_restrict ).lower ()} "
106+ return await self ._parent ._request (method = HttpMethod .GET , url = request_url )
0 commit comments