58 lines
1.5 KiB
Python
58 lines
1.5 KiB
Python
"""Lectura segura de datos ASUSWRT mediante la API web del router."""
|
|
|
|
import asyncio
|
|
import json
|
|
import os
|
|
|
|
import aiohttp
|
|
from asusrouter import AsusData, AsusRouter
|
|
|
|
|
|
def _load_env():
|
|
values = {}
|
|
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env")
|
|
try:
|
|
with open(path) as stream:
|
|
for line in stream:
|
|
line = line.strip()
|
|
if line and not line.startswith("#") and "=" in line:
|
|
key, value = line.split("=", 1)
|
|
values[key.strip()] = value.strip()
|
|
except OSError:
|
|
pass
|
|
return values
|
|
|
|
|
|
FILE_ENV = _load_env()
|
|
|
|
|
|
def _env(name, default=""):
|
|
return os.getenv(name, FILE_ENV.get(name, default)).strip()
|
|
|
|
|
|
async def _parental_data():
|
|
username = _env("ASUS_USERNAME")
|
|
password = _env("ASUS_PASSWORD")
|
|
if not username or not password:
|
|
raise RuntimeError(
|
|
"Falta ASUS_USERNAME o ASUS_PASSWORD en el .env de la Raspberry"
|
|
)
|
|
session = aiohttp.ClientSession()
|
|
router = AsusRouter(
|
|
hostname=_env("ASUS_ROUTER_IP", "192.168.50.1"),
|
|
username=username,
|
|
password=password,
|
|
session=session,
|
|
)
|
|
try:
|
|
await router.async_connect()
|
|
return await router.async_get_data(AsusData.PARENTAL_CONTROL, force=True)
|
|
finally:
|
|
await router.async_disconnect()
|
|
await session.close()
|
|
|
|
|
|
def parental_status():
|
|
data = asyncio.run(_parental_data())
|
|
return json.dumps(data, ensure_ascii=False, indent=2, default=str)
|