Skip to content

Commit 5a1935b

Browse files
committed
feat: pydantic
1 parent ec97903 commit 5a1935b

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ RatpAPI is a Python wrapper for interacting with the RATP (Régie Autonome des T
77
- Fetch global traffic information on the RATP network.
88
- Retrieve traffic information for specific lines using the `LineID` enum for easy reference.
99
- Get affluence data for journeys on particular lines.
10+
- Utilizes Pydantic models for data validation and parsing.
11+
1012

1113
## Installation
1214
To use RatpAPI in your project, you can install it via pip:
@@ -15,7 +17,7 @@ pip install ratp-api-python
1517
```
1618

1719
## Usage
18-
20+
a
1921
### Setting Up
2022
First, import the `RatpAPI` class and initialize it with your API key:
2123

@@ -44,8 +46,14 @@ print(global_traffic)
4446
### Fetching Line-Specific Traffic
4547
To get traffic information for a specific line using the `LineID` enum:
4648
```python
47-
line_traffic = api.get_line_traffic(LineID.RER_A)
48-
print(line_traffic)
49+
line_data = api.get_line_traffic(line_id=LineID.METRO_14)
50+
for situation in line_data.situations:
51+
print(situation)
52+
# Output:
53+
# isActive=True isPlanned=True criticity='HIGH' messages=["Jusqu'au 04/02, le week-end, trafic interrompu sur l'ensemble de la ligne en raison de travaux. Bus de remplacement. Plus d'informations sur la page dédiée."]
54+
# isActive=False isPlanned=True criticity='HIGH' messages=["Du 01/02 au 08/02, du lundi au jeudi à partir de 22h, trafic interrompu sur l'ensemble de la ligne en raison de travaux. Bus de remplacement. Plus d'informations sur la page dédiée."]
55+
# isActive=False isPlanned=True criticity='HIGH' messages=["Jusqu'au 31/01, du lundi au jeudi à partir de 22h, trafic interrompu sur l'ensemble de la ligne en raison de travaux. Bus de remplacement. Plus d'informations sur la page dédiée."]
56+
4957
```
5058

5159
### Fetching Affluence for Journeys

src/ratp_api/main.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import requests
77
from affluence.line_mappings import line_end_points
8+
from ratp_api.models.line import LineData
89

910
logging.basicConfig(level=logging.INFO)
1011

@@ -122,15 +123,17 @@ def get_global_traffic(self) -> dict:
122123
"""
123124
return self.make_request("GET", self.GLOBAL_TRAFFIC_URL)
124125

125-
def get_line_traffic(self, line_id: LineID) -> dict:
126+
def get_line_traffic(self, line_id: LineID) -> LineData:
126127
"""
127128
Retrieve traffic information for a specific line.
128129
129130
:param line_id: The ID of the line for which traffic information is requested.
130131
:return: A dictionary representing the traffic situation for the specified line.
131132
"""
132133
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
134137

135138
def get_affluence(self, start, end, line_id):
136139
"""
@@ -210,7 +213,39 @@ def get_all_lines_affluence(self):
210213

211214
return affluences
212215

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+
213243

214244
if __name__ == "__main__":
215245
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())

src/ratp_api/models/__init__.py

Whitespace-only changes.

src/ratp_api/models/line.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from pydantic import BaseModel, HttpUrl
2+
3+
4+
class Icon(BaseModel):
5+
svg: HttpUrl
6+
png: HttpUrl
7+
name: str
8+
9+
10+
class Assets(BaseModel):
11+
icon: Icon
12+
13+
14+
class Line(BaseModel):
15+
id: str
16+
name: str
17+
displayCode: str
18+
businessMode: str
19+
assets: Assets
20+
21+
22+
class SituationMessage(BaseModel):
23+
isActive: bool
24+
isPlanned: bool
25+
criticity: str
26+
messages: list[str]
27+
28+
29+
class TwitterAccountInfos(BaseModel):
30+
twitterId: str
31+
twitterUrl: HttpUrl
32+
isThreadActive: bool
33+
34+
35+
class LineData(BaseModel):
36+
line: Line
37+
situations: list[SituationMessage]
38+
twitterAccountInfos: TwitterAccountInfos

0 commit comments

Comments
 (0)