BaseProfile

For a documentation of the variables, please look at the documentation of ProfileInterface

class pyhafas.profile.base.BaseProfile(ua=None)[source]

Bases: pyhafas.profile.base.helper.request.BaseRequestHelper, pyhafas.profile.base.helper.format_products_filter.BaseFormatProductsFilterHelper, pyhafas.profile.base.helper.parse_lid.BaseParseLidHelper, pyhafas.profile.base.helper.date_time.BaseDateTimeHelper, pyhafas.profile.base.helper.parse_leg.BaseParseLegHelper, pyhafas.profile.base.helper.parse_remark.BaseParseRemarkHelper, pyhafas.profile.base.requests.location.BaseLocationRequest, pyhafas.profile.base.requests.journey.BaseJourneyRequest, pyhafas.profile.base.requests.journeys.BaseJourneysRequest, pyhafas.profile.base.requests.station_board.BaseStationBoardRequest, pyhafas.profile.base.requests.trip.BaseTripRequest, pyhafas.profile.interfaces.ProfileInterface

Profile for a “normal” HaFAS. Only for other profiles usage as basis.

addChecksum: bool = False
addMicMac: bool = False
availableProducts: Dict[str, List[int]] = {}
baseUrl: str = ''
defaultProducts: List[str] = []
defaultUserAgent: str = 'pyhafas'
requestBody: dict = {}
salt: str = ''

Helper

format_products_filter

class pyhafas.profile.base.helper.format_products_filter.BaseFormatProductsFilterHelper[source]

Bases: pyhafas.profile.interfaces.helper.format_products_filter.FormatProductsFilterHelperInterface

Helper for creating the products filter

format_products_filter(requested_products)[source]

Create the products filter given to HaFAS

Parameters

requested_products (dict) – Mapping of Products to whether it’s enabled or disabled

Return type

dict

Returns

value for HaFAS “jnyFltrL” attribute

date_time

class pyhafas.profile.base.helper.date_time.BaseDateTimeHelper[source]

Bases: pyhafas.profile.interfaces.helper.date_time.DateTimeHelperInterface

parse_date(date_string)[source]

Parses the date HaFAS returns

Parameters

date_string (str) – Date sent by HaFAS

Return type

date

Returns

Parsed date object

parse_datetime(time_string, date)[source]

Parses the time format HaFAS returns and combines it with a date

Parameters
  • time_string (str) – Time string sent by HaFAS (multiple formats are supported. One example: 234000)

  • date (date) – Start day of the leg/journey

Return type

datetime

Returns

Parsed date and time as datetime object

parse_timedelta(time_string)[source]

Parses the time HaFAS returns as timedelta object

Example use case is when HaFAS returns a duration of a leg :type time_string: str :param time_string: Time string sent by HaFAS (example for format is: 033200) :rtype: timedelta :return: Parsed time as timedelta object

transform_datetime_parameter_timezone(date_time)[source]

Transfers datetime parameters incoming by the user to the profile timezone

Parameters

date_time (datetime) – datetime parameter incoming by user. Can be timezone aware or unaware

Return type

datetime

Returns

Timezone aware datetime object in profile timezone

parse_leg

class pyhafas.profile.base.helper.parse_leg.BaseParseLegHelper[source]

Bases: pyhafas.profile.interfaces.helper.parse_leg.ParseLegHelperInterface

parse_leg(journey, common, departure, arrival, date, jny_type='JNY', gis=None)[source]

Parses Leg HaFAS returns into Leg object

Different Types of HaFAS responses can be parsed into a leg object with the multiple variables

Parameters
  • journey (dict) – Journey object given back by HaFAS (Data of the Leg to parse)

  • common (dict) – Common object given back by HaFAS

  • departure (dict) – Departure object given back by HaFAS

  • arrival (dict) – Arrival object given back by HaFAS

  • date (date) – Parsed date of Journey (Departing date)

  • jny_type (str) – HaFAS Journey type

  • gis – GIS object given back by HaFAS. Currently only used by “WALK” journey type.

Return type

Leg

Returns

Parsed Leg object

parse_legs(jny, common, date)[source]

Parses Legs (when multiple available)

Parameters
  • jny (dict) – Journeys object returned by HaFAS (contains secL list)

  • common (dict) – Common object returned by HaFAS

  • date (date) – Parsed date of Journey (Departing date)

Return type

List[Leg]

Returns

Parsed List of Leg objects

parse_lid

class pyhafas.profile.base.helper.parse_lid.BaseParseLidHelper[source]

Bases: pyhafas.profile.interfaces.helper.parse_lid.ParseLidHelperInterface

parse_lid(lid)[source]

Converts the LID given by HaFAS

Splits the LID (e.g. A=1@O=Siegburg/Bonn) in multiple elements (e.g. A=1 and O=Siegburg/Bonn). These are converted into a dict where the part before the equal sign is the key and the part after the value.

Parameters

lid (str) – Location identifier (given by HaFAS)

Return type

dict

Returns

Dict of the elements of the dict

parse_lid_to_station(lid, name='', latitude=0, longitude=0)[source]

Parses the LID given by HaFAS to a station object

Parameters
  • lid (str) – Location identifier (given by HaFAS)

  • name (str) – Station name (optional, if not given, LID is used)

  • latitude (float) – Latitude of the station (optional, if not given, LID is used)

  • longitude (float) – Longitude of the station (optional, if not given, LID is used)

Return type

Station

Returns

Parsed LID as station object

request

class pyhafas.profile.base.helper.request.BaseRequestHelper[source]

Bases: pyhafas.profile.interfaces.helper.request.RequestHelperInterface

activate_retry(retries=4, backoff_factor=1)[source]
Return type

None

calculate_checksum(data)[source]

Calculates the checksum of the request (required for most profiles)

Parameters

data (str) – Complete body as string

Return type

str

Returns

Checksum for the request

calculate_mic_mac(data)[source]

Calculates the mic-mac for the request (required for some profiles)

Parameters

data (str) – Complete body as string

Return type

Tuple[str, str]

Returns

Mic and mac to be sent to HaFAS

request(body)[source]

Sends the request and does a basic parsing of the response and error handling

Parameters

body – Reqeust body as dict (without the requestBody of the profile)

Return type

HafasResponse

Returns

HafasRespone object or Exception when HaFAS response returns an error

request_session = <requests.sessions.Session object>
url_formatter(data)[source]

Formats the URL for HaFAS (adds the checksum or mic-mac)

Parameters

data (str) – Complete body as string

Return type

str

Returns

Request-URL (maybe with checksum or mic-mac)

Mappings

error_codes

class pyhafas.profile.base.mappings.error_codes.BaseErrorCodesMapping(value)[source]

Bases: pyhafas.profile.interfaces.mappings.error_codes.ErrorCodesMappingInterface

Mapping of the HaFAS error code to the exception class

default defines the error when the error code cannot be found in the mapping

AUTH = <class 'pyhafas.types.exceptions.AuthenticationError'>
H500 = <class 'pyhafas.types.exceptions.JourneysTooManyTrainsError'>
H890 = <class 'pyhafas.types.exceptions.JourneysArrivalDepartureTooNearError'>
LOCATION = <class 'pyhafas.types.exceptions.LocationNotFoundError'>
R5000 = <class 'pyhafas.types.exceptions.AccessDeniedError'>
SQ005 = <class 'pyhafas.types.exceptions.TripDataNotFoundError'>
TI001 = <class 'pyhafas.types.exceptions.TripDataNotFoundError'>
default: Exception = <class 'pyhafas.types.exceptions.GeneralHafasError'>

Requests

journey

class pyhafas.profile.base.requests.journey.BaseJourneyRequest[source]

Bases: pyhafas.profile.interfaces.requests.journey.JourneyRequestInterface

format_journey_request(journey)[source]

Creates the HaFAS request body for a journey request

Parameters

journey (Journey) – Id of the journey (ctxRecon)

Return type

dict

Returns

Request body for HaFAS

parse_journey_request(data)[source]

Parses the HaFAS response for a journey request

Parameters

data (HafasResponse) – Formatted HaFAS response

Return type

Journey

Returns

List of Journey objects

journeys

class pyhafas.profile.base.requests.journeys.BaseJourneysRequest[source]

Bases: pyhafas.profile.interfaces.requests.journeys.JourneysRequestInterface

format_journeys_request(origin, destination, via, date, min_change_time, max_changes, products, max_journeys)[source]

Creates the HaFAS request body for a journeys request

Parameters
  • origin (Station) – Origin station

  • destination (Station) – Destionation station

  • via (List[Station]) – Via stations, maybe empty list)

  • date (datetime) – Date and time to search journeys for

  • min_change_time (int) – Minimum transfer/change time at each station

  • max_changes (int) – Maximum number of changes

  • products (Dict[str, bool]) – Allowed products (a product is a mean of transport like ICE,IC)

  • max_journeys (int) – Maximum number of returned journeys

Return type

dict

Returns

Request body for HaFAS

format_search_from_leg_request(origin, destination, via, min_change_time, max_changes, products)[source]

Creates the HaFAS request body for a journeys request

Parameters
  • origin (Leg) – Origin leg

  • destination (Station) – Destionation station

  • via (List[Station]) – Via stations, maybe empty list)

  • min_change_time (int) – Minimum transfer/change time at each station

  • max_changes (int) – Maximum number of changes

  • products (Dict[str, bool]) – Allowed products (a product is a mean of transport like ICE,IC)

Return type

dict

Returns

Request body for HaFAS

parse_journeys_request(data)[source]

Parses the HaFAS response for a journeys request

Parameters

data (HafasResponse) – Formatted HaFAS response

Return type

List[Journey]

Returns

List of Journey objects

location

class pyhafas.profile.base.requests.location.BaseLocationRequest[source]

Bases: pyhafas.profile.interfaces.requests.location.LocationRequestInterface

format_location_request(term, rtype='S')[source]

Creates the HaFAS request body for a location search request.

Parameters
  • term (str) – Search term

  • type – Result types. One of [‘S’ for stations, ‘ALL’ for addresses and stations]

Returns

Request body for HaFAS

parse_location_request(data)[source]

Parses the HaFAS response for a location request

Parameters

data (HafasResponse) – Formatted HaFAS response

Return type

List[Station]

Returns

List of Station objects

station_board

class pyhafas.profile.base.requests.station_board.BaseStationBoardRequest[source]

Bases: pyhafas.profile.interfaces.requests.station_board.StationBoardRequestInterface

format_station_board_request(station, request_type, date, max_trips, duration, products, direction)[source]

Creates the HaFAS request for a station board request (departure/arrival)

Parameters
  • station (Station) – Station to get departures/arrivals for

  • request_type (StationBoardRequestType) – ARRIVAL or DEPARTURE

  • date (datetime) – Date and time to get departures/arrival for

  • max_trips (int) – Maximum number of trips that can be returned

  • products (Dict[str, bool]) – Allowed products (e.g. ICE,IC)

  • duration (int) – Time in which trips are searched

  • direction (Optional[Station]) – Direction (end) station of the train. If none, filter will not be applied

Return type

dict

Returns

Request body for HaFAS

parse_station_board_request(data, departure_arrival_prefix)[source]

Parses the HaFAS data for a station board request

Parameters
  • data (HafasResponse) – Formatted HaFAS response

  • departure_arrival_prefix (str) – Prefix for specifying whether its for arrival or departure (either a for arrival or d for departure)

Return type

List[StationBoardLeg]

Returns

List of StationBoardLeg objects

trip

class pyhafas.profile.base.requests.trip.BaseTripRequest[source]

Bases: pyhafas.profile.interfaces.requests.trip.TripRequestInterface

format_trip_request(trip_id)[source]

Creates the HaFAS request for a trip request

Parameters

trip_id (str) – Id of the trip/leg

Return type

dict

Returns

Request body for HaFAS

parse_trip_request(data)[source]

Parses the HaFAS data for a trip request

Parameters

data (HafasResponse) – Formatted HaFAS response

Return type

Leg

Returns

Leg objects