|
5 | 5 |
|
6 | 6 | import requests
|
7 | 7 | from affluence.line_mappings import line_end_points
|
| 8 | +from ratp_api.models.line import LineData |
8 | 9 |
|
9 | 10 | logging.basicConfig(level=logging.INFO)
|
10 | 11 |
|
@@ -122,15 +123,17 @@ def get_global_traffic(self) -> dict:
|
122 | 123 | """
|
123 | 124 | return self.make_request("GET", self.GLOBAL_TRAFFIC_URL)
|
124 | 125 |
|
125 |
| - def get_line_traffic(self, line_id: LineID) -> dict: |
| 126 | + def get_line_traffic(self, line_id: LineID) -> LineData: |
126 | 127 | """
|
127 | 128 | Retrieve traffic information for a specific line.
|
128 | 129 |
|
129 | 130 | :param line_id: The ID of the line for which traffic information is requested.
|
130 | 131 | :return: A dictionary representing the traffic situation for the specified line.
|
131 | 132 | """
|
132 | 133 | url = self.LINE_TRAFFIC_URL.format(line_id)
|
133 |
| - return self.make_request("GET", url) |
| 134 | + line_data_raw = self.make_request("GET", url) |
| 135 | + line_data = LineData(**line_data_raw) |
| 136 | + return line_data |
134 | 137 |
|
135 | 138 | def get_affluence(self, start, end, line_id):
|
136 | 139 | """
|
@@ -210,7 +213,39 @@ def get_all_lines_affluence(self):
|
210 | 213 |
|
211 | 214 | return affluences
|
212 | 215 |
|
| 216 | + def get_line_affluence(self, line_id: LineID): |
| 217 | + """ |
| 218 | + Retrieve affluence information for a specific line. |
| 219 | +
|
| 220 | + :param line_id: The ID of the line for which affluence information is requested. |
| 221 | + :return: A dictionary containing affluence information, or an empty dict if not available. |
| 222 | + """ |
| 223 | + line_id = str(line_id.name).split("_")[1] |
| 224 | + endpoints = line_end_points.get(line_id) |
| 225 | + if not endpoints: |
| 226 | + logging.info(f"No endpoints found for {line_id}") |
| 227 | + return {} |
| 228 | + try: |
| 229 | + comfort = self.get_affluence( |
| 230 | + start=endpoints["start"]["coords"], |
| 231 | + end=endpoints["end"]["coords"], |
| 232 | + line_id=line_id, |
| 233 | + ) |
| 234 | + if "level" in comfort: |
| 235 | + comfort["lineDisplayCode"] = line_id |
| 236 | + return comfort |
| 237 | + else: |
| 238 | + logging.info(f"No affluence level found for {line_id}") |
| 239 | + except Exception as e: |
| 240 | + logging.info(f"Error fetching affluence for {line_id}: {e}") |
| 241 | + return {} |
| 242 | + |
213 | 243 |
|
214 | 244 | if __name__ == "__main__":
|
215 | 245 | api = RatpAPI(api_key="e2rDkJzd2c1dPaFh7e0pJ9H7NjeqTQHg6ql31LmZ")
|
216 |
| - print(api.get_line_traffic(line_id=LineID.METRO_1)) |
| 246 | + line_data = api.get_line_traffic(line_id=LineID.METRO_14) |
| 247 | + print(line_data) |
| 248 | + for situation in line_data.situations: |
| 249 | + print(situation) |
| 250 | + # print(api.get_line_affluence(line_id=LineID.METRO_9)) |
| 251 | + # print(api.get_all_lines_affluence()) |
0 commit comments