File: get-town-by-coords.py

package info (click to toggle)
python-aemet-opendata 0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 228 kB
  • sloc: python: 1,731; makefile: 4
file content (33 lines) | stat: -rw-r--r-- 915 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Get AEMET OpenData town data by coords example."""

import asyncio
import timeit

from _common import json_dumps
from _secrets import AEMET_COORDS, AEMET_OPTIONS
import aiohttp

from aemet_opendata.exceptions import AuthError
from aemet_opendata.interface import AEMET


async def main():
    """AEMET OpenData client example."""

    async with aiohttp.ClientSession() as aiohttp_session:
        client = AEMET(aiohttp_session, AEMET_OPTIONS)

        try:
            get_town_start = timeit.default_timer()
            town = await client.get_town_by_coordinates(
                AEMET_COORDS[0], AEMET_COORDS[1]
            )
            get_town_end = timeit.default_timer()
            print(json_dumps(town))
            print(f"Get Town time: {get_town_end - get_town_start}")
        except AuthError:
            print("API authentication error.")


if __name__ == "__main__":
    asyncio.run(main())