| 1 |
"""Asynchronous Python client for the AdGuard Home API.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from dataclasses import dataclass |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
from .exceptions import AdGuardHomeError |
| 9 |
|
| 10 |
if TYPE_CHECKING: |
| 11 |
from . import AdGuardHome |
| 12 |
|
| 13 |
|
| 14 |
@dataclass |
| 15 |
class AdGuardHomeFiltering: |
| 16 |
"""Controls AdGuard Home filtering. Blocks domains.""" |
| 17 |
|
| 18 |
adguard: AdGuardHome |
| 19 |
|
| 20 |
async def _config( |
| 21 |
self, *, enabled: bool | None = None, interval: int | None = None |
| 22 |
) -> None: |
| 23 |
"""Configure filtering on AdGuard Home. |
| 24 |
|
| 25 |
Args: |
| 26 |
---- |
| 27 |
enabled: Enable/Disable AdGuard Home filtering. |
| 28 |
interval: Number of days to keep data in the logs. |
| 29 |
|
| 30 |
""" |
| 31 |
if enabled is None: |
| 32 |
enabled = await self.enabled() |
| 33 |
if interval is None: |
| 34 |
interval = await self.interval() |
| 35 |
|
| 36 |
await self.adguard.request( |
| 37 |
"filtering/config", |
| 38 |
method="POST", |
| 39 |
json_data={"enabled": enabled, "interval": interval}, |
| 40 |
) |
| 41 |
|
| 42 |
async def enabled(self) -> bool: |
| 43 |
"""Return if AdGuard Home filtering is enabled or not. |
| 44 |
|
| 45 |
Returns |
| 46 |
------- |
| 47 |
The current state of the AdGuard Home filtering. |
| 48 |
|
| 49 |
""" |
| 50 |
response = await self.adguard.request("filtering/status") |
| 51 |
return response["enabled"] |
| 52 |
|
| 53 |
async def enable(self) -> None: |
| 54 |
"""Enable AdGuard Home filtering. |
| 55 |
|
| 56 |
Raises |
| 57 |
------ |
| 58 |
AdGuardHomeError: If enabling the filtering didn't succeed. |
| 59 |
|
| 60 |
""" |
| 61 |
try: |
| 62 |
await self._config(enabled=True) |
| 63 |
except AdGuardHomeError as exception: |
| 64 |
msg = "Enabling AdGuard Home filtering failed" |
| 65 |
raise AdGuardHomeError(msg) from exception |
| 66 |
|
| 67 |
async def disable(self) -> None: |
| 68 |
"""Disable AdGuard Home filtering. |
| 69 |
|
| 70 |
Raises |
| 71 |
------ |
| 72 |
AdGuardHomeError: If disabling the filtering didn't succeed. |
| 73 |
|
| 74 |
""" |
| 75 |
try: |
| 76 |
await self._config(enabled=False) |
| 77 |
except AdGuardHomeError as exception: |
| 78 |
msg = "Disabling AdGuard Home filtering failed" |
| 79 |
raise AdGuardHomeError(msg) from exception |
| 80 |
|
| 81 |
async def interval(self, *, interval: int | None = None) -> int: |
| 82 |
"""Return or set the time period to keep query log data. |
| 83 |
|
| 84 |
Args: |
| 85 |
---- |
| 86 |
interval: Set the time period (in days) to keep query log data. |
| 87 |
|
| 88 |
Returns: |
| 89 |
------- |
| 90 |
The current set time period to keep query log data. |
| 91 |
|
| 92 |
""" |
| 93 |
if interval: |
| 94 |
await self._config(interval=interval) |
| 95 |
return interval |
| 96 |
|
| 97 |
response = await self.adguard.request("filtering/status") |
| 98 |
return response["interval"] |
| 99 |
|
| 100 |
async def rules_count(self, *, allowlist: bool) -> int: |
| 101 |
"""Return the number of rules loaded. |
| 102 |
|
| 103 |
Args: |
| 104 |
---- |
| 105 |
allowlist: True to get the allowlists count, False for the blocklists. |
| 106 |
|
| 107 |
Returns: |
| 108 |
------- |
| 109 |
The number of filtering rules currently loaded in the AdGuard |
| 110 |
Home instance. |
| 111 |
|
| 112 |
""" |
| 113 |
response = await self.adguard.request("filtering/status") |
| 114 |
|
| 115 |
count = "whitelist_filters" if allowlist else "filters" |
| 116 |
if not response.get(count): |
| 117 |
return 0 |
| 118 |
|
| 119 |
return sum(fil["rules_count"] for fil in response[count]) |
| 120 |
|
| 121 |
async def add_url(self, *, allowlist: bool, name: str, url: str) -> None: |
| 122 |
"""Add a new filter subscription to AdGuard Home. |
| 123 |
|
| 124 |
Args: |
| 125 |
---- |
| 126 |
allowlist: True to add an allowlist, False for a blocklists. |
| 127 |
name: The name of the filter subscription. |
| 128 |
url: The URL of the filter list. |
| 129 |
|
| 130 |
Raises: |
| 131 |
------ |
| 132 |
AdGuardHomeError: Failed adding the filter subscription. |
| 133 |
|
| 134 |
""" |
| 135 |
try: |
| 136 |
await self.adguard.request( |
| 137 |
"filtering/add_url", |
| 138 |
method="POST", |
| 139 |
json_data={"whitelist": allowlist, "name": name, "url": url}, |
| 140 |
) |
| 141 |
except AdGuardHomeError as exception: |
| 142 |
msg = "Failed adding URL to AdGuard Home filter" |
| 143 |
raise AdGuardHomeError(msg) from exception |
| 144 |
|
| 145 |
async def remove_url(self, *, allowlist: bool, url: str) -> None: |
| 146 |
"""Remove a new filter subscription from AdGuard Home. |
| 147 |
|
| 148 |
Args: |
| 149 |
---- |
| 150 |
allowlist: True to remove an allowlist, False for a blocklists. |
| 151 |
url: Filter subscription URL to remove from AdGuard Home. |
| 152 |
|
| 153 |
Raises: |
| 154 |
------ |
| 155 |
AdGuardHomeError: Failed removing the filter subscription. |
| 156 |
|
| 157 |
""" |
| 158 |
try: |
| 159 |
await self.adguard.request( |
| 160 |
"filtering/remove_url", |
| 161 |
method="POST", |
| 162 |
json_data={"whitelist": allowlist, "url": url}, |
| 163 |
) |
| 164 |
except AdGuardHomeError as exception: |
| 165 |
msg = "Failed removing URL from AdGuard Home filter" |
| 166 |
raise AdGuardHomeError(msg) from exception |
| 167 |
|
| 168 |
async def enable_url(self, *, allowlist: bool, url: str) -> None: |
| 169 |
"""Enable a filter subscription in AdGuard Home. |
| 170 |
|
| 171 |
Args: |
| 172 |
---- |
| 173 |
allowlist: True to enable an allowlist, False for a blocklists. |
| 174 |
url: Filter subscription URL to enable on AdGuard Home. |
| 175 |
|
| 176 |
Raises: |
| 177 |
------ |
| 178 |
AdGuardHomeError: Failed enabling filter subscription. |
| 179 |
|
| 180 |
""" |
| 181 |
response = await self.adguard.request("filtering/status") |
| 182 |
filter_type = "whitelist_filters" if allowlist else "filters" |
| 183 |
|
| 184 |
# Excluded from coverage: |
| 185 |
# https://github.com/nedbat/coveragepy/issues/515 |
| 186 |
name = next( # pragma: no cover |
| 187 |
( |
| 188 |
fil["name"] |
| 189 |
for fil in response[filter_type] |
| 190 |
if fil["url"].lower() == url.lower() |
| 191 |
), |
| 192 |
"Unknown", |
| 193 |
) |
| 194 |
|
| 195 |
try: |
| 196 |
await self.adguard.request( |
| 197 |
"filtering/set_url", |
| 198 |
method="POST", |
| 199 |
json_data={ |
| 200 |
"url": url, |
| 201 |
"whitelist": allowlist, |
| 202 |
"data": {"enabled": True, "name": name, "url": url}, |
| 203 |
}, |
| 204 |
) |
| 205 |
except AdGuardHomeError as exception: |
| 206 |
msg = "Failed enabling URL on AdGuard Home filter" |
| 207 |
raise AdGuardHomeError(msg) from exception |
| 208 |
|
| 209 |
async def disable_url(self, *, allowlist: bool, url: str) -> None: |
| 210 |
"""Disable a filter subscription in AdGuard Home. |
| 211 |
|
| 212 |
Args: |
| 213 |
---- |
| 214 |
url: Filter subscription URL to disable on AdGuard Home. |
| 215 |
allowlist: True to update the allowlists, False for the blocklists. |
| 216 |
|
| 217 |
Raises: |
| 218 |
------ |
| 219 |
AdGuardHomeError: Failed disabling filter subscription. |
| 220 |
|
| 221 |
""" |
| 222 |
response = await self.adguard.request("filtering/status") |
| 223 |
filter_type = "whitelist_filters" if allowlist else "filters" |
| 224 |
|
| 225 |
# Excluded from coverage: |
| 226 |
# https://github.com/nedbat/coveragepy/issues/515 |
| 227 |
name = next( # pragma: no cover |
| 228 |
( |
| 229 |
fil["name"] |
| 230 |
for fil in response[filter_type] |
| 231 |
if fil["url"].lower() == url.lower() |
| 232 |
), |
| 233 |
"Unknown", |
| 234 |
) |
| 235 |
|
| 236 |
try: |
| 237 |
await self.adguard.request( |
| 238 |
"filtering/set_url", |
| 239 |
method="POST", |
| 240 |
json_data={ |
| 241 |
"url": url, |
| 242 |
"whitelist": allowlist, |
| 243 |
"data": {"enabled": False, "name": name, "url": url}, |
| 244 |
}, |
| 245 |
) |
| 246 |
except AdGuardHomeError as exception: |
| 247 |
msg = "Failed disabling URL on AdGuard Home filter" |
| 248 |
raise AdGuardHomeError(msg) from exception |
| 249 |
|
| 250 |
async def refresh(self, *, allowlist: bool, force: bool = False) -> None: |
| 251 |
"""Reload filtering subscriptions from URLs specified in AdGuard Home. |
| 252 |
|
| 253 |
Args: |
| 254 |
---- |
| 255 |
force: Force the reload of all filter subscriptions. |
| 256 |
allowlist: True to update the allowlists, False for the blocklists. |
| 257 |
|
| 258 |
Raises: |
| 259 |
------ |
| 260 |
AdGuardHomeError: Failed to refresh filter subscriptions. |
| 261 |
|
| 262 |
""" |
| 263 |
force_value = "true" if force else "false" |
| 264 |
|
| 265 |
try: |
| 266 |
await self.adguard.request( |
| 267 |
"filtering/refresh", |
| 268 |
method="POST", |
| 269 |
json_data={"whitelist": allowlist}, |
| 270 |
params={"force": force_value}, |
| 271 |
) |
| 272 |
except AdGuardHomeError as exception: |
| 273 |
msg = "Failed refreshing filter URLs in AdGuard Home" |
| 274 |
raise AdGuardHomeError(msg) from exception |