36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
import aiohttp
|
||
import json
|
||
|
||
LOCKS_API_URL = "https://papi.dataekb.ru/get_pacs"
|
||
|
||
|
||
async def fetch_locks() -> dict:
|
||
async with aiohttp.ClientSession() as session:
|
||
try:
|
||
async with session.get(LOCKS_API_URL) as response:
|
||
if response.status != 200:
|
||
print(f"Ошибка при получении данных замков: {response.status}")
|
||
return {}
|
||
text = await response.text()
|
||
except Exception as e:
|
||
print(f"Исключение при запросе данных замков: {str(e)}")
|
||
return {}
|
||
try:
|
||
data = json.loads(text)
|
||
if not data or not isinstance(data, list):
|
||
print("Неверный формат данных о замках, ожидался список.")
|
||
return {}
|
||
locks_dict = data[0]
|
||
return locks_dict
|
||
except Exception as e:
|
||
print(f"Ошибка при разборе данных замков: {str(e)}")
|
||
return {}
|
||
|
||
|
||
async def get_lock_by_label(label: str) -> dict:
|
||
locks = await fetch_locks()
|
||
for lock_id, details in locks.items():
|
||
if label == lock_id or details.get("name") == label:
|
||
return details
|
||
return {}
|