Debian Codegraph @

examples/get-town-by-coords-precise.py

raw
1 """Get AEMET OpenData town data by coords 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 AuthError
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 client.distance_high_precision(True)
21
22 try:
23 get_town_start = timeit.default_timer()
24 town = await client.get_town_by_coordinates(
25 AEMET_COORDS[0], AEMET_COORDS[1]
26 )
27 get_town_end = timeit.default_timer()
28 print(json_dumps(town))
29 print(f"Get Town time: {get_town_end - get_town_start}")
30 except AuthError:
31 print("API authentication error.")
32
33
34 if __name__ == "__main__":
35 asyncio.run(main())