|
1 | 1 | """API to DGEG.""" |
2 | | -from typing import List, Optional, Dict |
| 2 | +from typing import Dict, Optional |
| 3 | +from datetime import datetime |
3 | 4 | from aiohttp import ClientSession |
4 | 5 | import logging |
5 | 6 |
|
@@ -82,16 +83,24 @@ class DGEG: |
82 | 83 | def __init__(self, websession): |
83 | 84 | self.websession = websession |
84 | 85 |
|
85 | | - async def list_stations(self) -> List[Dict]: |
| 86 | + async def list_stations(self) -> list[Dict]: |
86 | 87 | """Get list of all stations.""" |
87 | 88 | try: |
88 | | - async with self._session.get(API_STATIONS_LIST) as response: |
89 | | - if response.status == 200: |
90 | | - stations = await response.json() |
| 89 | + async with self.websession.get(API_STATIONS_LIST) as res: |
| 90 | + if res.status == 200: |
| 91 | + json = await res.json() |
91 | 92 | # Sort stations by name for better display |
92 | | - return sorted(stations, key=lambda x: x.get('Nome', '').lower()) |
| 93 | + return sorted( |
| 94 | + json['resultado'], |
| 95 | + key=lambda x: ( |
| 96 | + x.get('Distrito', '').lower(), |
| 97 | + x.get('Localidade', '').lower(), |
| 98 | + x.get('Marca', '').lower(), |
| 99 | + x.get('Nome', '').lower() |
| 100 | + ) |
| 101 | + ) |
93 | 102 | else: |
94 | | - _LOGGER.error("Failed to fetch stations list. Status: %s", response.status) |
| 103 | + _LOGGER.error("Failed to fetch stations list. Status: %s", res.status) |
95 | 104 | return [] |
96 | 105 | except Exception as ex: |
97 | 106 | _LOGGER.error("Error fetching stations list: %s", str(ex)) |
|
0 commit comments