chizhik-api 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chizhik_api-0.1.0/LICENSE +21 -0
- chizhik_api-0.1.0/PKG-INFO +17 -0
- chizhik_api-0.1.0/README.md +1 -0
- chizhik_api-0.1.0/chizhik_api/__init__.py +3 -0
- chizhik_api-0.1.0/chizhik_api/api.py +142 -0
- chizhik_api-0.1.0/chizhik_api/manager.py +24 -0
- chizhik_api-0.1.0/chizhik_api.egg-info/PKG-INFO +17 -0
- chizhik_api-0.1.0/chizhik_api.egg-info/SOURCES.txt +11 -0
- chizhik_api-0.1.0/chizhik_api.egg-info/dependency_links.txt +1 -0
- chizhik_api-0.1.0/chizhik_api.egg-info/requires.txt +3 -0
- chizhik_api-0.1.0/chizhik_api.egg-info/top_level.txt +1 -0
- chizhik_api-0.1.0/setup.cfg +4 -0
- chizhik_api-0.1.0/setup.py +23 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Open Inflation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: chizhik_api
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python API client for Chizhik catalog
|
|
5
|
+
Home-page: https://github.com/Open-Inflation/chizhik_api
|
|
6
|
+
Author: Miskler
|
|
7
|
+
License: UNKNOWN
|
|
8
|
+
Platform: UNKNOWN
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
|
|
16
|
+
# chizhik_api
|
|
17
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# chizhik_api
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import aiohttp
|
|
2
|
+
from playwright.async_api import async_playwright
|
|
3
|
+
from playwright_stealth import stealth_async
|
|
4
|
+
import json
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ChizhikAPI:
|
|
8
|
+
def __init__(self, debug: bool = False, cookies: dict = {}):
|
|
9
|
+
self.debug = debug
|
|
10
|
+
self.cookies = cookies
|
|
11
|
+
self.user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
|
|
12
|
+
self.session_dict = {
|
|
13
|
+
"User-Agent": self.user_agent,
|
|
14
|
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
|
15
|
+
"Accept-Language": "en-GB,en;q=0.5",
|
|
16
|
+
"Accept-Encoding": "gzip, deflate, br, zstd",
|
|
17
|
+
"Connection": "keep-alive",
|
|
18
|
+
"Upgrade-Insecure-Requests": "1",
|
|
19
|
+
"Sec-Fetch-Dest": "document",
|
|
20
|
+
"Sec-Fetch-Mode": "navigate",
|
|
21
|
+
"Sec-Fetch-Site": "none",
|
|
22
|
+
"Sec-Fetch-User": "?1"
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async def _launch_browser(self, url: str) -> dict:
|
|
26
|
+
"""
|
|
27
|
+
Asynchronously launches a browser using the Playwright API to fetch data from a given URL.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
url (str): The URL to fetch data from.
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
dict: A dictionary containing the fetched data.
|
|
34
|
+
"""
|
|
35
|
+
# Используем асинхронный Playwright API
|
|
36
|
+
async with async_playwright() as p:
|
|
37
|
+
if self.debug: print("Launching browser...")
|
|
38
|
+
browser = await p.chromium.launch(headless=True) # Включите headless=False для визуального интерфейса
|
|
39
|
+
context = await browser.new_context(
|
|
40
|
+
user_agent=self.user_agent,
|
|
41
|
+
no_viewport=True
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
if self.debug: print("Creating page...")
|
|
45
|
+
page = await context.new_page()
|
|
46
|
+
|
|
47
|
+
# Применяем Stealth-режим
|
|
48
|
+
if self.debug: print("Applying Stealth mode...")
|
|
49
|
+
await stealth_async(page)
|
|
50
|
+
|
|
51
|
+
if self.debug: print("Rendering page...")
|
|
52
|
+
await page.goto(url, wait_until="networkidle")
|
|
53
|
+
|
|
54
|
+
if self.debug: print("Fetching cookies...")
|
|
55
|
+
output_cookies = {}
|
|
56
|
+
for cookie in await context.cookies():
|
|
57
|
+
output_cookies[cookie["name"]] = cookie["value"]
|
|
58
|
+
self.cookies = output_cookies
|
|
59
|
+
|
|
60
|
+
if self.debug: print("Fetching HTML...")
|
|
61
|
+
content = await page.content()
|
|
62
|
+
|
|
63
|
+
if self.debug: print("Closing browser...")
|
|
64
|
+
await browser.close()
|
|
65
|
+
|
|
66
|
+
if self.debug: print("Parsing HTML...")
|
|
67
|
+
real_output = json.loads(
|
|
68
|
+
content.\
|
|
69
|
+
removeprefix('<html><head><meta name="color-scheme" content="light dark"><meta charset="utf-8"></head><body><pre>').\
|
|
70
|
+
removesuffix('</pre><div class="json-formatter-container"></div></body></html>')
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
if self.debug: print("Done!\n")
|
|
74
|
+
return real_output
|
|
75
|
+
|
|
76
|
+
async def _fetch(self, url: str) -> tuple[bool, dict]:
|
|
77
|
+
"""
|
|
78
|
+
Asynchronously fetches data from the specified URL using aiohttp.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
url (str): The URL to fetch data from.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
tuple[bool, dict]: A tuple containing a boolean indicating the success of the fetch and a dictionary containing the response data.
|
|
85
|
+
- If the response content type is 'text/html', the function returns (False, {}).
|
|
86
|
+
- If the response content type is 'application/json', the function returns (True, response.json()).
|
|
87
|
+
|
|
88
|
+
Raises:
|
|
89
|
+
aiohttp.ClientError: If there was an error connecting to the server.
|
|
90
|
+
Exception: If the response content type is unknown or the response status is 403 (Forbidden) or any other unknown error/status code.
|
|
91
|
+
"""
|
|
92
|
+
async with aiohttp.ClientSession() as session:
|
|
93
|
+
if self.debug: print(f"Requesting \"{url}\"... Cookies: {self.cookies}")
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
async with session.get(
|
|
98
|
+
url=url,
|
|
99
|
+
headers=self.session_dict,
|
|
100
|
+
cookies=self.cookies
|
|
101
|
+
) as response:
|
|
102
|
+
if response.status == 200: # 200 OK
|
|
103
|
+
if self.debug:
|
|
104
|
+
print(f"Response status: {response.status}, response type: {response.headers['content-type']}")
|
|
105
|
+
|
|
106
|
+
if response.headers['content-type'].startswith('text/html'):
|
|
107
|
+
return False, {}
|
|
108
|
+
elif response.headers['content-type'].startswith('application/json'):
|
|
109
|
+
return True, await response.json()
|
|
110
|
+
else:
|
|
111
|
+
raise Exception(f"Unknown response type: {response.headers['content-type']}")
|
|
112
|
+
elif response.status == 403: # 403 Forbidden (сервер воспринял как бота)
|
|
113
|
+
raise Exception("Anti-bot protection. Use Russia IP address and try again.")
|
|
114
|
+
else:
|
|
115
|
+
raise Exception(f"Response status: {response.status} (unknown error/status code). Please, create issue on GitHub")
|
|
116
|
+
|
|
117
|
+
async def request(self, url: str) -> dict:
|
|
118
|
+
"""
|
|
119
|
+
Asynchronously sends a request to the specified URL using the playwright and aiohttp.
|
|
120
|
+
|
|
121
|
+
The function automatically selects a method for sending data based on whether cookies are available.
|
|
122
|
+
If cookies are available, aiohttp is used as the priority method.
|
|
123
|
+
If no cookies are present, the page is opened in a full browser using playwright to create cookies (which takes longer).
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
url (str): The URL to send the request to.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
dict: A dictionary containing the response data.
|
|
130
|
+
"""
|
|
131
|
+
if len(self.cookies) > 0:
|
|
132
|
+
response_ok, response = await self._fetch(url=url)
|
|
133
|
+
if not response_ok:
|
|
134
|
+
if self.debug: print('Unable to fetch categories list, start browser...')
|
|
135
|
+
# Если получен HTML, переходим в браузер
|
|
136
|
+
return await self._launch_browser(url=url) # возвращаем результат из браузера
|
|
137
|
+
else:
|
|
138
|
+
if self.debug: print('Categories list fetched successfully!\n')
|
|
139
|
+
return response
|
|
140
|
+
else:
|
|
141
|
+
if self.debug: print('No cookies found, start browser (maybe first start)...')
|
|
142
|
+
return await self._launch_browser(url=url)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from .api import ChizhikAPI
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
CATALOG_URL = "https://app.chizhik.club/api/v1"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Chizhik = ChizhikAPI(debug=False)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
async def categories_list(city_id: str = None) -> dict:
|
|
11
|
+
url = f"{CATALOG_URL}/catalog/unauthorized/categories/"
|
|
12
|
+
if city_id: url += f"?city_id={city_id}"
|
|
13
|
+
return await Chizhik.request(url)
|
|
14
|
+
|
|
15
|
+
async def products_list(category_id: int, page: int = 1, city_id: str = None) -> dict:
|
|
16
|
+
url = f"{CATALOG_URL}/catalog/unauthorized/products/?page={page}&category_id={category_id}"
|
|
17
|
+
if city_id: url += f"&city_id={city_id}"
|
|
18
|
+
return await Chizhik.request(url)
|
|
19
|
+
|
|
20
|
+
async def cities_list(search_name: str, page: int = 1) -> dict:
|
|
21
|
+
return await Chizhik.request(f"{CATALOG_URL}/geo/cities/?name={search_name}&page={page}")
|
|
22
|
+
|
|
23
|
+
async def active_inout() -> dict:
|
|
24
|
+
return await Chizhik.request(f"{CATALOG_URL}/catalog/unauthorized/active_inout/")
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: chizhik-api
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python API client for Chizhik catalog
|
|
5
|
+
Home-page: https://github.com/Open-Inflation/chizhik_api
|
|
6
|
+
Author: Miskler
|
|
7
|
+
License: UNKNOWN
|
|
8
|
+
Platform: UNKNOWN
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
|
|
16
|
+
# chizhik_api
|
|
17
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
setup.py
|
|
4
|
+
chizhik_api/__init__.py
|
|
5
|
+
chizhik_api/api.py
|
|
6
|
+
chizhik_api/manager.py
|
|
7
|
+
chizhik_api.egg-info/PKG-INFO
|
|
8
|
+
chizhik_api.egg-info/SOURCES.txt
|
|
9
|
+
chizhik_api.egg-info/dependency_links.txt
|
|
10
|
+
chizhik_api.egg-info/requires.txt
|
|
11
|
+
chizhik_api.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
chizhik_api
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name='chizhik_api',
|
|
5
|
+
version='0.1.0',
|
|
6
|
+
packages=find_packages(),
|
|
7
|
+
install_requires=[
|
|
8
|
+
'aiohttp',
|
|
9
|
+
'playwright',
|
|
10
|
+
'playwright_stealth',
|
|
11
|
+
],
|
|
12
|
+
author='Miskler',
|
|
13
|
+
description='A Python API client for Chizhik catalog',
|
|
14
|
+
long_description=open('README.md').read(),
|
|
15
|
+
long_description_content_type='text/markdown',
|
|
16
|
+
url='https://github.com/Open-Inflation/chizhik_api',
|
|
17
|
+
classifiers=[
|
|
18
|
+
'Programming Language :: Python :: 3',
|
|
19
|
+
'License :: OSI Approved :: MIT License',
|
|
20
|
+
'Operating System :: OS Independent',
|
|
21
|
+
],
|
|
22
|
+
python_requires='>=3.10',
|
|
23
|
+
)
|