Skip to content

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
cohaolain committed Mar 12, 2023
2 parents d2880a6 + af55980 commit 33daaa4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# [v2.1.0] - 2023.03.12
### Added
- Added flight departure time filter keyword arguments to `get_cheapest_flights` and `get_cheapest_return_flights`.

# [v2.0.1] - 2023.03.11
### Fixed
- Module description to account for new functionality.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This module allows you to retrieve either:
1) The cheapest flights, with or without return flights, within a fixed set of dates.
or
2) All available flights between two locations, on a given date

This is done directly through Ryanair's API, and does not require an API key.
## Installation
Run the following command in the terminal:
Expand Down
56 changes: 41 additions & 15 deletions ryanair/ryanair.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
This is done directly through Ryanair's API, and does not require an API key.
"""
import logging
from typing import Union

import backoff
import requests
from datetime import datetime, date as dt_date
from datetime import datetime, date, time
from time import sleep

from deprecated import deprecated
Expand Down Expand Up @@ -42,15 +43,21 @@ def get_return_flights(self, source_airport, date_from, date_to,
return_date_from, return_date_to, destination_country)

def get_cheapest_flights(self, airport, date_from, date_to, destination_country=None,
custom_params=None):
custom_params=None,
departure_time_from: Union[str, time] = "00:00",
departure_time_to: Union[str, time] = "23:59",
):
query_url = ''.join((Ryanair.BASE_SERVICES_API_URL,
"oneWayFares"))

params = {
"departureAirportIataCode": airport,
"outboundDepartureDateFrom": self._format_date_for_api(date_from),
"outboundDepartureDateTo": self._format_date_for_api(date_to),
"currency": self.currency}
"currency": self.currency,
"outboundDepartureTimeFrom": self._format_time_for_api(departure_time_from),
"outboundDepartureTimeTo": self._format_time_for_api(departure_time_to)
}
if destination_country:
params['arrivalCountryCode'] = destination_country
if custom_params:
Expand All @@ -70,7 +77,12 @@ def get_cheapest_flights(self, airport, date_from, date_to, destination_country=
def get_cheapest_return_flights(self, source_airport, date_from, date_to,
return_date_from, return_date_to,
destination_country=None,
custom_params=None):
custom_params=None,
outbound_departure_time_from: Union[str, time] = "00:00",
outbound_departure_time_to: Union[str, time] = "23:59",
inbound_departure_time_from: Union[str, time] = "00:00",
inbound_departure_time_to: Union[str, time] = "23:59",
):
query_url = ''.join((Ryanair.BASE_SERVICES_API_URL,
"roundTripFares"))

Expand All @@ -80,7 +92,12 @@ def get_cheapest_return_flights(self, source_airport, date_from, date_to,
"outboundDepartureDateTo": self._format_date_for_api(date_to),
"inboundDepartureDateFrom": self._format_date_for_api(return_date_from),
"inboundDepartureDateTo": self._format_date_for_api(return_date_to),
"currency": self.currency}
"currency": self.currency,
"outboundDepartureTimeFrom": self._format_time_for_api(outbound_departure_time_from),
"outboundDepartureTimeTo": self._format_time_for_api(outbound_departure_time_to),
"inboundDepartureTimeFrom": self._format_time_for_api(inbound_departure_time_from),
"inboundDepartureTimeTo": self._format_time_for_api(inbound_departure_time_to)
}
if destination_country:
params['arrivalCountryCode'] = destination_country
if custom_params:
Expand All @@ -98,8 +115,9 @@ def get_cheapest_return_flights(self, source_airport, date_from, date_to,
else:
return []

def get_all_flights(self, origin_airport, date, destination,
locale="en-ie", origin_is_mac=False, destination_is_mac=False, custom_params=None):
def get_all_flights(self, origin_airport, date_out, destination,
locale="en-ie", origin_is_mac=False, destination_is_mac=False,
custom_params=None):
query_url = ''.join((Ryanair.BASE_AVAILABILITY_API_URL, f"{locale}/availability"))

params = {
Expand All @@ -109,7 +127,7 @@ def get_all_flights(self, origin_airport, date, destination,
"CHD": 0,
"INF": 0,

"DateOut": self._format_date_for_api(date),
"DateOut": self._format_date_for_api(date_out),
"DateIn": "",

"Origin": origin_airport,
Expand Down Expand Up @@ -192,15 +210,23 @@ def _parse_all_flights_availability_result_as_flight(response, origin_full, dest
)

@staticmethod
def _format_date_for_api(date):
if isinstance(date, str):
return date
def _format_date_for_api(d: Union[datetime, date, str]):
if isinstance(d, str):
return d

if isinstance(d, datetime):
return d.date().isoformat()

if isinstance(date, datetime):
return date.date().isoformat()
if isinstance(d, date):
return d.isoformat()

@staticmethod
def _format_time_for_api(t):
if isinstance(t, str):
return t

if isinstance(date, dt_date):
return date.isoformat()
if isinstance(t, time):
return t.strftime("%H:%M")

@property
def num_queries(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
long_description = f.read()

setup(name='ryanair-py',
version='2.0.1',
version='2.1.0',
description='A module which allows you to retrieve data about the cheapest one-way and return flights '
'in a date range, or all available flights on a given day for a given route.',
long_description=long_description,
Expand Down

0 comments on commit 33daaa4

Please sign in to comment.