forked from izconcept/Trippy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpedia.js
137 lines (121 loc) · 4.21 KB
/
expedia.js
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
var EXPEDIA_KEY = '1XKcKOZGpyfXQZvmupYy8cbK2XatgGUP';
var SUMMER_DATE = '2016-07-15';
var SUMMER_DATE_OUT = '2016-07-17'
var FALL_DATE = '2016-10-15';
var FALL_DATE_OUT = '2016-10-17';
var WINTER_DATE = '2017-01-01-15';
var WINTER_DATE_OUT = '2017-01-17';
var SPRING_DATE = '2017-04-15';
var SPRING_DATE_OUTPUT = '2017-04-17';
function getJSON(theURL){
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theURL, false );
xmlHttp.send( null );
response = xmlHttp.responseText;
response = JSON.parse(response);
console.log(response);
return response;
}
function getFlightURL(home, destination, date){
var flight = "http://terminal2.expedia.com:80/x/flights/search/1/";
home = home.replace(/ /g,"%20");
destination = destination.replace(/ /g,"%20");
flight = flight.concat(home).concat("/").concat(destination).concat("/").concat(date).concat("?apikey=").concat(EXPEDIA_KEY);
return flight;
}
function getHotelsURL(city, country, date1, date2){
var hotels = " http://terminal2.expedia.com:80/x/mhotels/search?city=";
city = city.replace(/ /g,"%20");
country = country.replace(/ /g,"%20");
hotels = hotels.concat(city).concat("%20").concat(country).concat("&filterUnavailable=true&sortOrder=true&checkInDate=").concat(date1).concat("&checkOutDate=").concat(date2).concat("&room1=2&apikey=").concat(EXPEDIA_KEY);
return hotels;
}
function getHotelsArray(hotelsJSON){
array = hotelsJSON.hotelList;
return array;
}
function expensiveHotel(array){
var highest = 0;
for(i=1; i<array.length; i++) {
var temp = array[i];
if(parseInt(array[i].lowRateInfo.priceToShowUsers) > parseInt(array[highest].lowRateInfo.priceToShowUsers)){
highest = i;
}
}
return array[highest];
}
function cheapHotel(array){
var lowest = 0;
for(i=1; i<array.length; i++) {
var temp = array[i];
if(parseInt(array[i].lowRateInfo.priceToShowUsers) < parseInt(array[lowest].lowRateInfo.priceToShowUsers)){
lowest = i;
}
}
return array[lowest];
}
function averageFlightPrice(flightJSON){
return flightJSON.trendData.median;
}
//var flightURL = getFlightURL('YUL', 'LAX', SUMMER_DATE);
//var flightJSON = getJSON(flightURL);
//var hotels = getHotelsURL('toronto', 'canada', SUMMER_DATE, SUMMER_DATE_OUT);
//var hotelsJSON = getJSON(hotels);
//var hotelsArray = getHotelsArray(hotelsJSON);
//document.write(hotelsArray.toString());
//document.write(hotelsArray[0].lowRateInfo.averageRate);
//expensiveHotel(hotelsArray).name;
//cheapHotel(hotelsArray).name;
//document.write(averageFlightPrice(flightJSON));
function setDateTo( season){
var date;
switch (season){
case "summer":
date = SUMMER_DATE;
break;
case "fall":
date = FALL_DATE;
break;
case "winter":
date = WINTER_DATE;
break;
case "spring":
date = SPRING_DATE;
break;
}
return date;
}
function setDateFrom( season){
var date;
switch (season){
case "summer":
date = SUMMER_DATE_OUT;
break;
case "fall":
date = FALL_DATE_OUT;
break;
case "winter":
date = WINTER_DATE_OUT;
break;
case "spring":
date = SPRING_DATE_OUT;
break;
}
return date;
}
function result(home, homeAirport, dest, destAirport, destCountry, season){
var departDate = setDateTo(season);
var returnDate = setDateFrom(season);
var flightURL = getFlightURL(homeAirport, destAirport, departDate);
var flightJSON = getJSON(flightURL);
var flightPrice = averageFlightPrice(flightJSON);
var hotelsURL = getHotelsURL(dest, destCountry, departDate, returnDate);
var hotelsJSON = getJSON(hotelsURL);
var hotelsArray = getHotelsArray(hotelsJSON);
var cheapestHotel = cheapHotel(hotelsArray);
var mostExpensiveHotel = expensiveHotel(hotelsArray);
dest = dest.toUpperCase();
destCountry = destCountry.toUpperCase();
return output = "Average flight price: $" + flightPrice +"<br>Cheapest hotel: $" + cheapestHotel.lowRateInfo.averageRate + " per night<br>Most expensive hotel: $" + mostExpensiveHotel.lowRateInfo.averageRate + " per night";
}