2023-11-27 22:44:17 +02:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from typing import Any, List, Mapping, Union
|
2023-11-24 12:21:02 +02:00
|
|
|
|
2023-11-27 22:44:17 +02:00
|
|
|
from isodate import Duration, parse_duration
|
2022-09-24 12:40:38 +03:00
|
|
|
|
2024-09-15 01:54:56 +03:00
|
|
|
from ..classes.stop import Stop
|
2023-11-24 12:21:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Url:
|
|
|
|
"""Traffic message channel url object."""
|
2022-09-24 12:40:38 +03:00
|
|
|
|
2023-11-24 12:21:02 +02:00
|
|
|
def __init__(self, data: Mapping[str, Any]) -> None:
|
2023-11-27 22:44:17 +02:00
|
|
|
self.name: str = data["name"]
|
|
|
|
self.url: str = data["url"]
|
2022-09-24 12:40:38 +03:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"{self.name}: {self.url}"
|
|
|
|
|
|
|
|
|
2023-11-24 12:21:02 +02:00
|
|
|
class Channel:
|
|
|
|
"""Traffic message channel object."""
|
|
|
|
|
|
|
|
def __init__(self, data: Mapping[str, Any]) -> None:
|
2023-11-27 22:44:17 +02:00
|
|
|
self.name: str = data["name"]
|
2022-09-24 12:40:38 +03:00
|
|
|
url = []
|
2023-11-24 12:21:02 +02:00
|
|
|
url.extend(Url(link) for link in url)
|
2023-11-27 22:44:17 +02:00
|
|
|
self.url: List[Url] = url
|
|
|
|
self.time_start: Union[datetime, None] = (
|
|
|
|
None
|
|
|
|
if "validFromDate" not in data
|
|
|
|
else datetime.strptime(
|
|
|
|
f"{data['validFromDate']} {data['validFromTime']}", "%Y-%m-%d %H:%M:%S"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.time_end: Union[datetime, None] = (
|
|
|
|
None
|
|
|
|
if "validToDate" not in data
|
|
|
|
else datetime.strptime(
|
|
|
|
f"{data['validToDate']} {data['validToTime']}", "%Y-%m-%d %H:%M:%S"
|
|
|
|
)
|
|
|
|
)
|
2022-09-24 12:40:38 +03:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2023-11-27 22:44:17 +02:00
|
|
|
return f"{self.name}: from {self.time_start} until {self.time_end}"
|
2022-09-24 12:40:38 +03:00
|
|
|
|
|
|
|
|
2023-11-24 12:21:02 +02:00
|
|
|
class Message:
|
|
|
|
"""Traffic message object."""
|
2022-09-24 12:40:38 +03:00
|
|
|
|
2023-11-24 12:21:02 +02:00
|
|
|
def __init__(self, data: Mapping[str, Any]) -> None:
|
2023-11-27 22:44:17 +02:00
|
|
|
self.affected_stops: List[Stop] = []
|
|
|
|
self.valid_from_stop: Union[Stop, None] = (
|
2023-11-24 12:21:02 +02:00
|
|
|
None if "validFromStop" not in data else Stop(data["validFromStop"])
|
|
|
|
)
|
2023-11-27 22:44:17 +02:00
|
|
|
self.valid_to_stop: Union[Stop, None] = (
|
|
|
|
None if "validToStop" not in data else Stop(data["validToStop"])
|
|
|
|
)
|
|
|
|
self.channels: Union[Channel, None] = []
|
2023-11-24 12:21:02 +02:00
|
|
|
self.channels.extend(Channel(channel) for channel in data["channel"])
|
2023-11-27 22:44:17 +02:00
|
|
|
self.id: str = data["id"]
|
|
|
|
self.active: bool = data["act"]
|
2024-09-19 23:03:23 +03:00
|
|
|
self.head: str = "" if "head" not in data else data["head"]
|
|
|
|
self.lead: str = "" if "lead" not in data else data["lead"]
|
|
|
|
self.text: str = "" if "text" not in data else data["text"]
|
2023-11-27 22:44:17 +02:00
|
|
|
self.company: Union[str, None] = data.get("company")
|
|
|
|
self.category: Union[str, None] = data.get("category")
|
|
|
|
self.priority: Union[int, None] = data.get("priority")
|
|
|
|
self.products: int = data["products"]
|
|
|
|
self.icon: Mapping[str, Any] = data["icon"]
|
|
|
|
self.time_start: Union[datetime, None] = (
|
|
|
|
None
|
|
|
|
if "validFromDate" not in data or "validFromTime" not in data
|
|
|
|
else datetime.strptime(
|
|
|
|
f"{data['validFromDate']} {data['validFromTime']}", "%Y-%m-%d %H:%M:%S"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.time_end: Union[datetime, None] = (
|
|
|
|
None
|
|
|
|
if "validToDate" not in data or "validToTime" not in data
|
|
|
|
else datetime.strptime(
|
|
|
|
f"{data['validToDate']} {data['validToTime']}", "%Y-%m-%d %H:%M:%S"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.date_start_alt: str = data["altStart"]
|
|
|
|
self.date_end_alt: str = data["altEnd"]
|
|
|
|
self.time_modified: Union[datetime, None] = (
|
|
|
|
None
|
|
|
|
if "modDate" not in data or "modTime" not in data
|
|
|
|
else datetime.strptime(
|
|
|
|
f"{data['modDate']} {data['modTime']}", "%Y-%m-%d %H:%M:%S"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.daily_start: Union[datetime, None] = (
|
|
|
|
None
|
|
|
|
if "dailyStartingAt" not in data
|
|
|
|
else datetime.strptime(data["dailyStartingAt"], "%H:%M:%S")
|
|
|
|
)
|
|
|
|
self.daily_duration: Union[Duration, timedelta] = parse_duration(
|
|
|
|
data["dailyDuration"]
|
|
|
|
)
|
|
|
|
self.base_type: Union[str, None] = data.get("baseType")
|
2022-09-24 12:40:38 +03:00
|
|
|
|
2023-11-24 12:21:02 +02:00
|
|
|
if "affectedStops" in data:
|
|
|
|
self.affected_stops.extend(
|
|
|
|
Stop(stop) for stop in data["affectedStops"]["StopLocation"]
|
|
|
|
)
|
2022-09-24 12:40:38 +03:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2023-11-27 22:44:17 +02:00
|
|
|
return f"{self.base_type} message with priority {self.products} valid from {self.time_start} until {self.time_end}: {self.head} - {self.lead}"
|