Skip to content

Commit

Permalink
Remove any non-number characters from flight numbers
Browse files Browse the repository at this point in the history
Fixes #288.

Southwest now includes a prefix to the start of their flight numbers.
Instead of '100' it is now 'WN100'. I'm not sure if the only prefix is
WN, so I opted to remove all non-number characters as the fare checking
still requires only numbers. Eventually, this will probably change and
these changes can be reverted.
  • Loading branch information
jdholtz committed Aug 12, 2024
1 parent ca6717f commit edcb465
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
12 changes: 10 additions & 2 deletions lib/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,16 @@ def _convert_to_utc(self, flight_date: str, airport_timezone: Any) -> datetime:
return utc_time

def _get_flight_number(self, flights: JSON) -> str:
"""
Formats the flight number in the way that the fare checker expects it, which is with only
numbers and a slash separating each number with a zero-width space on either side.
"""
flight_number = ""
for flight in flights:
flight_number += flight["number"] + "\u200b/\u200b"
# Remove all non-numbers from the flight number
flight_num = "".join(i for i in flight["number"] if i.isdigit())

return flight_number.rstrip("\u200b/\u200b")
flight_number += flight_num + "\u200b/\u200b"

# Remove any slashes and zero-width spaces from the end
return flight_number.rstrip("/\u200b")
2 changes: 1 addition & 1 deletion tests/integration/test_check_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def handler(mocker: MockerFixture) -> None:
"departureAirport": {"code": "LAX", "name": "test_outbound"},
"departureDate": "2021-12-06",
"departureTime": "14:40",
"flights": [{"number": "100"}],
"flights": [{"number": "WN100"}],
}
flight = Flight(flight_info, {}, "TEST")
# Make sure it isn't affected by local time
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_monitoring_and_scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def mock_get_driver(self) -> mock.Mock:
"departureAirport": {"code": "LAX", "name": "test_outbound"},
"departureDate": "2020-10-13",
"departureTime": "14:40",
"flights": [{"number": "100"}, {"number": "101"}],
"flights": [{"number": "WN100"}, {"number": "WN101"}],
},
],
}
Expand Down Expand Up @@ -199,14 +199,14 @@ def mock_get_driver(self) -> mock.Mock:
"departureAirport": {"code": "LAX", "name": "test_outbound"},
"departureDate": "2020-10-13",
"departureTime": "14:40",
"flights": [{"number": "100"}],
"flights": [{"number": "WN100"}],
},
{
"arrivalAirport": {"name": "test_outbound", "country": None},
"departureAirport": {"code": "SYD", "name": "test_inbound"},
"departureDate": "2020-10-16",
"departureTime": "07:20",
"flights": [{"number": "101"}],
"flights": [{"number": "WN101"}],
},
],
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_fare_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_flight(mocker: MockerFixture) -> Flight:
"departureAirport": {"name": None},
"arrivalAirport": {"name": None, "country": None},
"departureTime": None,
"flights": [{"number": "100"}],
"flights": [{"number": "WN100"}],
}

reservation_info = {"bounds": [flight_info]}
Expand Down
13 changes: 7 additions & 6 deletions tests/unit/test_flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _set_up_flight(self) -> None:
"arrivalAirport": {"name": None, "country": None},
"departureDate": "1971-06-18",
"departureTime": "07:00",
"flights": [{"number": "100"}],
"flights": [{"number": "WN100"}],
}

# Needs to be mocked so it is only run when Flight is instantiated
Expand All @@ -44,7 +44,7 @@ def test_flight_is_international_when_country_is_specified(
"departureAirport": {"name": None},
"arrivalAirport": {"name": None, "country": country},
"departureTime": None,
"flights": [{"number": "100"}],
"flights": [{"number": "WN100"}],
}
flight = Flight(flight_info, {}, "")

Expand All @@ -58,7 +58,7 @@ def test_flights_with_the_same_flight_numbers_and_departure_times_are_equal(
"departureAirport": {"name": None},
"arrivalAirport": {"name": None, "country": None},
"departureTime": None,
"flights": [{"number": "100"}],
"flights": [{"number": "WN100"}],
}
flight1 = Flight(flight_info, {}, "")
flight2 = Flight(flight_info, {}, "")
Expand All @@ -76,7 +76,7 @@ def test_flights_with_the_same_flight_numbers_and_departure_times_are_equal(
"departureAirport": {"name": None},
"arrivalAirport": {"name": None, "country": None},
"departureTime": None,
"flights": [{"number": "101"}],
"flights": [{"number": "WN101"}],
},
datetime(1999, 1, 1, 8, 59),
),
Expand All @@ -85,7 +85,7 @@ def test_flights_with_the_same_flight_numbers_and_departure_times_are_equal(
"departureAirport": {"name": None},
"arrivalAirport": {"name": None, "country": None},
"departureTime": None,
"flights": [{"number": "100"}],
"flights": [{"number": "WN100"}],
},
datetime(1999, 1, 1, 9, 59),
),
Expand Down Expand Up @@ -141,7 +141,8 @@ def test_convert_to_utc_converts_local_time_to_utc(self) -> None:
assert self.flight._local_departure_time == tz.localize(datetime(1999, 12, 31, 23, 59))

@pytest.mark.parametrize(
["numbers", "expected_num"], [(["100"], "100"), (["100", "101"], "100\u200b/\u200b101")]
["numbers", "expected_num"],
[(["WN100"], "100"), (["WN100", "WN101"], "100\u200b/\u200b101")],
)
def test_get_flight_number_creates_flight_number_correctly(
self, numbers: List[str], expected_num: str
Expand Down

0 comments on commit edcb465

Please sign in to comment.