diff --git a/backend/backend/idfm_interface/idfm_interface.py b/backend/backend/idfm_interface/idfm_interface.py index 20ef89d..99ea17e 100644 --- a/backend/backend/idfm_interface/idfm_interface.py +++ b/backend/backend/idfm_interface/idfm_interface.py @@ -1,4 +1,5 @@ from collections import defaultdict +from logging import getLogger from re import compile as re_compile from time import time from typing import ( @@ -37,6 +38,10 @@ from .idfm_types import ( from .ratp_types import Picto as RatpPicto + +logger = getLogger(__name__) + + class IdfmInterface: IDFM_ROOT_URL = "https://prim.iledefrance-mobilites.fr/marketplace" @@ -358,9 +363,22 @@ class IdfmInterface: picto_id = fields.picto.id_ if fields.picto is not None else None + line_id = fields.id_line + try: + formatted_line_id = int(line_id[1:] if line_id[0] == "C" else line_id) + except ValueError: + logger.warning("Unable to format %s line id.", line_id) + continue + + try: + operator_id = int(fields.operatorref) # type: ignore + except (ValueError, TypeError): + logger.warning("Unable to format %s operator id.", fields.operatorref) + operator_id = 0 + ret.append( Line( - id=fields.id_line, + id=formatted_line_id, short_name=fields.shortname_line, name=fields.name_line, status=IdfmLineState(fields.status.value), @@ -373,7 +391,7 @@ class IdfmInterface: ), colour_web_hexa=fields.colourweb_hexa, text_colour_hexa=fields.textcolourprint_hexa, - operator_id=optional_value(fields.operatorref), + operator_id=operator_id, operator_name=optional_value(fields.operatorname), accessibility=IdfmState(fields.accessibility.value), visual_signs_available=IdfmState( @@ -403,13 +421,21 @@ class IdfmInterface: fields.arrxepsg2154, fields.arryepsg2154 ) + try: + postal_region = int(fields.arrpostalregion) + except ValueError: + logger.warning( + "Unable to format %s postal region.", fields.arrpostalregion + ) + continue + yield Stop( id=int(fields.arrid), name=fields.arrname, epsg3857_x=epsg3857_point[0], epsg3857_y=epsg3857_point[1], town_name=fields.arrtown, - postal_region=fields.arrpostalregion, + postal_region=postal_region, transport_mode=TransportMode(fields.arrtype.value), version=fields.arrversion, created_ts=created_ts, diff --git a/backend/backend/models/line.py b/backend/backend/models/line.py index 4888e48..786c585 100644 --- a/backend/backend/models/line.py +++ b/backend/backend/models/line.py @@ -27,7 +27,7 @@ from .stop import _Stop class LineStopAssociations(Base): id = mapped_column(BigInteger, primary_key=True) - line_id = mapped_column(String, ForeignKey("lines.id")) + line_id = mapped_column(BigInteger, ForeignKey("lines.id")) stop_id = mapped_column(BigInteger, ForeignKey("_stops.id")) __tablename__ = "line_stop_associations" @@ -53,7 +53,7 @@ class Line(Base): db = db - id = mapped_column(String, primary_key=True) + id = mapped_column(BigInteger, primary_key=True) short_name = mapped_column(String) name = mapped_column(String, nullable=False) @@ -68,7 +68,7 @@ class Line(Base): colour_web_hexa = mapped_column(String, nullable=False) text_colour_hexa = mapped_column(String, nullable=False) - operator_id = mapped_column(String) + operator_id = mapped_column(Integer) operator_name = mapped_column(String) accessibility = mapped_column(Enum(IdfmState), nullable=False) diff --git a/backend/backend/models/stop.py b/backend/backend/models/stop.py index 658869f..05e05e2 100644 --- a/backend/backend/models/stop.py +++ b/backend/backend/models/stop.py @@ -53,7 +53,7 @@ class _Stop(Base): name = mapped_column(String, nullable=False, index=True) town_name = mapped_column(String, nullable=False) - postal_region = mapped_column(String, nullable=False) + postal_region = mapped_column(Integer, nullable=False) epsg3857_x = mapped_column(Float, nullable=False) epsg3857_y = mapped_column(Float, nullable=False) diff --git a/backend/backend/schemas/line.py b/backend/backend/schemas/line.py index 8a65841..01feeed 100644 --- a/backend/backend/schemas/line.py +++ b/backend/backend/schemas/line.py @@ -46,7 +46,7 @@ class TransportMode(StrEnum): class Line(BaseModel): - id: str + id: int shortName: str name: str status: IdfmLineState diff --git a/backend/main.py b/backend/main.py index ff1acd5..e5a1f08 100755 --- a/backend/main.py +++ b/backend/main.py @@ -73,13 +73,11 @@ trace.set_tracer_provider(TracerProvider()) trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(OTLPSpanExporter())) tracer = trace.get_tracer(APP_NAME) -with tracer.start_as_current_span("foo"): - print("Hello world!") - idfm_interface = IdfmInterface(API_KEY, db) +# TODO: Add command line argument to force database reset. @app.on_event("startup") async def startup(): await db.connect(DB_PATH, clear_static_data=True) @@ -102,7 +100,7 @@ def optional_datetime_to_ts(dt: datetime | None) -> int | None: @app.get("/line/{line_id}", response_model=LineSchema) -async def get_line(line_id: str) -> LineSchema: +async def get_line(line_id: int) -> LineSchema: line: Line | None = await Line.get_by_id(line_id) if line is None: