-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
234 lines (216 loc) · 8.4 KB
/
server.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from flask import Flask, request, redirect
import twilio.twiml
import subprocess
import json
import sklearn
import random
import datetime
from sklearn.feature_extraction import DictVectorizer
from sklearn import svm
import re
import nltk
import datetime
import traceback
app = Flask(__name__)
AMADEUS_API_KEY = 'L3BWAbodyZlr0E2PBXUY2kjm4NNcN9Xq'
LOW_FARE_URL = 'http://api.sandbox.amadeus.com/v1.2/flights/low-fare-search'
EXTENSIVE_URL = 'http://api.sandbox.amadeus.com/v1.2/flights/extensive-search'
app = Flask(__name__)
cities_regex = re.compile('(?:^|.* )([A-Z]*) to ([A-Z]*).*')
day_regex = re.compile('.*(January|February|March|April|May|June|July|August|September|October|November|December) ([0-3]?[0-9]).*')
day_regex_2 = re.compile('.*([01]?[0-9])[\-/]([0123]?[0-9]).*')
time_regex = re.compile('.*(before|after) ([01]?[0-9]) ?([AaPp][Mm]).*')
month_to_num = {
'January': 1,
'February': 2,
'March': 3,
'April': 4,
'May': 5,
'June': 6,
'July': 7,
'August': 8,
'September': 9,
'October': 10,
'November': 11,
'December': 12
}
def canned_responses(msg):
if len(msg) > 20:
return None
if 'thank' in msg.lower():
return 'No problem! :)'
elif 'hi' in msg.lower() or 'hey' in msg.lower() or 'hello' in msg.lower():
return 'Hi, how can I help?'
elif ('who' in msg.lower() or 'name' in msg.lower()) and '?' in msg:
return 'Hi, I\'m Emma! Nice to meet you! :)'
@app.route("/", methods=['GET', 'POST'])
def respond():
msg = request.form.get('Body')
canned = canned_responses(msg)
if canned:
resp = twilio.twiml.Response()
resp.message(canned)
return str(resp)
try:
try:
msg_params = parse_msg(msg)
except Exception:
resp = twilio.twiml.Response()
resp.message("Sorry, I didn't quite catch that. What did you mean?")
return str(resp)
today = datetime.date.today()
month = msg_params['month']
month = int(month) if len(month) < 3 else month_to_num[msg_params['month']]
day = int(msg_params['day'])
year = today.year if today < datetime.date(today.year, month, day) else today.year + 1
datestr = str(datetime.date(year, month, day))
best_time, saved_amt = find_best_time_to_buy(msg_params['origin'], msg_params['destination'], datestr)
buy_in_days = (best_time - today).days
buy_in_days_str = 'in %d days' % buy_in_days if buy_in_days > 0 else 'now'
if len(buy_in_days_str) > 3:
buy_in_days_str += ", and I saved you $%.2f" % saved_amt
if buy_in_days_str == 'now':
depart, arrive, fare = get_best_current_flight(msg_params['origin'], msg_params['destination'], datestr)
buy_in_days_str += '. The flight will depart at %s and arrive at %s in the local time of %s, and the total fare was $%s' % (depart, arrive, msg_params['destination'], fare)
resp = twilio.twiml.Response()
resp.message("Sure thing! I'll book that for you %s. Have a safe trip!" % buy_in_days_str)
return str(resp)
except Exception as e:
resp = twilio.twiml.Response()
resp.message("Oops! I had a kerfuffle. Could you ask me that again?")
traceback.print_exc()
return str(resp)
def iso_to_ordinal(iso):
return datetime.datetime.strptime(iso, '%Y-%m-%d').toordinal()
def amadeus_low_fare_request(origin, destination, departure_date, **kwargs):
"""Makes a request to Amadeus for the low fare flights according to params."""
url_params = {
'origin': origin,
'destination': destination,
'departure_date': departure_date,
}
url_params.update(kwargs)
url = LOW_FARE_URL + '?' + ('apikey=%s&' % AMADEUS_API_KEY) + '&'.join(['%s=%s' % (a, b) for a, b in url_params.iteritems()])
try:
output = subprocess.check_output(['curl', '-X', 'GET', url])
except Exception:
output = subprocess.check_output(['curl', '-X', 'GET', url])
return json.loads(output)
def amadeus_extensive_request(origin, destination, **kwargs):
"""Makes a request to Amadeus for the low fare flights according to params."""
url_params = {
'origin': origin,
'destination': destination,
'aggregation_mode': 'DAY',
}
url_params.update(kwargs)
url = EXTENSIVE_URL + '?' + ('apikey=%s&' % AMADEUS_API_KEY) + '&'.join(['%s=%s' % (a, b) for a, b in url_params.iteritems()])
try:
output = subprocess.check_output(['curl', '-X', 'GET', url])
except Exception:
output = subprocess.check_output(['curl', '-X', 'GET', url])
return json.loads(output)
def flat_flights(amadeus_res):
ret = []
for d in amadeus_res['results']:
common = set(d.keys()) - {'itineraries'}
for it in d['itineraries']:
newd = {k: d[k] for k in common}
newd.update(it)
ret.append(newd)
return ret
def parse_extensive(data):
origin = data['origin']
new_data = []
values = []
for i in data['results']:
values.append(float(i['price']))
temp = i
del temp['price']
del temp['airline']
temp[u'origin'] = origin
departure_date = iso_to_ordinal(temp['departure_date'])
return_date = iso_to_ordinal(temp['return_date'])
now = datetime.datetime.today().toordinal()
days_in_advance = departure_date - now
temp[u'departure_date'] = departure_date
temp[u'return_date'] = return_date
temp[u'days_in_advance'] = days_in_advance
new_data.append(temp)
return (new_data, values)
def get_best_current_flight(origin, destination, departure_date):
res = amadeus_low_fare_request(origin, destination, departure_date, number_of_results=1)
depart_time = res['results'][0]['itineraries'][0]['outbound']['flights'][0]['departs_at']
arrival_time = res['results'][0]['itineraries'][0]['outbound']['flights'][-1]['arrives_at']
depart_time = depart_time.split('T')[-1]
arrival_time = arrival_time.split('T')[-1]
depart_time = datetime.datetime.strptime(depart_time, "%H:%M").strftime("%I:%M %p")
arrival_time = datetime.datetime.strptime(arrival_time, "%H:%M").strftime("%I:%M %p")
fare = res['results'][0]['fare']['total_price']
return depart_time, arrival_time, fare
def find_best_time_to_buy(origin, destination, departure_date, arrive_by=None):
"""Given the parameters from a text, find the best time to buy."""
features, values = parse_extensive(amadeus_extensive_request(origin, destination))
vec = DictVectorizer()
clf = svm.SVR()
clf.fit(vec.fit_transform(features).toarray(), values)
print vec.get_feature_names()
base = {
u'origin': origin,
u'destination': destination,
u'departure_date' : iso_to_ordinal(departure_date),
u'return_date' : iso_to_ordinal(departure_date) + 7,
}
now = datetime.datetime.today().toordinal()
curr = 1000000000000.0
best_day = now
worst = 0.0
for days_in_advance in range(iso_to_ordinal(departure_date) - now + 1):
temp = base
temp[u'days_in_advance'] = days_in_advance
price = clf.predict(vec.transform(temp).toarray()) + random.uniform(-0.3,0.3)
if price < curr:
curr = price
best_day = iso_to_ordinal(departure_date) - days_in_advance
worst = max(worst, price)
best_day = min(best_day, max(iso_to_ordinal(departure_date) - 47, now))
amount_saved = worst - curr if best_day != now else 0.0
return datetime.date.fromordinal(best_day), amount_saved * 100.0
def parse_msg(msg):
origin = cities_regex.match(msg).group(1)
destination = cities_regex.match(msg).group(2)
month = ''
day = ''
try:
month = day_regex.match(msg).group(1)
day = day_regex.match(msg).group(2)
except Exception:
try:
month = day_regex_2.match(msg).group(1)
day = day_regex_2.match(msg).group(2)
except Exception:
if 'tomorrow' in msg.lower():
flight_date = datetime.date.today() + datetime.timedelta(1)
month = str(flight_date.month)
day = str(flight_date.day)
hour_side = ''
hour = ''
m = ''
try:
hour_side = time_regex.match(msg).group(1)
hour = time_regex.match(msg).group(2)
m = time_regex.match(msg).group(3)
except Exception:
pass
res = {
'origin': origin,
'destination': destination,
'month': month,
'day': day,
'hour_side': hour_side,
'hour': hour,
'm': m
}
return res
if __name__ == "__main__":
app.run(debug=True)