2022-09-24 12:40:38 +03:00
from datetime import datetime
2023-11-24 12:21:02 +02:00
from typing import Any , Mapping
2022-09-24 12:40:38 +03:00
from isodate import parse_duration
2023-11-24 13:39:25 +02:00
from pyrmv . 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 :
2022-09-24 12:40:38 +03:00
self . name = data [ " name " ]
self . url = data [ " url " ]
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 :
2022-09-24 12:40:38 +03:00
self . name = data [ " name " ]
url = [ ]
2023-11-24 12:21:02 +02:00
url . extend ( Url ( link ) for link in url )
2022-09-24 12:40:38 +03:00
self . url = url
self . time_start = datetime . strptime ( data [ " validFromTime " ] , " % H: % M: % S " )
self . date_start = datetime . strptime ( data [ " validFromDate " ] , " % Y- % m- %d " )
self . time_end = datetime . strptime ( data [ " validToTime " ] , " % H: % M: % S " )
self . date_end = datetime . strptime ( data [ " validToDate " ] , " % Y- % m- %d " )
def __str__ ( self ) - > str :
return f " { self . name } : from { self . time_start } { self . date_start } until { self . time_end } { self . date_end } "
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 :
2022-09-24 13:08:16 +03:00
self . affected_stops = [ ]
2023-11-24 12:21:02 +02:00
self . valid_from_stop = (
None if " validFromStop " not in data else Stop ( data [ " validFromStop " ] )
)
self . valid_to_stop = None if " validToStop " not in data else Stop ( data [ " validToStop " ] )
2022-09-24 13:08:16 +03:00
self . channels = [ ]
2023-11-24 12:21:02 +02:00
self . channels . extend ( Channel ( channel ) for channel in data [ " channel " ] )
2022-09-24 12:40:38 +03:00
self . id = data [ " id " ]
self . active = data [ " act " ]
self . head = data [ " head " ]
self . lead = data [ " lead " ]
self . text = data [ " text " ]
self . company = data [ " company " ]
self . category = data [ " category " ]
2023-11-19 13:19:31 +02:00
self . priority = None if " priority " not in data else data [ " priority " ]
2022-09-24 12:40:38 +03:00
self . products = data [ " products " ]
self . icon = data [ " icon " ]
self . time_start = datetime . strptime ( data [ " sTime " ] , " % H: % M: % S " )
self . date_start = datetime . strptime ( data [ " sDate " ] , " % Y- % m- %d " )
self . time_end = datetime . strptime ( data [ " eTime " ] , " % H: % M: % S " )
self . date_end = datetime . strptime ( data [ " eDate " ] , " % Y- % m- %d " )
self . date_start_alt = data [ " altStart " ]
self . date_end_alt = data [ " altEnd " ]
self . time_modified = datetime . strptime ( data [ " modTime " ] , " % H: % M: % S " )
self . date_modified = datetime . strptime ( data [ " modDate " ] , " % Y- % m- %d " )
self . daily_start = datetime . strptime ( data [ " dailyStartingAt " ] , " % H: % M: % S " )
self . daily_duration = parse_duration ( data [ " dailyDuration " ] )
2023-11-24 12:21:02 +02:00
self . base_type = data [ " baseType " ] if " baseType " in data else None
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-24 12:21:02 +02:00
return f " { self . base_type } message with priority { self . products } valid from { self . time_start . time ( ) } { self . date_start . date ( ) } until { self . time_end . time ( ) } { self . date_end . date ( ) } : { self . head } - { self . lead } "