-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathsky_picker.py
More file actions
85 lines (76 loc) · 3.51 KB
/
sky_picker.py
File metadata and controls
85 lines (76 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
""" Classes and examples for searching for flights using SkyPicker. """
from __future__ import unicode_literals, absolute_import, generators, \
print_function
import requests
from datetime import datetime
class SkyPickerApi(object):
""" SkyPicker API. """
def __init__(self):
""" Initializes the API object with URL attributes. """
self.base_url = 'https://api.skypicker.com/'
self.path = ''
self.param_str = ''
@property
def full_url(self):
""" Returns the full URL for requesting the data. """
return '{}{}{}'.format(self.base_url, self.path, self.param_str)
def get_request(self):
""" Requests the API endpoint and returns the response """
headers = {'content-type': 'application/json'}
resp = requests.get(self.full_url, headers=headers)
return resp.json()
def search_places(self, place_name, locale=None):
""" Finds matching place API ids to use for searches.
:param place_name: string of the place name to search for
:kwarg locale: two letter lowercase locale string
returns JSON response
"""
self.path = 'places'
self.param_str = '?term={}'.format(place_name)
if locale:
self.param_str += '&locale={}'.format(locale)
return self.get_request()
def search_flights(self, origin, destination, start_date, end_date,
num_passengers):
""" Searches for flights given a time range and origin and destination.
:param origin: string representing the ID or IATA
:param destination: string representing the ID or IATA
:param start_date: datetime representing first possible travel date
:param end_date: datetime representing last possible travel date
:param num_passengers: integer
returns JSON response
"""
self.path = 'flights'
self.param_str = '?flyFrom=' + \
'{}&to={}&dateFrom={}&dateTo={}&passengers={}'.format(
origin, destination, start_date.strftime('%d/%m/%Y'),
end_date.strftime('%d/%m/%Y'), num_passengers)
resp = self.get_request()
flights = []
for flight in resp.get('data'):
flight_info = {
'departure': datetime.utcfromtimestamp(flight.get('dTimeUTC')),
'arrival': datetime.utcfromtimestamp(flight.get('aTimeUTC')),
'price': flight.get('price'),
'currency': resp.get('currency'),
'legs': []
}
flight_info['duration'] = flight_info['arrival'] - \
flight_info['departure']
flight_info['duration_hours'] = (flight_info[
'duration'].total_seconds() / 60.0) / 60.0
for route in flight['route']:
flight_info['legs'].append({
'carrier': route['airline'],
'departure': datetime.utcfromtimestamp(
route.get('dTimeUTC')),
'arrival': datetime.utcfromtimestamp(
route.get('aTimeUTC')),
'from': '{} ({})'.format(route['cityFrom'],
route['flyFrom']),
'to': '{} ({})'.format(route['cityTo'], route['flyTo']),
})
flight_info['carrier'] = ', '.join(set([c.get('carrier') for c
in flight_info['legs']]))
flights.append(flight_info)
return flights