| 1 |
"""Basic AEMET OpenData client example.""" |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import timeit |
| 5 |
|
| 6 |
from _common import json_dumps |
| 7 |
from _secrets import AEMET_COORDS, AEMET_OPTIONS |
| 8 |
import aiohttp |
| 9 |
|
| 10 |
from aemet_opendata.exceptions import ApiError, AuthError, TooManyRequests, TownNotFound |
| 11 |
from aemet_opendata.interface import AEMET |
| 12 |
|
| 13 |
|
| 14 |
async def main(): |
| 15 |
"""AEMET OpenData client example.""" |
| 16 |
|
| 17 |
async with aiohttp.ClientSession() as aiohttp_session: |
| 18 |
client = AEMET(aiohttp_session, AEMET_OPTIONS) |
| 19 |
|
| 20 |
try: |
| 21 |
select_start = timeit.default_timer() |
| 22 |
await client.select_coordinates(AEMET_COORDS[0], AEMET_COORDS[1]) |
| 23 |
select_end = timeit.default_timer() |
| 24 |
print(json_dumps(client.data())) |
| 25 |
print(f"Select time: {select_end - select_start}") |
| 26 |
print("***") |
| 27 |
|
| 28 |
update_start = timeit.default_timer() |
| 29 |
await client.update() |
| 30 |
update_end = timeit.default_timer() |
| 31 |
print(json_dumps(client.data())) |
| 32 |
print(f"Update time: {update_end - update_start}") |
| 33 |
except ApiError: |
| 34 |
print("API data error") |
| 35 |
except AuthError: |
| 36 |
print("API authentication error.") |
| 37 |
except TooManyRequests: |
| 38 |
print("Too many requests.") |
| 39 |
except TownNotFound: |
| 40 |
print("Town not found.") |
| 41 |
|
| 42 |
|
| 43 |
if __name__ == "__main__": |
| 44 |
asyncio.run(main()) |