Last active
June 28, 2017 12:25
-
-
Save VelkyVenik/7d7d3462fad8864ce02f82de9a3c9f74 to your computer and use it in GitHub Desktop.
Kiwi.com Python víkend task - https://gist.github.com/martin-kokos/d578679d97eb1652dfeb3e7f2a4e115b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
import argparse | |
from datetime import datetime | |
# Definice vstupnich dat | |
parser = argparse.ArgumentParser(description='Coz takhle dat si o vikendu kiwi') | |
parser.add_argument('--one-way', help='uz se nechci vracet (default)', action='store_true') | |
parser.add_argument('--return', help='jak dlouho tam budu') | |
parser.add_argument('--cheapest', help='nemam prachy (default)', action='store_true') | |
parser.add_argument('--shortest', help='pospicham', action='store_true') | |
parser.add_argument('--from', '-f', help='odkud', required=True) | |
parser.add_argument('--to', '-t', help='kam', required=True) | |
parser.add_argument('--date', '-d', help='kdy', required=True) | |
# Zpracovani vstupnich dat | |
args = vars(parser.parse_args()) | |
# Tohle vsechno umime | |
request_args = {'flyFrom': None, 'dateTo': None, 'to': None, 'dateFrom': None, 'typeFlight': None, | |
'sort': None, 'daysInDestinationFrom': None, 'daysInDestinationTo': None} | |
# Kontrola dat | |
if args['return'] and args['one_way']: | |
print('Pokud chces jenom tam, tak si tam zustan jak dlouho chces') | |
exit(1) | |
if args['cheapest'] and args['shortest']: | |
print('Bud levne nebo rychle') | |
exit(1) | |
request_args['flyFrom'] = args['from'] | |
request_args['to'] = args['to'] | |
d = datetime.strptime(args['date'], '%Y-%m-%d') | |
request_args['dateFrom'] = d.strftime('%d/%m/%Y') | |
request_args['sort'] = 'duration' if args['shortest'] else 'price' | |
request_args['typeFlight'] = 'round' if args['return'] else 'one_way' | |
if request_args['typeFlight'] == 'round': | |
request_args['daysInDestinationFrom'] = args['return'] | |
print("Hledam let z %s do %s dne %s (%s, %s)" % ( | |
request_args['flyFrom'], | |
request_args['to'], | |
request_args['dateFrom'], | |
'zpatecni nejdrive za %s dni' % request_args['daysInDestinationFrom'] if args['return'] else 'jednosmerny', | |
'nejkratsi' if args['shortest'] else 'nejlevnejsi' | |
)) | |
# Nalezeni letu | |
try: | |
r = requests.get('https://api.skypicker.com/flights', params=request_args) | |
except requests.exceptions.RequestException as e: | |
print('Chyba pri hledani letu:', e) | |
exit(1) | |
try: | |
flight = r.json()['data'][0] | |
except ValueError as e: | |
print('Chybna odpoved pri hledani letu:', e) | |
exit(1) | |
print("Nalezen let %s za %s EUR" % (flight['id'], flight['price'])) | |
# Zabookovani nalezeneho letu | |
booking = """ | |
{ | |
"lang": "en", | |
"bags": 0, | |
"passengers": [ | |
{ | |
"lastName": "Bernard", | |
"cardno": "314159265", | |
"phone": "+44 123456789", | |
"birthday": "1990-01-01", | |
"nationality": "UK", | |
"firstName": "Alois", | |
"title": "Mr", | |
"expiration": 1454371200, | |
"email": "alois@bernard.beer", | |
"documentID" : "pivoJeDobraVec" | |
} | |
], | |
"locale": "en", | |
"currency": "CZK", | |
"booking_token": "%s", | |
"affily": "affilid", | |
"booked_at": "affilid" | |
} | |
""" % flight['booking_token'] | |
headers = {'Content-Type': 'application/json'} | |
try: | |
r = requests.post('http://37.139.6.125:8080/booking', data=booking, headers=headers) | |
except requests.exceptions.RequestException as e: | |
print('Chyba pri bookovani letu: ', e) | |
exit(1) | |
try: | |
pnr = r.json()['pnr'] | |
except ValueError as e: | |
print('Chybna odpoved pri bookovani letu:', e) | |
exit(1) | |
print('Let zabookovan - %s.\nA to je vse :-)' % pnr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment